2008 October 31th Friday (十月 三十一日 金曜日)

 Saving Device Contexts

  Normally when you call GetDC or BeginPaint, Windows gives you a device context with default values for all the attributes. Any changes you make to the attributes are
lost when the device context is released with the ReleaseDC or EndPaint call. If your program needs to use nondefault device context attributes, you'll have to initialize
the device context every time you obtain a new device context handle:

case WM_PAINT:
     hdc = BeginPaint (hwnd, &ps) ;
     [initialize device context attributes]
     [paint client area of window]
      EndPaint (hwnd, &ps) ;
       return 0 ;

  Although this approach is generally satisfactory, you might prefer that changes you make to the attributes be saved when you release the device context so that they will
be in effect the next time you call GetDC or BeginPaint. You can accomplish this by including the CS_OWNDC flag as part of the window class style when you register the window
class:

wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC ;

  Now each window that you create based on this window class will have its own private device context that continues to exist when the window is destroyed. When you use the
CS_OWNDC style, you need to initialize the device context attributes only once, perhaps while processing the WM_CREATE message:

case WM_CREATE:
     hdc = GetDC (hwnd) ;
     [initialize device context attributes]
     ReleaseDC (hwnd, hdc) ;

  The attributes continue to be valid until you change them.

  The CS_OWNDC style affects only the device contexts retrieved from GetDC and BeginPaint and not device contexts obtained from the other functions (such as GetWindowDC). Employing
CS_OWNDC was once discouraged because it required some memory overhead; nowadays it can improve performance in some graphics-intensive Windows NT applications. Even if you use CS_OWNDC,
you should still release the device context handle before exiting the window procedure.

  In some cases you might want to change certain device context attributes, do some painting using the changed attributes, and then revert to the original device context. To simplify
this process, you save the state of a device context by calling

idSaved = SaveDC (hdc) ;

  Now you can change some attributes. When you want to return to the device context as it existed before the SaveDC call, you use

RestoreDC (hdc, idSaved) ;

  You can call SaveDC any number of times before you call RestoreDC.

  Most programmers use SaveDC and RestoreDC in a different manner, however, much like PUSH and POP instructions in assembly language. When you call SaveDC, you don't need to save the return value:

SaveDC (hdc) ;

  You can then change some attributes and call SaveDC again. To restore the device context to a saved state, call

RestoreDC (hdc, -1) ;

  This restores the device context to the state saved by the most recent SaveDC function.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值