Re: RAII classes to simplify GDI programming
"yarp" <lac-3QW3q7+2X3hWk0Htik3J/[email protected]> Sat, 26 Nov 2011 08:00:16 -0000
| Newsgroups | gmane.comp.windows.wtl |
|---|---|
| Message-ID | <[email protected]> |
Thank you for sharing, very useful indeed. But I have a doubt on what will occur if I use several instances of the same object. Let's assume this: CSetTextColor clr1(hDC, RGB(0,0,0)); CSetTextColor clr2(hDC, RGB(0xff,0,0)); m_oldTextColor for clr2 will be clr1, so restoring the previous DC state is depending on which order will the compiler call the objects destructors. Must be clr2, clr1 so all is ok, but is there a guarantee for this in C++ specifications ? Yarp --- In [email protected], "Peter Kvitek" <pete@...> wrote: > > 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/