gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 23:38:11 25 4
gpt4 key购买 nike

所以,我有两个功能。首先用于处理父事件,其次用于处理子事件。只是一个微不足道的方法。

// 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_COMMAND case 条目。也许我用错误的名字注册了类(class)?不,我复制粘贴了它们。我用谷歌搜索但没有找到答案。请帮忙。

最佳答案

来自 MSDN WM_COMMAND .

lParam represents a handle to a control window and in case of menu or accelerator its 0.

你应该使用 LOWORD (wparam) 获取命令 ID。

关于C++ winApi 无法处理子窗口事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57580800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com