"SaveDC"和 "RestoreDC"函数不能在不同的消息处理代码中使用吗?

标签 c winapi gdi

我尝试在 WM_CREATE 消息中设置我的首选设备上下文属性,然后在 WM_PAINT 消息中使用它。我的方法是分别在WM_CREATEWM_PAINT消息中使用SaveDCRestoreDC函数。但结果并不能满足我的需求。我真正需要的是在客户区域的中心显示一个圆圈。

#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("HelloWin") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;

     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;

     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }

     hwnd = CreateWindow (szAppName,                  // window class name
                          TEXT ("The Hello Program"), // window caption
                          WS_OVERLAPPEDWINDOW,        // window style
                          CW_USEDEFAULT,              // initial x position
                          CW_USEDEFAULT,              // initial y position
                          CW_USEDEFAULT,              // initial x size
                          CW_USEDEFAULT,              // initial y size
                          NULL,                       // parent window handle
                          NULL,                       // window menu handle
                          hInstance,                  // program instance handle
                          NULL) ;                     // creation parameters

     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;

     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     HDC         hdc ;
     PAINTSTRUCT ps ;
     RECT rect;
     static int nSavedDC;

     switch (message)
     {          
     case WM_CREATE:
         hdc = GetDC(hwnd);
         SetMapMode(hdc, MM_LOMETRIC);
         GetClientRect(hwnd, &rect);
         SetViewportOrgEx(hdc, rect.right/2, rect.bottom/2, NULL);
         nSavedDC = SaveDC(hdc);         //I want to save the current state of device context to be used in WM_PAINT message.
         ReleaseDC(hwnd, hdc);
         return 0;

     case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
          RestoreDC(hdc, nSavedDC);       //Restore the the state of device context which is saved in WM_CREATE message.
          Ellipse(hdc, -100, 100, 100, -100);
          EndPaint (hwnd, &ps) ;
          return 0 ;

     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

最佳答案

您的代码缺乏错误处理。您需要检查每个 GDI 函数调用返回的所有值。

MSDN page 中所述SaveDC/RestoreDC 的目的主要是在绘制完成后将状态恢复到原始状态。这正是您在 WM_CREATEWM_PAINT 消息处理程序中没有执行的操作。您始终将 DC 置于修改状态。

至于使用SaveDC/RestoreDC一次设置DC状态,然后在每次绘制操作时快速恢复它,而不是每次我想到的时候都从头开始设置至少一个障碍:如果某些其他函数在处理程序 DC 恢复不在 DC 状态堆栈顶部的项目之间调用 RestoreDC ,那么位于堆栈顶部的已保存状态将被销毁,如下所示here .

关于 "SaveDC"和 "RestoreDC"函数不能在不同的消息处理代码中使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44610963/

相关文章:

c - 如何访问 UNICODE_STRING 命令行变量?

c++ - 虚拟 ListView 没有得到 CDDS_ITEMPREPAINT c++

c++ - DPI 意识真的需要吗?

c++ - 如何从 C++(winapi) 中的资源加载 GDI::Image (GIF)?

c++ - 如何以编程方式确定网卡是虚拟网卡?

c - GUI 到 Windows 服务

c - C 语言的欧拉数 10 项目

c - strcpy 的 valgrind 错误

c - 在进程启动时编辑 IAT(重定向到另一个 DLL)

c++ - 如何获取 GDI 句柄列表