|
Windows API TutorialsIt is enough to attach them to your canvas for the time you want to use them. class WhitePen : public StockObject{ public: WhitePen (HDC hdc): StockObject (hdc, WHITE_PEN) {} }; // example void Controller::Paint (HWND hwnd) { PaintCanvas canvas (hwnd); WhitePen pen (canvas); canvas.Line (0, 10, 100, 10); // destructor of WhitePen // destructor of PaintCanvas } If your program keeps using a small set of non-stock pens, you might want to create them up-front (e.g. by embedding them in the View object) and use a PenHolder object to temporarily attach them to your Canvas. class Pen { public: Pen (COLORREF color) { _hPen = CreatePen (PS_SOLID, 0, color); } ~Pen () { DeleteObject (_hPen); } operator HPEN () { return _hPen; } private: HPEN _hPen; }; class PenHolder { public: PenHolder (HDC hdc, HPEN hPen) : _hdc (hdc) { _hPenOld = (HPEN)SelectObject (_hdc, hPen); } ~PenHolder () { SelectObject (_hdc, _hPenOld); } private: HDC _hdc; HPEN _hPenOld; }; class View { public: View () : _penGreen (RGB (0, 255, 128)) {} void Paint (Canvas & canvas) { PenHolder holder (canvas, _penGreen); canvas.Line (0, 10, 100, 10); // destructor of PenHolder } private: Pen _penGreen; }; Finally, if your program needs colored pens on-demand, i.e., you can't possibly preallocate them for all the colors you'll need, you should use colored pens ...» |
Код для вставки книги в блог HTML
phpBB
текст
|
|