有人可以解释为什么使用 DefWindowProc 一切正常,但如果我删除它,字符串“Hello windows”会出现在屏幕上,没有窗口和任何按钮?“UpdateWindow”向窗口过程发送WM_PAINT消息,为什么会显示文本但跳过创建窗口?
#include <windows.h>
LRESULT CALLBACK proc1 (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Program");
HWND hwnd1;
MSG msg1;
WNDCLASS wndclass1;
wndclass1.style = CS_HREDRAW | CS_VREDRAW;
wndclass1.lpfnWndProc = proc1;
wndclass1.cbClsExtra = 0;
wndclass1.cbWndExtra = 0;
wndclass1.hInstance = hInstance;
wndclass1.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass1.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass1.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass1.lpszMenuName = NULL;
wndclass1.lpszClassName = szAppName;
RegisterClass (&wndclass1);
hwnd1 = CreateWindow ( szAppName,
TEXT ("My window"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL );
ShowWindow (hwnd1, iCmdShow);
UpdateWindow (hwnd1);
while ( GetMessage(&msg1, NULL, 0, 0) )
{
TranslateMessage(&msg1);
DispatchMessage(&msg1);
};
return msg1.wParam;
};
LRESULT CALLBACK proc1 (HWND hwnd1, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch (message)
{
case WM_CREATE:
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd1, &ps) ;
GetClientRect (hwnd1, &rect) ;
DrawText (hdc, TEXT ("Hello windows"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint (hwnd1, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
};
return DefWindowProc (hwnd1, message, wParam, lParam) ;
}