Re: RAII classes to simplify GDI programming
Philipp Kursawe <[email protected]> Tue, 22 Nov 2011 11:28:37 +0100
| Newsgroups | gmane.comp.windows.wtl |
|---|---|
| Message-ID | <CAD6pGjzSyc_joVLmfs3me-g1i==t4d77wv-BDz7+0A0ThssdFQ@mail.gmail.com> |
Thanks for sharing. I had something similar for my own projects, in an even more C++ templated way :) https://gist.github.com/1385377 On Mon, Nov 21, 2011 at 8:37 PM, Peter Kvitek <[email protected]> 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 > > > > [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/