EM_SETHANDLE

2.x

EM_SETHANDLE
wParam = (WPARAM) (HLOCAL) hloc; /* handle of local memory object */
lParam = 0L;                     /* not used, must be zero        */

An application sends an EM_SETHANDLE message to set the handle to the local memory that will be used by a multiline edit control.

This message is processed only by multiline edit controls.

Parameters

hloc

Value of wParam. Identifies the local memory. This handle must have been created by a previous call to the LocalAlloc function using the LMEM_MOVEABLE flag. The memory should contain a null-terminated string, or the first byte of the allocated memory should be set to zero.

Return Value

This message does not return a value.

Comments

Before an application sets a new memory handle, it should send an EM_GETHANDLE message to retrieve the handle to the current memory buffer and should free that memory by using the LocalFree function.

Sending an EM_SETHANDLE message clears the undo buffer (EM_CANUNDO returns zero) and the internal modification flag (EM_GETMODIFY returns zero). The edit-control window is redrawn.

An application can send this message to a multiline edit control in a dialog box only if it has created the dialog box with the DS_LOCALEDIT style flag set.

Example

This example frees the current memory for the edit control, allocates new memory, and reads up to BUF_SIZE bytes of a file into the allocated memory. It then sends an EM_SETHANDLE message to set the handle of the edit control to the new memory, effectively placing up to BUF_SIZE bytes of the file into the edit control.

#define BUF_SIZE 4 * 1024

HANDLE hFile;
OFSTRUCT ofs;
HLOCAL hOldMem, hNewMem;
PSTR pBuf;
int cbRead;

/* Get the handle to the old memory and free it. */

hOldMem = (HLOCAL) SendDlgItemMessage(hdlg,
    ID_MYEDITCONTROL, EM_GETHANDLE, 0, 0L);
LocalFree(hOldMem);

/* Allocate new memory and read the file into it. */

hNewMem = LocalAlloc(LMEM_MOVEABLE, BUF_SIZE);
pBuf = LocalLock(hNewMem);
hFile = OpenFile("test.txt", &ofs, OF_READ);
cbRead = _lread(hFile, pBuf, BUF_SIZE);
pBuf[cbRead] = '\0';          /* terminating null character */
_lclose(hFile);

/* Adjust the buffer for the amount actually read in. */

LocalReAlloc(hNewMem, cbRead, 0);

/* Set the handle to the new buffer. */

LocalUnlock(hNewMem);
SendDlgItemMessage(hdlg, ID_MYEDITCONTROL,
    EM_SETHANDLE, hNewMem, 0L);

See Also

EM_CANUNDO, EM_GETHANDLE, EM_GETMODIFY, LocalAlloc, LocalFree