windows - DefWindowProc WinApi 的行为

标签 windows winapi wm-paint

有人可以解释为什么使用 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/

相关文章:

windows - 如何将低级命令(非 APDU)发送到 Windows 10(移动)上的智能卡?

c - C语言的串口

winapi - 为什么有时会用空矩形调用 WM_PAINT?

c++ - WM_CTLCOLORSTATIC lParam, C++ WINAPI

c++ - 重新启动后禁用USB键盘

c++ - GradientFill API 未按应有的方式执行

c - 处理 WM_PAINT

php - 在 Windows 10 上的 apache2.4 上启动 laravel 应用程序

c - 从键盘获取输入,无需等待输入

c - 在事件查看器的情况下无法捕获 future 事件