(or why did I write this?)
I just got tired of having to do all the silly GDI+ initialization/deinitialization and double-buffering stuff, every time I wanted to use it. Pain in the behindment. So this class basically takes all that work away. All you do is implement a
Draw function that takes a
Graphics object, and the class does all the other GDI+ related stuff.
Ok. To use this puppy, all you've got to do is this:
- Derive your class from
CGdiPlusHelper, and (unless you've got some weird special needs), leave the
tUseDoubleBuffering template argument alone. Double buffering REALLY speeds up the drawing speed, and helps eliminate flicker.
- Do NOT implement the
WM_PAINT message. Instead, implement a function with the following prototype:
void Draw(Graphics* g, int nWidth, int nHeight);
This will be your main drawing function. You can use the passed in
Graphics object to do all your drawing.
CGdiPlusHelper class, preferably as far in front of your
MSG_MAP as possible. Oh, if you want to force a redraw on the window, call
SetDirty and only then
Invalidate. Otherwise, in double-buffered mode, it'll just reuse the same cached bitmap as before.
Note that the header file will automatically link in GdiPlus.lib, include GdiPlus.h, and
using namespace GdiPlus;
it. i.e., You're all set to go to use GDI+ by just including this file.
You need to install WTL7 and Microsoft Platform SDK to get WTL (although I THINK you only need it for the demo.) and GDI+.
- 5/23/2002: Original article submission
Thanks to Alex Farber and his great article, which really contains all the
CachedBitmap etc. stuff I used.
I also loosely based the drawing algorithm in the demo on Mazdak's article.
Speaking of Copyright
(you knew it was coming...)
The code is completely, utterly, and absolutely free. Feel free to use it in private or commercial applications, modify it, sit on it, or print it out and use it for toilet paper. God knows I've ignored enough copyrights from CodeProject to have the nerve to make my own. One thing though -- if this code messes up your computer or puts your project seven months behind schedule, it's not my fault. None of it is my fault. Not even that "format c:" somewhere in there. Er, just kidding. Oh yeah, needless to say, you can't claim this code as your own. (i.e., I'd hate for someone to reprint this code, and copyright the hell out of it. Not sure if that's possible anyhow, but it would suck. Greatly.)
P.S. Probably a pointless thing to say, but if you're going to rate the article badly, at least take the time to write a comment saying how I could improve it, or what you find inadequate about it...