简体   繁体   English

检测鼠标在子外点击 window

[英]Detect mouse click outside child window

I want to destroy a child window when user click somewhere outside its window.当用户单击 window 之外的某个位置时,我想销毁一个孩子 window。 I tried to use SetCapture() to detect mouse click.我尝试使用 SetCapture() 来检测鼠标点击。 Here is the code:这是代码:

HWND textbox;

//IN case WM_CREATE:
 CreateWindowEx(NULL, L"BUTTON", L"button!",
        WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 10, 10, 150, 40, hwnd,
        (HMENU)IDC_TEXTBOX, NULL, NULL);

 textbox = CreateWindowEx(
        NULL, L"EDIT", NULL,
        WS_CHILD | WS_VISIBLE | ES_MULTILINE | WS_BORDER,
        100, 100, 300, 200, hwnd, (HMENU)ID_TEXTBOX, NULL, NULL
    );
 SetWindowText(textbox, L"the initial text");

//IN CASE WM_ONLBUTTONDOWN:
  if (textbox != NULL) {
        if (SetCapture(textbox) == NULL) {
            ReleaseCapture();
            DestroyWindow(textbox);
            textbox = NULL;
        }
    }

The window did get destroyed when i clicked outside its region.当我在其区域之外单击时,window 确实被破坏了。 HOWEVER, when i click the button it didn't get destroyed as i expected.但是,当我单击按钮时,它并没有像我预期的那样被破坏。 I want the child window to get destroyed when user clicks WHEREVER outside its window .当用户单击window 之外的 WHEREVER时,我希望孩子 window 被破坏。 Why did my code fail?为什么我的代码失败了? And how do i do this right?我该怎么做呢?

EDIT : Here is a screenshot to make things clear.编辑:这是一个让事情变得清晰的屏幕截图。 The window only have an edit window with initial text and a button. window 只有一个带有初始文本和按钮的编辑 window。 As mentioned above, the child window disappears when i click outside its window but when i click the button (which is also outside window), the edit window remains.如上所述,当我在 window 外部单击时,子 window 消失,但是当我单击按钮(也在窗口外)时,编辑 window 仍然存在。 在此处输入图像描述

According to this thread , I find the EN_KILLFOCUS notification which will work for you in the Edit Control Document.根据这个线程,我在编辑控制文档中找到了适用于您的EN_KILLFOCUS通知。

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

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