我试图使用C++设置一个简单的窗口,但我对CreateWindowEx的调用返回NULL。我使用的大部分代码都来自MSDN网站上的example。我尝试过的方法都不起作用,任何帮助都将不胜感激。
代码如下:
//Include the windows header
#include <Windows.h>
//Forward declaration of the WndProc function
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
//Main entry point
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
//Window class name
const wchar_t windowName[] = L"Window Class";
//Set up window class
WNDCLASS wnd;
wnd.lpfnWndProc = WndProc;
wnd.hInstance = hInstance;
wnd.lpszClassName = windowName;
//Register window class
RegisterClass(&wnd);
//Create window
//! This returns NULL
HWND hWnd = CreateWindowEx(
0,
windowName,
L"Windows Programming",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
//Simple check to see if window creation failed
if(hWnd == NULL) {
//Pause
system("PAUSE");
return -1;
}
//Show the window
ShowWindow(hWnd, nCmdShow);
//Main message loop
MSG msg;
while(GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
//WndProc function
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch(msg) {
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hDc = BeginPaint(hWnd, &ps);
FillRect(hDc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW + 1));
EndPaint(hWnd, &ps);
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}发布于 2012-12-02 02:16:44
请注意,来自MSDN的示例在设置它所关心的字段之前将WNDCLASS的所有字段置零。
WNDCLASS wnd = { }; // from MSDN example空大括号是C和C++的缩写,用于将整个结构初始化为0。将其编写为{ 0 }也很常见,它在技术上略有不同,但具有相同的净效果。
在您的代码中,您删除了初始化:
WNDCLASS wnd; // your code因此,您可能会在其他重要字段中获得一些垃圾值,比如cbClsExtra或cbWndExtra,这会导致类无法注册。因为类没有注册,所以不能创建该类的窗口。
发布于 2012-12-02 01:58:27
我已经让你的代码正常工作了。基本上,我在使用WNDCLASS (或WNDCLASSEX)结构时所做的就是使用所有参数,以确保不会遗漏某些东西。
//Include the windows header
#include <Windows.h>
//Forward declaration of the WndProc function
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
//Main entry point
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
//Window class name
const wchar_t windowName[] = L"Window Class";
//Set up window class
WNDCLASS wnd;
wnd.cbClsExtra = 0;
wnd.cbWndExtra = 0;
wnd.hCursor = LoadCursor(0, IDC_ARROW);
wnd.hIcon = LoadIcon(0, IDI_WINLOGO);
wnd.lpszMenuName = 0;
wnd.style = 0;
wnd.hbrBackground = 0;
wnd.lpfnWndProc = WndProc;
wnd.hInstance = hInstance;
wnd.lpszClassName = windowName;
//Register window class
RegisterClass(&wnd);
//Create window
//! This returns NULL
HWND hWnd = CreateWindowEx(
0,
windowName,
L"Windows Programming",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
//Simple check to see if window creation failed
if(hWnd == NULL) {
//Pause
system("PAUSE");
return -1;
}
//Show the window
ShowWindow(hWnd, nCmdShow);
//Main message loop
MSG msg;
while(GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
//WndProc function
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch(msg) {
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hDc = BeginPaint(hWnd, &ps);
FillRect(hDc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW + 1));
EndPaint(hWnd, &ps);
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}https://stackoverflow.com/questions/13661912
复制相似问题