RAII classes to simplify GDI programming

"Peter Kvitek" <[email protected]> Mon, 21 Nov 2011 11:37:59 -0800
Newsgroups gmane.comp.windows.wtl
Message-ID <[email protected]>
I've been using WTL for years and always felt it's the most useful tool ever
created for Win32 GUI programming. Thank you, Nenad!

However, GDI programming still lacks the level of elegance common for WTL.
For example, to set up a display context for drawing, one has to write code
like this:

HFONT hFontOld = dc.SelectFont(hMyFont);
COLORREF clrOld = dc.SetTextColor(clrMyTextColor);
HBITMAP hbmOld = dcMem.SelectBitmap(hMyBitmap);

. (drawing code)

dcMem.SelectBitmap(hbmOld);
dc.SetTextColor(clrOld);
dc.SelectFont(hFontOld);

It's tedious, hard to maintain and leads to resource leaks if the drawing
code does not proceed to cleanup part.

A set of simple RAII classes found here:
http://www.kvitek.com/pub/atlgdiraii.h resolves all these problems and makes
GDI drawing code much more elegant and safe, here's the example:

CSelectFont selectFont (dc, hMyFont);
CSetTextColor setTextColor (clrMyTextColor);
CSelectBitmap selectBitmap (dcMem, hMyBitmap);

. (drawing code)



Below is the list of classes that are implemented. I believe this covers all
the important GDI states, let me know if I missed something.

CSelectPen
CSelectBrush
CSelectFont
CSelectBitmap
CSelectPalette
CSetBkMode
CSetBkColor
CSetTextColor
CSetPolyFillMode
CSetROP2
CSetStretchBltMode
CSetMapMode
CSetArcDirection
CSetTextCharacterExtra
CSetBrushOrg
CSaveRestoreDC

Feel free to use it any way you like. I hope you'll find it as useful as I
did.

Thank you,
/Peter

 



[Non-text portions of this message have been removed]



------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/wtl/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/wtl/join
    (Yahoo! ID required)

<*> To change settings via email:
    [email protected] 
    [email protected]

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/