按钮按下处理 win32 c++
Button Press Handling win32 c++
我正在创建一个插件,它加载带有 2 个按钮的本机 window
,按下时它们应该会弹出一个消息框,但没有弹出窗口
正在为消息循环创建线程
//create message thread
class startMessageThreadLoop
{
public:
static DWORD WINAPI StaticThreadStart(void* Param)
{
MessageBox(hWnd, L"StaticThreadStart", L"StaticThreadStart", 0);
startMessageThreadLoop* This = (startMessageThreadLoop*)Param;
return This->ThreadStart();
}
DWORD ThreadStart(void)
{
MessageBox(hWnd, L"ThreadStart", L"ThreadStart", 0);
//create message loop for buttons
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0))
{
//translate and send messages
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
void startMyThread()
{
MessageBox(hWnd, L"startMyThread", L"startMyThread", 0);
DWORD ThreadID;
CreateThread(NULL, 0, StaticThreadStart, (void*) this, 0, &ThreadID);
char szTest[100];
printf(szTest, "%d", ThreadID);
MessageBox(hWnd, LPCWSTR(szTest), L"ThreadIDBaby", 0);
}
};
正在启动线程
startMessageThreadLoop ThreadLoopInstance;
ThreadLoopInstance.startMyThread();
正在创建 window
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);
手柄按钮
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
if (LOWORD(wParam) == IDC_SELECT_VIDEO) {
MessageBox(hWnd, L"Heeey", L"Hoi", 0);
}
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
}
正在创建按钮并显示 Window
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.
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.
RECT rect = { 0, 0, uploadWNDWidth, uploadWNDHeight };
AdjustWindowRect(&rect, GetWindowLong(hWnd, GWL_STYLE), FALSE);
SetWindowPos(hWnd, 0, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE);
if (!hWnd)
{
MessageBox(NULL, _T("Call to CreateWindow failed!"), _T("Win32 Guided Tour"), NULL);
return 1;
}
MSG msg;
// The parameters to ShowWindow explained:
// hWnd: the value returned from CreateWindow
//nCmdShow: the fourth parameter from WinMain
ShowWindow(hWnd,
nCmdShowUpload);
UpdateWindow(hWnd);
我相信我正在做我必须做的一切以使按钮工作,我在一个线程中有一个消息循环,我正在注册和处理按钮和 window,我错过了什么?
window 的消息队列链接到创建 window 的线程。消息循环必须在同一个线程中处理。
我正在创建一个插件,它加载带有 2 个按钮的本机 window
,按下时它们应该会弹出一个消息框,但没有弹出窗口
正在为消息循环创建线程
//create message thread
class startMessageThreadLoop
{
public:
static DWORD WINAPI StaticThreadStart(void* Param)
{
MessageBox(hWnd, L"StaticThreadStart", L"StaticThreadStart", 0);
startMessageThreadLoop* This = (startMessageThreadLoop*)Param;
return This->ThreadStart();
}
DWORD ThreadStart(void)
{
MessageBox(hWnd, L"ThreadStart", L"ThreadStart", 0);
//create message loop for buttons
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0))
{
//translate and send messages
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
void startMyThread()
{
MessageBox(hWnd, L"startMyThread", L"startMyThread", 0);
DWORD ThreadID;
CreateThread(NULL, 0, StaticThreadStart, (void*) this, 0, &ThreadID);
char szTest[100];
printf(szTest, "%d", ThreadID);
MessageBox(hWnd, LPCWSTR(szTest), L"ThreadIDBaby", 0);
}
};
正在启动线程
startMessageThreadLoop ThreadLoopInstance;
ThreadLoopInstance.startMyThread();
正在创建 window
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);
手柄按钮
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
if (LOWORD(wParam) == IDC_SELECT_VIDEO) {
MessageBox(hWnd, L"Heeey", L"Hoi", 0);
}
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
}
正在创建按钮并显示 Window
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.
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.
RECT rect = { 0, 0, uploadWNDWidth, uploadWNDHeight };
AdjustWindowRect(&rect, GetWindowLong(hWnd, GWL_STYLE), FALSE);
SetWindowPos(hWnd, 0, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE);
if (!hWnd)
{
MessageBox(NULL, _T("Call to CreateWindow failed!"), _T("Win32 Guided Tour"), NULL);
return 1;
}
MSG msg;
// The parameters to ShowWindow explained:
// hWnd: the value returned from CreateWindow
//nCmdShow: the fourth parameter from WinMain
ShowWindow(hWnd,
nCmdShowUpload);
UpdateWindow(hWnd);
我相信我正在做我必须做的一切以使按钮工作,我在一个线程中有一个消息循环,我正在注册和处理按钮和 window,我错过了什么?
window 的消息队列链接到创建 window 的线程。消息循环必须在同一个线程中处理。