How to get the visible region of a window or CDC(转自http://www.programmersheaven.com)

If one is dealing with an application that performs a lot of painting or drawing, the OnDraw function can spend a lot of time each time it is called by the framework. In this situation, if the user hides the application main window and brings it back again, he will be punished with a long waiting period to recover the original aspect of the view.

This boring waiting time can be saved if the application recovers the last drawn CDC bitmap whenever no redrawing is necessary. And that works fine if one is able to find out what is the clipping region that has to be saved (normally using BitBlt) and lately recovered.

It seems to be a fair amount of MFC and API functions that deal with this problem but no one seems to be able to provide the actual clipping region for a CDC and the visible region seems to be out of reach. We comment here some of them.

CWnd::GetUpdateRgn( CRgn* pRgn, BOOL bErase = FALSE ); Will give us the window update region, if it is invoked before beginpaint or before CDC is created. This region has to be intersected with the visible region of the window to obtain the really updated region.

APIs GetClipRgn(HDC hdc, HRGN hrgn ) and GetMetaRgn(HDC hdc, HRGN hrgn ); Those functions seem to be designed to fit our purposes but they are not given us the expected result.. In fact they are returning cero and they do not provide any useful info. After carefully reading, one can notice that Microsoft documentation states that

An application-defined clipping region is a clipping region identified by the SelectClipRgn function. It is not a clipping region created when the application calls the BeginPaint function.

If the user does not define a clipping region by means of SelectClipRgn the function simply indicates that there is none. This seems to be our case.

After several working days spent on this item, we do not have any good idea to get the visible region for a window and we are about to give up and let our users bored watching long unneeded redraws.

Does anyone have any suggestion to deal with this subject.

Report
Re: How to get the visible region of a window or CDC. Posted by AsmGuru62 on 17 Dec 2002 at 8:36 AM
: If one is dealing with an application that performs a lot of painting or drawing, the OnDraw function can spend a lot of time each time it is called by the framework. In this situation, if the user hides the application main window and brings it back again, he will be punished with a long waiting period to recover the original aspect of the view.
:
: This boring waiting time can be saved if the application recovers the last drawn CDC bitmap whenever no redrawing is necessary. And that works fine if one is able to find out what is the clipping region that has to be saved (normally using BitBlt) and lately recovered.
:
: It seems to be a fair amount of MFC and API functions that deal with this problem but no one seems to be able to provide the actual clipping region for a CDC and the visible region seems to be out of reach. We comment here some of them.
:
: CWnd::GetUpdateRgn( CRgn* pRgn, BOOL bErase = FALSE ); Will give us the window update region, if it is invoked before beginpaint or before CDC is created. This region has to be intersected with the visible region of the window to obtain the really updated region.
:
: APIs GetClipRgn(HDC hdc, HRGN hrgn ) and GetMetaRgn(HDC hdc, HRGN hrgn ); Those functions seem to be designed to fit our purposes but they are not given us the expected result.. In fact they are returning cero and they do not provide any useful info. After carefully reading, one can notice that Microsoft documentation states that
:
: An application-defined clipping region is a clipping region identified by the SelectClipRgn function. It is not a clipping region created when the application calls the BeginPaint function.
:
: If the user does not define a clipping region by means of SelectClipRgn the function simply indicates that there is none. This seems to be our case.
:
: After several working days spent on this item, we do not have any good idea to get the visible region for a window and we are about to give up and let our users bored watching long unneeded redraws.
:
: Does anyone have any suggestion to deal with this subject.
:
:
:
Good topic!

How many objects do you have on your document?
Also, what is a nature of your application?

I have a couple of things to say for improving drawing performance.

1. If you have complex little things you draw - try to compose them with a set of bitmaps from RC file. Do not draw pixels or polygons if it can be avoided. In most cases it can be done easy.

2. When the application comes from minimized state to a normal state - there is no clipping region - the whole client area will be painted.

3. Use CDC::RectVisible to detect areas which needs to be painted. Every object - whatever the real object form is - can be surrounded by a RECT, so you can detect with that function - does it really needs to be painted or not.

4. Use resource caching: if you have a 1000 lines drawn by a RED pen or whatever color it is, but it is the same color - do you do it every object:

HPEN hPen = CreatePen (...);
DrawObject (...);
DeleteObject (hPen);

do not do it 1000 times - do it once at the beginning of OnDraw() and use it 1000 times. Same goes for every GDI resource - BRUSH, FONT, etc. - all same things can be cached.

5. If you use a lot of bitmaps - cache them too - there is a lot involved in drawing a bitmap:

- create a memory DC,
- select bitmap into it,
- draw something,
- deselect bitmap,
- destroy memory DC.

Make a class for such memory DC - you will save 2 first steps and 2 last steps!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值