问题
I am unable to figure out how to properly use the EM_SETHANDLE mechanism to set the text for an edit control. Get and Set window text will be too slow for my application.
From the documentation I understand that the allocated buffer will be sued by the control and it works partially for me.
When the text is entered in the control, it is seen in the buffer but when the buffer is updated using memcpy etc (no bug in the code), the updated text won't show properly. I even tried EM_SETHANDLE (SetHandle() ) after every update but it fails after a couple of attempts. There is some kind of heap allocation failure. RedrawWindow() won't work either.
I am unable to get any proper info on the net on the usage. Help!
My code, leaving the app specific details, looks like this.
// init
HANDLE m_hMem = HeapAlloc(...)
m_edit.SetHandle(m_hMem)
// on some event
char *pbuf = (char*)m_hMem;
memcpy(...)
thanks in advance
回答1:
The docs for EM_GETHANDLE tells you that this memory has to be movable memory allocated by LocalAlloc.
I'm guess you can get away with something like this:
int cbCh = sizeof(TCHAR) > 1 ? sizeof(TCHAR) : IsUsingComCtlV6() ? sizeof(WCHAR) : sizeof(char);
HLOCAL hOrgMem = SendMessage(hEdit,EM_GETHANDLE,0,0);
HLOCAL hNewMem = LocalReAlloc(hOrgMem,cbCh * cchYourTextLength,LMEM_MOVEABLE);
if (hNewMem)
{
//LocalLock, assign string, LocalUnlock
SendMessage(hEdit,EM_SETHANDLE,(WPARAM)hNewMem,0);
}
回答2:
Looks like you need to allocate the memory with LocalAlloc
. See the companion message EM_GETHANDLE: http://msdn.microsoft.com/en-us/library/bb761576(v=vs.85).aspx
来源:https://stackoverflow.com/questions/5500237/how-to-use-em-sethandle-on-edit-control