有人可以解释为什么使用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) ;
}
最佳答案
如果您省略DefWindowProc
,则表示“对于上面我没有处理的所有消息,什么也不做。”这意味着许多消息(如“请绘制按钮”)将被视为“什么都不做”。结果:没有按钮。 Pass all unhandled messages to DefWindowProc。
关于windows - DefWindowProc WinApi的行为,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11375812/