我想設置一個簡單的窗口使用C++,但我的電話CreateWindowEx
返回NULL
。我使用的大多數代碼都來自MSDN網站上的example。沒有我已經嘗試過的工作,任何幫助將不勝感激。C++ CreateWindowEx返回NULL
下面是代碼:
//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);
}
我在代碼中看不到'GetLastError',但你在這裏。不過,我注意到填寫你的'WNDCLASS'的幾個方面都失蹤了。 – chris
使用'GetLastError'獲取錯誤代碼併發布。 – Nawaz
RegisterClass成功了嗎?它返回了什麼。 –