简体   繁体   English

使用 C++/WinAPI/MFC 在编辑/文本框控件上捕获一个特殊的点击事件

[英]Trap a special click event on an Edit/Textbox control with C++/WinAPI/MFC

I'm coding in C++/MFC on a Windows platform (using MS VS2008.) I have this Edit control (which is basically a text box) that is set to be read-only.我在 Windows 平台上用 C++/MFC 进行编码(使用 MS VS2008。)我有这个设置为只读的编辑控件(基本上是一个文本框)。 The control displays some basic information.该控件显示一些基本信息。 I want to add an "Easter Egg" to my app, ie when a user Ctrl+Shift clicks on this edit control it must display some additional info.我想向我的应用程序添加一个“复活节彩蛋”,即当用户 Ctrl+Shift 单击此编辑控件时,它必须显示一些附加信息。 The question is how to trap such a click event using MFC/native WinAPIs?问题是如何使用 MFC/本机 WinAPI 捕获这样的点击事件?

The most straight forward way is to subclass the edit control using SetWindowLong and catch WM_LBUTTONDOWN event.最直接的方法是使用SetWindowLong子类化编辑控件并捕获WM_LBUTTONDOWN事件。 You'd then want to call GetAsyncKeyState or equivalent to check whether the specific key is being pressed or not, and show the message.然后,您需要调用GetAsyncKeyState或等效方法来检查是否按下了特定键,并显示消息。

There is no need to do subclassing.不需要做子类化。 Just catch WM_PARENTNOTIFY只要抓住WM_PARENTNOTIFY

case WM_PARENTNOTIFY: {
    if (LOWORD(wParam) == WM_LBUTTONDOWN)
        printf("x: %i, y: %i\n", LOWORD(lParam), HIWORD(lParam));
}
break;

By default, child windows in a dialog box have the WS_EX_NOPARENTNOTIFY style and so doesn't notify the parent window.默认情况下,对话框中的子窗口具有WS_EX_NOPARENTNOTIFY样式,因此不会通知父窗口。 You should remove this style.您应该删除此样式。

case WM_INITDIALOG: {
    HWND hChildWnd = GetDlgItem(hWnd, IDC_CHILD);
    LONG style = GetWindowLong(hChildWnd, GWL_EXSTYLE);
    style &= ~WS_EX_NOPARENTNOTIFY;
    SetWindowLong(hChildWnd, GWL_EXSTYLE, style);
    ...

PS I hope it's not too late :D PS我希望现在还不算太晚:D

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

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