user2286339
user2286339

Reputation: 194

Win32 edit control EM_SETSEL doesn't work

I have a button and a single-line edit control with some default text, and I want to highlight all the edit control's text when the button is clicked.

For some reason, SendMessage has no effect and the text isn't highlighted - what am I doing wrong?

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
    case WM_CREATE:                                     
        hwndEdit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("edit"), L"Default text",
            WS_CHILD | WS_VISIBLE | ES_LEFT | ES_AUTOHSCROLL,
            10, 10, 150, 24, hWnd, (HMENU)IDC_EDIT,
            hInst, NULL);

        hwndButton = CreateWindowEx(0, TEXT("button"), L"Mark text",
            WS_CHILD | WS_VISIBLE,
            100, 100, 75, 24, hWnd, (HMENU)IDC_BUTTON,
            hInst, NULL);
        break;

    case WM_COMMAND:                                    
        {
            // Parse the menu selections:
            switch (LOWORD(wParam))
            {
                case IDC_BUTTON:
                    SendMessage(hwndEdit, EM_SETSEL, 0, -1);
                    break;

                case IDM_ABOUT:
                    DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                    break;
                case IDM_EXIT:
                    DestroyWindow(hWnd);
                    break;
                default:
                    return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;

    case WM_PAINT:                                      
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code that uses hdc here...
            EndPaint(hWnd, &ps);
        }
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

Thank you! Chris

Upvotes: 2

Views: 1618

Answers (1)

Anders
Anders

Reputation: 101756

Have you tried the ES_NOHIDESEL style?

Negates the default behavior for an edit control. The default behavior hides the selection when the control loses the input focus and inverts the selection when the control receives the input focus. If you specify ES_NOHIDESEL, the selected text is inverted, even if the control does not have the focus.

Upvotes: 6

Related Questions