取消选择编辑控制Win32 C++

发布于 2025-02-14 01:08:42 字数 1627 浏览 2 评论 0原文

我将如何在编辑控制中取消选择文本?

输入输入后,我希望用户能够取消选择编辑控件。 因为即使您单击它并按下键,它也会将其输入编辑。

这是我的编辑控件的代码:

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

我已经检查了MSDN文档,但没有找到任何其他样式以实现任务。

如果您对如何完成这项任务有任何想法,您可以帮助我吗?
谢谢。

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.

If you have any ideas on how to achieve this task could you help me out?
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

李不 2025-02-21 01:08:43

您可以使用相同的技巧来删除(组合框),弹出菜单等下拉列表。

  1. 您需要对编辑控件进行子类别,以便首先接收消息到自己的窗口过程。

  2. 在您的文本框子类中wm_setfocus处理程序,调用setCapture,即使在文本框外面,下一个单击也会传递到外面。

  3. 在文本框子类的处理程序中,用于鼠标单击消息,测试位置,如果在文本框外,请调用setFocus(null)放弃焦点。 (这是弹出窗口会解散自己的地方)。还调用发行()

  4. 还调用reareAcapture()在子类的wm_killfocus处理程序中,因为鼠标单击不是失去焦点的唯一方法。


以上是在文本框之外单击任何单击的方法删除其焦点。如果您只想在父窗口的客户端区域中单击即可进行文本框,那么您可以跳过子类和捕获,并且只需在父窗口中单击鼠标单击事件,调用setfocus(null)或setFocus(parenhhwnd)

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.

  2. In your textbox subclass WM_SETFOCUS handler, call SetCapture so the next click is delivered to the textbox even if it's outside.

  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. (This is where a popup would dismiss itself). Also call ReleaseCapture().

  4. Also call ReleaseCapture() in the subclass's WM_KILLFOCUS handler, since mouse clicks are not the only way to lose focus.


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).

╰つ倒转 2025-02-21 01:08:43

我在窗口proc中处理了wm_lbuttondown消息。

因此,当我单击父窗口上的鼠标按钮时,它将将焦点设置为父窗口。

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

如果您单击父窗口以外的任何其他窗口,则此方法可能不会触发WM_LBUTTONDOWN。

上述语句可能会有一些例外,但总体而言,它可以与按钮和编辑一起使用,但由于某种原因, 不使用静态文本。

答案由以下方式提供:
@paulsanders和@benvoigt

I handled the WM_LBUTTONDOWN message in the window proc.

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文