I have a program where I allow the user to determine the length of the selected text in an edit control. The user makes a menu selection which calls a function which gets the menu selection length (or the length of text in the whole control if there is no selection). The length then shows up in a MessageBox.
This works ok but when the MessageBox opens, the selection is lost. I tried to reset the selection immediately afterwards but it doesn't work. Here's the function:
void TextLength(HWND hWnd)
{
char pTemp[10];
ULONG len;
ULONG start;
ULONG end;
SendMessage(hWnd, EM_GETSEL, (WPARAM) &start, (LPARAM) &end);
if ((len = end - start) == 0) len = GetWindowTextLength(ghEdit); //if no selection, count all
ltoa(len, pTemp, 10);
MessageBox(ghEdit, pTemp, "Length of Text", MB_OK | MB_APPLMODAL);
SendMessage(hWnd, EM_SETSEL, (WPARAM) start, (LPARAM) end);
}
Thing is, if I comment out the MessageBox line, the EM_SETSEL message sends ok (I can tell because if I change it to something like: SendMessage(ghEdit, EM_SETSEL, (WPARAM) 0, (LPARAM) end); I can see the selection change). as long as I don't open a MessageBox. I tried a bunch of variations on both the MessageBox flags/HWNDS etc. as well as successive MessageBoxes to verify that the start and end values are still there. However, nothing works. Maybe I have to send an additional message to the main window? Any suggestions appreciated.
--Will Duty
wduty@radicalfringe.com
This works ok but when the MessageBox opens, the selection is lost. I tried to reset the selection immediately afterwards but it doesn't work. Here's the function:
void TextLength(HWND hWnd)
{
char pTemp[10];
ULONG len;
ULONG start;
ULONG end;
SendMessage(hWnd, EM_GETSEL, (WPARAM) &start, (LPARAM) &end);
if ((len = end - start) == 0) len = GetWindowTextLength(ghEdit); //if no selection, count all
ltoa(len, pTemp, 10);
MessageBox(ghEdit, pTemp, "Length of Text", MB_OK | MB_APPLMODAL);
SendMessage(hWnd, EM_SETSEL, (WPARAM) start, (LPARAM) end);
}
Thing is, if I comment out the MessageBox line, the EM_SETSEL message sends ok (I can tell because if I change it to something like: SendMessage(ghEdit, EM_SETSEL, (WPARAM) 0, (LPARAM) end); I can see the selection change). as long as I don't open a MessageBox. I tried a bunch of variations on both the MessageBox flags/HWNDS etc. as well as successive MessageBoxes to verify that the start and end values are still there. However, nothing works. Maybe I have to send an additional message to the main window? Any suggestions appreciated.
--Will Duty
wduty@radicalfringe.com