gpt4 book ai didi

c++ Win32 Api GetMessage 线程内关闭程序

转载 作者:太空宇宙 更新时间:2023-11-04 13:01:35 25 4
gpt4 key购买 nike

我正在使用 win32 api 构建一个接口(interface),我想用一个线程来管理另一个类中的所有内容,以继续在 main 中进行工作。我有这段代码:

WindowManager.h

class UIManager::WindowManager {
private:
//Class data
HINSTANCE hInstance;
WNDCLASSEX winClass; /* Data structure for the windowclass */
MSG msg;
HWND hwnd;
//Window Data
LPCSTR wiName;
int startX = 250;
int startY = 150;
int endX = 544;
int endY = 375;
//Private managers
void makeWindow();
public:
WindowManager(HINSTANCE & hInstance, std::string wiName = "Window Name");
void show();
};

WindowManager.c(我使用 bind 是因为我在命名空间中有类,这是我发现线程允许我编译而不是抛出错误的唯一方法)

#include "WindowManager.h"

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

//Private
//
void UIManager::WindowManager::makeWindow()
{
int ms = GetMessage(&msg, 0, 0, 0); //I do this to see if it gets to
while (GetMessage(&msg, NULL, 0, 0)) { //this part, but never happens
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

//Public
//
UIManager::WindowManager::WindowManager(HINSTANCE & hInstance, std::string wiName)
{
this->wiName = wiName.c_str();
this->hInstance = hInstance;
winClass.cbSize = sizeof(winClass);
winClass.hInstance = hInstance;
winClass.lpszClassName = this->wiName;
winClass.lpfnWndProc = WndProc; //Execution callback
//Load default editable ellements
winClass.hCursor = LoadCursor(0, IDC_ARROW); /*Default*/
winClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); /*Default*/ //Alt+Tab Dialog
winClass.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
winClass.lpszMenuName = NULL; /* No menu */
RegisterClassEx(&winClass);

//Create Window
hwnd = CreateWindowEx(
0,
this->wiName, /* Title Class */
this->wiName, /* Title Text */
WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE | WS_OVERLAPPEDWINDOW,
startX, /* X Start */
startY, /* Y Start */
endX, /* The programs width */
endY, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hInstance, /* Program Instance handler */
NULL
);
SetWindowPos(hwnd, 0, 0, 0, 20, 20, 0);
}

void UIManager::WindowManager::show()
{
std::thread listener(std::bind(&WindowManager::makeWindow, this));
listener.detach();
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
{ //This part is executed
//...blablabla
break;
}

case WM_COMMAND:
{//This part is never executed
//...blablabla
break;
}

case WM_DESTROY:
{//This part is never executed
//...blabla
PostQuitMessage(0);
break;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}

窗口正确执行和显示,甚至执行 WM_CREATE,但是当“GetMessage”被执行时,它会立即结束程序而不会抛出任何错误或类似的东西。

我用 GetMessage 的值添加了一个 int 以查看它是否在 Debug模式下到达“while”,但它确实发生了。有没有办法查看可能抛出的错误或阻止它关闭程序?还是我在线程内部调用“GetMessage”时做错了?

最佳答案

你的代码有几个问题:

  • 您在一个线程中创建窗口,但尝试在另一个线程中运行消息循环。 GetMessage 只处理属于调用线程的窗口消息。
  • 您永远不会等待后台线程处理消息,而是与其分离并继续在主线程中执行,可能会结束应用程序。
  • 您不检查 GetMessage 返回的值并将其用作 bool 值。

如果你想创建后台 ui 线程,你需要将所有窗口创建代码移到那里,并在线程上调用 join。消息循环应该是这样的:

::MSG msg;
for(;;)
{
auto const res{::GetMessage(&msg, NULL, 0, 0))};
if(0 < res)
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
else if(0 == res)
{ // PostQuitMessage was called...
break;
}
else
{ // an error occurred...
break;
}
}

关于c++ Win32 Api GetMessage 线程内关闭程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43974900/

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