【发布时间】:2021-03-17 22:21:54
【问题描述】:
我是 C++ 中 win32 API GUI 编程的新手。当程序编译并执行时,它总是在CreateWindowsEx 处失败。抱歉,我现在只能解释这些。
除此之外,它还通过了窗口注册,这很好。但是从 Windows Creation 执行开始,它总是返回 NULL。
这是我的 C++ 代码:
#include <windows.h>
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int ncmdshow){
//MessageBox(NULL, "Hello ", " H", MB_OK);
WNDCLASSEX wc = {0};
//memset(&wc,0,sizeof(wc));
//wc.cbSize = sizeof(WNDCLASSEX);
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WindowProcedure; /* This is where we will send messages to */
wc.hInstance = hInst;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */
if(! RegisterClassEx(&wc) ){
MessageBox(NULL, "Error 1", " H", MB_OK);
return -1;
}
HWND hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, /* x */
CW_USEDEFAULT, /* y */
640, /* width */
480, /* height */
NULL,NULL,hInst,NULL);
//returns 0x0
//return GetLastError();
if( hwnd == NULL){
MessageBox(NULL, "Error 2", " H", MB_OK);
return GetLastError();
}
MSG msg = {0};
while( GetMessage(&msg, NULL, 0, 0) ){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp){
switch(msg){
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProcW(hWnd, msg, wp, lp);
}
return 0;
}
请以新手的水平接近,这样我很容易理解应该做什么。
【问题讨论】:
-
根据文档:“要获取扩展错误信息,请调用 GetLastError。”
-
@AlexF 更改为 "WindowClass" 但仍然失败 :-(
-
@AlexF 已编译但仍无法正常工作
-
@AlexF 实际上尝试了@AlexK 所说的。将
return -1替换为return GetLastError(),但返回 0x0 (ERROR_SUCCESS) -
@AlexF 我试过了,但它仍然返回 0x0(错误成功)
标签: c++ windows user-interface winapi