我想创建一个面板,它自己对按钮进行分组:
HWND my_panel = CreateWindow(
"STATIC",
"",
WS_VISIBLE | WS_CHILD | WS_BORDER,
30,
100,
300,
300,
main_window, // main dialog
NULL,
( HINSTANCE ) GetWindowLong( main_window, GWL_HINSTANCE ),
NULL
);
然后我在此面板中添加一个按钮:
HWND button_in_a_group = CreateWindow(
"BUTTON",
"Hello world",
WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
20,
20,
50,
50,
my_panel, // as a child for above
NULL,
( HINSTANCE ) GetWindowLong( main_window, GWL_HINSTANCE ),
NULL
);
单击按钮时,它不会向WM_COMMAND
发送WM_PARENTNOTIFY
回调函数。然后,如果我按Enter键,它就可以了 - 按钮会发送WM_COMMAND
。
如何在嵌套按钮上启用鼠标单击,以及为什么嵌套窗口无法按预期工作?
答案 0 :(得分:3)
将消息发送到父窗口。在这种情况下,静态窗口是按钮的父级。因此主窗口不接收按钮消息,WM_PARENTNOTIFY
除外。
您可以继承静态窗口:
SetWindowSubclass(my_panel, ChildProc, 0, 0);
定义ChildProc
以捕获按钮消息。另请参阅Subclassing Controls
该按钮还需要一个标识符,如下所示:
CreateWindow("BUTTON", "Hello world", ... my_panel, HMENU(BUTTON_ID) ...);
单击按钮时, WM_COMMAND
消息将发送到ChildProc
。 BN_CLICKED
通知带有BUTTON_ID
注意,SetWindowSubclass
需要额外的标头和库:
#include <CommCtrl.h>
#pragma comment(lib, "Comctl32.lib") //Visual Studio option for adding libraries
...
LRESULT CALLBACK ChildProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam, UINT_PTR, DWORD_PTR)
{
switch(msg) {
case WM_COMMAND:
switch(LOWORD(wParam)) {
case BUTTON_ID:
MessageBox(0, "hello world", 0, 0);
break;
}
break;
case WM_NCDESTROY:
RemoveWindowSubclass(hwnd, ChildProc, 0);
break;
}
return DefSubclassProc(hwnd, msg, wParam, lParam);
}