我尝试在WM_CREATE
消息中设置首选的设备上下文属性,然后在WM_PAINT
消息中使用它。我的方法是分别在SaveDC
和RestoreDC
消息中使用WM_CREATE
和WM_PAINT
函数。但是结果不符合我的需要。我真正需要的是在客户区域的中心显示一个圆圈。
#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_CREATE
和WM_PAINT
消息处理程序中都没有执行的操作。您始终将DC保留为修改状态。
至于使用SaveDC
/ RestoreDC
一次设置DC状态,然后在每次绘制操作时快速恢复它,而不是每次都从头开始设置它,我至少想到了一个障碍:如果某些其他函数在在处理程序之间的DC还原项目(不在DC状态堆栈的顶部)之间,则保存在堆栈顶部的状态将被破坏,如here所述。
关于c - 不能在不同的消息处理代码中使用“SaveDC”和“RestoreDC”功能吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44610963/