Win32编辑控件EM_SETSEL不起作用

时间:2017-07-10 06:38:12

标签: c winapi

我有一个按钮和带有一些默认文本的单行编辑控件,我想在单击按钮时突出显示所有编辑控件的文本。

出于某种原因,SendMessage没有效果,文字没有突出显示 - 我做错了什么?

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;
}

谢谢! 克里斯

1 个答案:

答案 0 :(得分:4)

您是否尝试过ES_NOHIDESEL样式?

  

取消编辑控件的默认行为。当控件丢失输入焦点时,默认行为会隐藏选择,并在控件接收输入焦点时反转选择。如果指定ES_NOHIDESEL,则即使控件没有焦点,也会反转所选文本。