简体   繁体   English

取消选择编辑控件win32 c++

[英]Deselect edit control win32 c++

How would I go about deselecting the text in edit control?我将如何去取消选择编辑控件中的文本?

After entering the input I want the user to be able to deselect the edit control.输入输入后,我希望用户能够取消选择编辑控件。 Because even after you click out of it and press a key, it gets entered into the edit.因为即使您单击它并按下一个键,它也会进入编辑。

Here is the code for my edit control:这是我的编辑控件的代码:

HFONT fontMain = CreateFont(
            -16,                                                // Height Of Font
            0,                                                  // Width Of Font
            0,                                                  // Angle Of Escapement
            0,                                                  // Orientation Angle
            0,      // Font Weight
            false,                              // Italic
            false,                          // Underline
            false,                          // Strikeout
            ANSI_CHARSET,                               // Character Set Identifier
            OUT_TT_PRECIS,                                      // Output Precision
            CLIP_DEFAULT_PRECIS,                                // Clipping Precision
            ANTIALIASED_QUALITY,                                // Output Quality
            FF_DONTCARE|DEFAULT_PITCH,                          // Family And Pitch
            TEXT("Calibri"));

HWND editControl = CreateWindow(
                TEXT("EDIT"),
                TEXT("TEST TEXT"),
                WS_CHILD | WS_VISIBLE | ES_LEFT | ES_MULTILINE,
                x, y, width, height,
                window,
                (HMENU) 100,
                instance,
                NULL);
SendMessage(window /* parent window */, WM_SETFONT, (WPARAM)fontMain, NULL);
DeleteObject(fontMain);

I have checked MSDN docs and have not found any additional styles to add to achieve the task.我检查了 MSDN 文档并没有找到任何额外的样式来添加来完成任务。

If you have any ideas on how to achieve this task could you help me out?如果您对如何完成此任务有任何想法,您能帮帮我吗?
Thank you.谢谢你。

You could use the same trick that works to dismiss dropdown list (of combo box), popup menus, and the like.您可以使用相同的技巧来消除(组合框的)下拉列表、弹出菜单等。

  1. You'll need to subclass the EDIT control so you receive messages first to your own window procedure.您需要继承 EDIT 控件,以便首先接收到您自己的窗口过程的消息。

  2. In your textbox subclass WM_SETFOCUS handler, call SetCapture so the next click is delivered to the textbox even if it's outside.在您的文本框子类WM_SETFOCUS处理程序中,调用SetCapture以便将下一次单击传递到文本框,即使它在外面。

  3. In the textbox subclasses's handler for mouse click messages, test the location and if outside the textbox, call SetFocus(NULL) to give up the focus.在文本框子类的鼠标单击消息处理程序中,测试位置,如果在文本框之外,则调用SetFocus(NULL)以放弃焦点。 (This is where a popup would dismiss itself). (这是弹出窗口会自行消失的地方)。 Also call ReleaseCapture() .也调用ReleaseCapture()

  4. Also call ReleaseCapture() in the subclass's WM_KILLFOCUS handler, since mouse clicks are not the only way to lose focus.还要在子类的WM_KILLFOCUS处理程序中调用ReleaseCapture() ,因为鼠标单击不是失去焦点的唯一方法。


The above is the way to have any click outside the textbox remove its focus.以上是让文本框外的任何点击移除其焦点的方法。 If you only want clicks in the client area of your parent window to defocus the textbox, then you can certainly skip the subclassing and capture, and just handle the mouse click events in the parent window, calling SetFocus(NULL) or SetFocus(parentHWnd) .如果您只想在父窗口的客户区域单击以使文本框失焦,那么您当然可以跳过子类化和捕获,而只需处理父窗口中的鼠标单击事件,调用SetFocus(NULL)SetFocus(parentHWnd) .

I handled the WM_LBUTTONDOWN message in the window proc.我在窗口过程中处理了 WM_LBUTTONDOWN 消息。

Such that when I click the mouse button on the parent window, it will set the focus to the parent window.这样当我在父窗口上单击鼠标按钮时,它将焦点设置到父窗口。

static LRESULT WINAPI WndProc
     (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message){
        case WM_LBUTTONDOWN : {
            // Passing the parent window as the parameter.
            SetFocus(HWND parentWindow);
        }
    }
}

This method works as the WM_LBUTTONDOWN might not be triggered if you click on any other window except the parent window.此方法的工作原理是,如果单击父窗口以外的任何其他窗口,可能不会触发 WM_LBUTTONDOWN。

There might be some exceptions to the above statement, but overall it works with buttons and edits but not static text for some reason.上述语句可能有一些例外,但总体而言,它适用于按钮和编辑,但由于某种原因不适用于静态文本

The answers were provided by: @PaulSanders and @BenVoigt答案由@PaulSanders 和@BenVoigt提供

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM