gpt4 book ai didi

c++ - C++ win32 中的 WM_COMMAND 捕捉按钮按下

转载 作者:可可西里 更新时间:2023-11-01 13:54:32 39 4
gpt4 key购买 nike

我正在尝试使用 WM_Command 在 c++ win32 中获取按钮按下事件

HWND hBtn;
HWND hBtnParent = HWND("UploadVideo");
HWND SelectVideoBTN, UploadBTN;
HWND hWnd;

HINSTANCE hUpload;
WNDCLASSEX wcexUpload;
int nCmdShowUpload = 1;
using namespace std;

LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
loader::alert("rrr");
switch (message)
{
case WM_COMMAND:
if (LOWORD(wParam) == WORD(SelectVideoBTN)) {
loader::alert("hello");
}
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}

SelectVideoBTN = CreateWindow(
L"BUTTON", // Predefined class; Unicode assumed
L"Select Video's", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
10, // x position
460, // y position
100, // Button width
25, // Button height
hWnd, // Parent window
NULL, // No menu.
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL); // Pointer not needed.

UploadBTN = CreateWindow(
L"BUTTON", // Predefined class; Unicode assumed
L"Upload", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
390, // x position
460, // y position
100, // Button width
25, // Button height
hWnd, // Parent window
NULL, // No menu.
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL); // Pointer not needed.

我一直在看这个例子 - http://forums.devshed.com/programming-42/create-button-clicked-148407.html - 但我不能完全让它工作,它甚至不会调用 CALLBACK WindowProcedure - 有没有人可以帮助我?

按钮出现在我创建的 window 上,我创建了 window -

WNDCLASSEX vidUploader;

vidUploader.cbSize = sizeof(WNDCLASSEX);

vidUploader.style = CS_HREDRAW | CS_VREDRAW;
vidUploader.lpfnWndProc = WndProc;
vidUploader.cbClsExtra = 0;
vidUploader.cbWndExtra = 0;
vidUploader.hInstance = hUpload;
vidUploader.hIcon = LoadIcon(hUpload, MAKEINTRESOURCE(IDI_P2GOVIDEOUPLOADER20));
vidUploader.hCursor = LoadCursor(NULL, IDC_ARROW);
vidUploader.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
vidUploader.lpszMenuName = MAKEINTRESOURCE(IDC_P2GOVIDEOUPLOADER20);
vidUploader.lpszClassName = (LPCWSTR)(L"UploadVideo");
vidUploader.hIconSm = LoadIcon(wcexUpload.hInstance, MAKEINTRESOURCE(IDI_SMALL));

RegisterClassEx(&vidUploader);

hInst = hUpload; // Store instance handle in our global variable

然后创建窗口

    hWnd = CreateWindow((LPCWSTR)(L"UploadVideo"), (LPCWSTR)(L"Upload Video's"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 100, NULL, NULL, hUpload, NULL);
if (!hWnd)
{
MessageBox(NULL, _T("Call to CreateWindow failed!"), _T("Win32 Guided Tour"), NULL);

return 1;
}


// The parameters to ShowWindow explained:
// hWnd: the value returned from CreateWindow
// nCmdShow: the fourth parameter from WinMain
ShowWindow(hWnd,
nCmdShowUpload);

UpdateWindow(hWnd);

最佳答案

子窗口(即具有 WS_CHILD 窗口样式的窗口)由唯一的数值标识,通常称为控件 ID 或窗口 ID。例如,它在收到 WM_COMMAND 消息时传递给父级。但是,您从未为按钮控件分配控件 ID,父窗口无法识别它们。如果是子窗口,调用 CreateWindow 时的 hMenu 参数重载以携带唯一标识符:

hMenu
For a child window, hMenu specifies the child-window identifier, an integer value used by a dialog box control to notify its parent about events. The application determines the child-window identifier; it must be unique for all child windows with the same parent window.

换句话说,您的应用程序选择一个数值分配给控件。由于较低的 ID 已被对话管理器使用(例如 IDOK),通常的做法是从 100 开始分配控件 ID(请参阅 Why do dialog editors start assigning control IDs with 100?)。

在你的WM_COMMAND然后,您可以将 LOWORD(wParam) 与分配给按钮控件的标识符进行比较。

您需要对代码应用以下更改。

// Declare control IDs. This is usually done in a file called Resource.h
#define IDC_SELECT_VIDEO (100)

更改您的窗口创建代码:

SelectVideoBTN = CreateWindow(
L"BUTTON", // Predefined class; Unicode assumed
L"Select Video's", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
10, // x position
460, // y position
100, // Button width
25, // Button height
hWnd, // Parent window
(HMENU)IDC_SELECT_VIDEO, // Assign appropriate control ID
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL); // Pointer not needed.

检查 WM_COMMAND 处理程序中的控件 ID:

    switch (message)
{
case WM_COMMAND:
if (LOWORD(wParam) == IDC_SELECT_VIDEO) {
loader::alert("hello");
}
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}


如果您的窗口过程根本没有被调用,这可能意味着您没有在调用线程上发送消息。 GUI 线程总是需要一个消息循环。标准的消息循环就足够了:

MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

关于c++ - C++ win32 中的 WM_COMMAND 捕捉按钮按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37271194/

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