开发者

correct usage of GetClipRgn?

I want to write a function that needs to set the clipping region on a DC but restore 开发者_如何学编程any existing clipping region on the DC when it is done.

So I found GetClipRgn which sounds like exactly what I want but seems confusing. I couldn't find any examples of using it and Petzold had nothing to offer.

What I came up with was this:

void DrawStuff( HDC hDC )
{
    HRGN restoreRegion = CreateRectRgn( 0, 0, 0, 0 );
    if (GetClipRgn( hDC, restoreRegion ) != 1)
    {
        DeleteObject( restoreRegion );
        restoreRegion = NULL;
    }

    // 
    // Set new region, do drawing
    //

    SelectClipRgn( hDC, restoreRegion );
    if (restoreRegion != NULL)
    {
        DeleteObject( restoreRegion );
    }
}

It just seems weird that I need to create a region in order to get the current region.

Is this correct usage?

Is there are better way to achieve the same effect?


Well the closest thing to a correct answer is Hans Passant's comment:

Yeah, it's a weird function. Your code looks okay.


I use the SaveDC and RestoreDC functions:

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.

It feels cleaner.


Will int SelectClipRgn( __in HDC hdc, __in HRGN hrgn); do the job?

The SelectClipRgn function selects a region as the current clipping region for the specified device context.

Only a copy of the selected region is used. The region itself can be selected for any number of other device contexts or it can be deleted.

The SelectClipRgn function assumes that the coordinates for a region are specified in device units.

To remove a device-context's clipping region, specify a NULL region handle.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜