gpt4 book ai didi

winapi - Win32 API - RegisterClassEx 错误

转载 作者:行者123 更新时间:2023-12-02 07:43:29 24 4
gpt4 key购买 nike

我正在尝试使用 VC++ 编译器和 Visual Studio 通过 Win32 API 打开一个简单的窗口。我想知道为什么这门课不及格;我试过在没有指针的情况下分配它,以及将它作为指针分配并将它作为引用发送给函数。然而,无论我尝试什么,RegisterClassEx 函数都拒绝返回 true。

这是为什么,可以采取什么措施?

来自 WinMain

WNDCLASSEX* wc = new WNDCLASSEX;
HWND hwnd;
MSG msg;
bool done;

wc->style = CS_HREDRAW | CS_VREDRAW;
wc->lpfnWndProc = WndProc;
wc->cbClsExtra = 0;
wc->cbWndExtra = 0;
wc->hInstance = hInstance;
wc->hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc->hCursor = LoadCursor(NULL, IDC_ARROW);
wc->hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc->lpszClassName = L"someclass";

if (!RegisterClassEx(wc)) {
MessageBox(NULL, L"Class registration has failed!", L"Error!", MB_OK | MB_ICONINFORMATION);
return 0;
}

最佳答案

您必须通过填写cbSize 成员来告诉Windows 您的WNDCLASSEX 结构有多大。您在调用 RegisterClassEx 之前未能初始化此成员,这可能是该函数失败的原因。 sizeof 运算符就是您所需要的。

您还未能初始化该结构的其他一些成员,例如 lpszMenuName。如果您没有显式初始化它们,它们将包含垃圾数据,这可能会导致 RegisterClassEx 函数失败。如果您不使用它们,则需要将它们显式设置为 0。

此外,仅仅因为 RegisterClassEx 参数接受指向 WNDCLASSEX 结构的指针并不意味着您必须创建结构一个指针。您可以在堆栈上创建一个常规对象,并使用寻址运算符 (&) 将指针传递给该函数。

请注意,根据 the documentation , 您也可以调用 GetLastError function获取有关调用 RegisterClassEx 函数时出错的更多详细信息。这将帮助您在遇到问题时进行调试。

工作示例代码:

WNDCLASSEX wc    = {0};  // make sure all the members are zero-ed out to start
wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszClassName = L"someclass";

if (!RegisterClassEx(&wc)) {
MessageBox(NULL, L"Class registration has failed!",
L"Error!", MB_OK | MB_ICONERROR);
return 0;
}

关于winapi - Win32 API - RegisterClassEx 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8775443/

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