我尝试createWindow但ghWnd总是为null,奇怪的是,在我朋友的笔记本电脑上,这很好,但是在我的笔记本电脑上,我有一个问题,因为ghWnd为null,它不会出现。
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS wndclass;
/* Register the frame class */
wndclass.style = 0;
wndclass.lpfnWndProc = (WNDPROC)MainWndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (hInstance, szAppName);
wndclass.hCursor = LoadCursor (NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wndclass.lpszMenuName = szAppName;
wndclass.lpszClassName = szAppName;
if (!RegisterClass (&wndclass) )
return FALSE;
/* Create the frame */
ghWnd = CreateWindow (szAppName,
"Proyek Grafkom",
WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
CW_USEDEFAULT,
CW_USEDEFAULT,
WIDTH,
HEIGHT,
NULL,
NULL,
hInstance,
NULL);
/* make sure window was created */
if (!ghWnd)
return FALSE;
/* show and update main window */
ShowWindow (ghWnd, nCmdShow);
UpdateWindow (ghWnd);
}
我该做什么?
和消息循环?这是我的代码,复制:
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam,
lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE
hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
MSG messages;
const char* classnm = "Thw window classname";
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
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)(COLOR_WINDOW+1);
wc.lpszMenuName = nullptr;
wc.lpszClassName = classnm;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
std::cout << "Window registration failed!";
return -1;
}
HWND hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
classnm,
"Answer on the stack overflow",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
nullptr, nullptr, hInstance, NULL);
if(hwnd == nullptr)
{
std::cout << "Window creation failed!";
return -2;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
//Mesaage loop:
while(GetMessage(&messages, nullptr, 0, 0) > 0)
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return 0;
}