C++ winAPI 无法处理子窗口事件



所以,我有两个函数。第一个用于处理父事件,第二个用于处理子事件。只是一个微不足道的方法。

// Parent
LONG WINAPI WndProc(HWND hwnd, UINT Message,
WPARAM wparam, LPARAM lparam) {
// задаются пользователем, по идее.
HDC hdc;
PAINTSTRUCT ps;
switch (Message) {
case WM_LBUTTONDOWN:
{
ShowWindow(hChildWnd, SW_SHOW | SW_SHOWNORMAL);
UpdateWindow(hChildWnd);
}
break;
case WM_COMMAND:
switch (lparam) {
case 300:
showNotification(L"Hey"); // not working???
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, Message, wparam, lparam);
}
return 0;
}
// Child
LONG WINAPI WndProc1(HWND hwnd, UINT Message,
WPARAM wparam, LPARAM lparam) {
switch (Message) {
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND:
switch (lparam) {
case 300:
showNotification(L"Hey"); // not working???
break;
}
break;
default:
return DefWindowProc(hwnd, Message, wparam, lparam);
}
return 0;
}

当然,我在WinMain函数中创建、注册类并初始化这些窗口:

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
MSG msg;
// parent
WNDCLASS wc;
memset(&wc, 0, sizeof(WNDCLASS));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = CreateSolidBrush(0x00FFFFFF);
wc.lpszClassName = L"My Class";
RegisterClass(&wc);
// Child
WNDCLASS wcChild1;
memset(&wcChild1, 0, sizeof(WNDCLASS));
wcChild1.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wcChild1.lpfnWndProc = (WNDPROC)WndProc1;
wcChild1.hInstance = hInstance;
wcChild1.hbrBackground = CreateSolidBrush(0x00FFFFFF);
wcChild1.lpszClassName = L"My Dialog Class";
RegisterClass(&wcChild1);

hWnd = CreateWindowW(L"My Class", L"График функции loga(x)",
WS_OVERLAPPEDWINDOW,
00, 00, 1366, 768, NULL, NULL,
hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
hChildWnd = CreateWindowW(
L"My Dialog Class", L"Диалог",
WS_OVERLAPPED | WS_CAPTION,
10,
10,
90,
170,
NULL,
NULL,
hInstance,
NULL);

HWND hButton1 = CreateWindow(_T("button"), _T("OK"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON ,
10,//x
90,//y
80,//width
30,//height
hChildWnd,
(HMENU)300,//id кнопки
hInstance, NULL);

ShowWindow(hChildWnd, SW_HIDE);
UpdateWindow(hChildWnd);

while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

但是我无法在WndProc1内处理此单击事件。在WndProc之内也是如此。 我的调试器只是绕过了我的WM_COMMANDcase条目。也许我用错误的名称注册了类?不,我复制粘贴了它们。我用谷歌搜索但没有找到答案。请帮忙。

来自 MSDN WM_COMMAND。

lParam 表示控制窗口的句柄,在菜单或 加速其 0.

您应该使用 LOWORD(wparam( 来获取命令 ID。

相关内容

  • 没有找到相关文章

最新更新