更新时间:2015-12-15 22:27:52浏览次数:1+次
int SaveDC(HDC hdc);
BOOL RestoreDC(HDC hdc,int nSavedDC);
hdc参数自然就是设备环境DC的句柄咯。这个DC句柄来源于各种获取DC的函数,比如BeginPaint、GetDC等。SaveDC的返回值就是指示状态的一个整数值。这个整数值就是RestoreDC函数第二个参数需要传入的。这样我们将这个整数值传入RestoreDC函数后,就可以将当前的DC状态恢复到这个整数对应的状态。TextOut(hdc,0,20*iY++,_T("开始绘制,准备保存DC"),lstrlen(_T("开始绘制,准备保存DC")));
SetTextColor(hdc,RGB(255,0,0));
TextOut(hdc,0,20*iY++,Info,lstrlen(Info));
iRed = SaveDC(hdc);//-保存一次
SetTextColor(hdc,RGB(0,255,0));
TextOut(hdc,0,20*iY++,Info,lstrlen(Info));
iGreen = SaveDC(hdc);//-保存第二次
SetTextColor(hdc,RGB(0,0,255));
TextOut(hdc,0,20*iY++,Info,lstrlen(Info));
iBlue = SaveDC(hdc);//-保存第三次
这样保存了三次之后,效果图如下:TextOut(hdc,0,20*iY++,_T("开始恢复DC绘制"),lstrlen(_T("开始恢复DC绘制")));
//-依次从上到下来恢复
RestoreDC(hdc,-1);//-恢复一次
TextOut(hdc,0,20*iY++,Info,lstrlen(Info));
RestoreDC(hdc,-1);//-恢复第二次
TextOut(hdc,0,20*iY++,Info,lstrlen(Info));
RestoreDC(hdc,-1);//-恢复第三次
TextOut(hdc,0,20*iY++,Info,lstrlen(Info));
执行后的栈的情况和窗口效果如下:TextOut(hdc,0,20*iY++,_T("最后试图被删掉的DC"),lstrlen(_T("最后试图被删掉的DC")));
RestoreDC(hdc,iGreen);
TextOut(hdc,0,20*iY++,Info,lstrlen(Info));
RestoreDC(hdc,iBlue);
TextOut(hdc,0,20*iY++,Info,lstrlen(Info));
执行后,栈顶指针没有变化,窗口效果图如下:#include "windows.h"
#include <tchar.h>
// - 项目是Unicode字符集
LRESULT CALLBACK WinProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
int iY=0;
TCHAR Info[100]=_T("C++技术网http://www.cjjjs.com");
int iRed,iGreen,iBlue;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);//会清除无效矩形
TextOut(hdc,0,20*iY++,_T("开始绘制,准备保存DC"),lstrlen(_T("开始绘制,准备保存DC")));
SetTextColor(hdc,RGB(255,0,0));
TextOut(hdc,0,20*iY++,Info,lstrlen(Info));
iRed = SaveDC(hdc);//-保存一次
SetTextColor(hdc,RGB(0,255,0));
TextOut(hdc,0,20*iY++,Info,lstrlen(Info));
iGreen = SaveDC(hdc);//-保存第二次
SetTextColor(hdc,RGB(0,0,255));
TextOut(hdc,0,20*iY++,Info,lstrlen(Info));
iBlue = SaveDC(hdc);//-保存第三次
TextOut(hdc,0,20*iY++,_T("开始恢复DC绘制"),lstrlen(_T("开始恢复DC绘制")));
//-依次从上到下来恢复
RestoreDC(hdc,-1);//-恢复一次
TextOut(hdc,0,20*iY++,Info,lstrlen(Info));
RestoreDC(hdc,-1);//-恢复第二次
TextOut(hdc,0,20*iY++,Info,lstrlen(Info));
RestoreDC(hdc,-1);//-恢复第三次
TextOut(hdc,0,20*iY++,Info,lstrlen(Info));
TextOut(hdc,0,20*iY++,_T("最后试图被删掉的DC"),lstrlen(_T("最后试图被删掉的DC")));
RestoreDC(hdc,iGreen);
TextOut(hdc,0,20*iY++,Info,lstrlen(Info));
RestoreDC(hdc,iBlue);
TextOut(hdc,0,20*iY++,Info,lstrlen(Info));
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrev,LPSTR lpCmd,int iShow)
{
TCHAR ClassName[] = _T("MyClass");
TCHAR title1[] = _T("C++技术网http://www.cjjjs.com");
WNDCLASS wndClass;
wndClass.cbClsExtra=0;
wndClass.cbWndExtra=0;
wndClass.hbrBackground= (HBRUSH)GetStockObject(GRAY_BRUSH);
wndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndClass.hInstance = hInstance;
wndClass.lpfnWndProc = WinProc;
wndClass.lpszClassName = ClassName;
wndClass.lpszMenuName=NULL;
wndClass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndClass))
{
return 0;
}
HWND hwnd = CreateWindow(ClassName,title1,WS_OVERLAPPEDWINDOW,0,0,440,400,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
MSG msg;
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
相关资讯