Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

Creating a painter

Implementing an own painter

One of the most important aspects of the background_wnd is the separation of the actual drawing from the window-logic. background_wnd takes care of managing a memory-dc, Z-Order, drawing-rect, transparency, visibility of painters and so on. You only have to concentrate on creating a painter. This is very easy:

To understand the basic concept and the implementation details I recommend that you study layout_painter or stretch_painter because these two classes show you some nice techniques that are possible with the painter-concept ;-)

Aggregating painters

Of course you can aggregate and use other painters in your draw()-function. stretch_painter does exactly that:
void stretch_painter::draw(HDC hDC, int cx, int cy)
{
     ...
     transparent_painter p(cr_not_used_);
     p.set_painter<stretch_painter_helper> (hPainterDC, wnd_size(cx, cy));
     p.draw(hDC, rc_stretch_.width(), rc_stretch_.height());          
     ...
}

But that is not all: I included a class which holds another painter - painter_holder. You can derive your painter from painter_holder if you want to manipulate the appearance of a painter. Good examples are transparent_painter and stretch_painter: the former draws another painter but with one color transparent while the latter draws the painter stretched.

Here's the definition of painter_holder:
namespace win32 { namespace gui {
     
     // painter_holder
     struct painter_holder
     { 
          // exceptions
          typedef bg_wnd::general_exception<painter_holder, 0> no_painter;
          typedef bg_wnd::general_exception<painter_holder, 1> invalid_painter_cast;
               
     
          // the big 4
          painter_holder(painter_base* pPainter = NULL);
          painter_holder(const painter_holder& holder);
          virtual ~painter_holder();                   
          const painter_holder& operator= (const painter_holder& holder);
     
          // set the painter and release it  
          void release_painter();
          void set_painter(const painter_base& painter);
          template<class Painter> void set_painter();
          template<class Painter> void set_painter(typename Painter::param0 param);
          template<class Painter> void set_painter(typename Painter::param0 param0, typename Painter::param1 param1);   
     
          // painter access
          painter_base& get_painter() const;
          template<class Painter> Painter& get_painter() const;           
          bool painter_available() const;
          
          void swap(painter_holder& holder);
     };  
     
} }  // namespace win32::gui

Note:



by Steven Weiss. You can contact me at steven11@gmx.de.