SaveDC与RestoreDC

来源:互联网 发布:mysql中的日期函数 编辑:程序博客网 时间:2024/06/14 19:30

以下内容摘自Windows程序设计一书

某些情况下,您可能想改变某些设备内容属性,用改变后的属性进行绘图,然后恢复原来的设备内容。要简化这一过程,可以通过如下调用来保存设备内容的状态:

idSaved = SaveDC(hdc) ;

现在,可以改变一些属性,在想要回到调用SaveDC前存在的设备内容是,调用:

RestoreDC(hdc , idSaved) ;

您可以在调用RestoreDC之前调用SaveDC数次。

还有一种写法是:RestoreDC(hdc , -1) ;

这就将设备内容恢复到最近由SaveDC函数保存的状态中。

以下内容摘自MSDN

The SaveDC function saves the current state of the specified device context (DC) by copying data describing selected objects and graphic modes (such as the bitmap, brush, palette, font, pen, region, drawing mode, and mapping mode) to a context stack.

////////////////////////////////////////////////////////

The RestoreDC function restores a device context (DC) to the specified state. The DC is restored by popping state information off a stack created by earlier calls to the SaveDC function.
Remarks
Each DC maintains a stack of saved states. The SaveDC function pushes the current state of the DC onto its stack of saved states. That state can be restored only to the same DC from which it was created. After a state is restored, the saved state is destroyed and cannot be reused. Furthermore, any states saved after the restored state was created are also destroyed and cannot be used. In other words, the RestoreDC function pops the restored state (and any subsequent states) from the state information stack.

上面红线部分需要注意:在被恢复的状态之后的其余保存状态同样被摧毁并且不能被使用,也就是说当前恢复状态以及随后的状态都会被一起弹出。