[Contents] [Prev] [Next]

Step 4: Drawing in the window

You can find the source for Step 4 in the file STEP04.CPP in the directory EXAMPLES\OWL\TUTORIAL. In this step, you'll add the ability to draw a line in the window by pressing the left mouse button and dragging. To do this, you'll add a two new events, WM_MOUSEMOVE and WM_LBUTTONUP, to the TDrawWindow response table, along with functions to handle those events. You'll also add a TClientDC * to the class.

Adding new events

To let the user draw on the window, the application must handle a number of events:

You need to add two macros to the window class' response table, EV_WM_MOUSEMOVE and EV_WM_LBUTTONUP. The new response table should look something like this:

You also need to add the EvLButtonUp and EvMouseMove functions to the TDrawWindow class.

Adding a TClientDC pointer

The scheme used in Step 3 to draw a line isn't very robust:

You can take care of both of these problems rather easily by adding a new protected data member to TDrawWindow. This data member is a TDC * called DragDC. It works this way:

Initializing DragDC

You must make sure that DragDC is set to 0 when you construct the TDrawWindow object:

TDrawWindow::TDrawWindow(TWindow  *parent)
    Init(parent,  0,  0);
    DragDC  =  0;

Cleaning up after DragDC

Because DragDC is a pointer to a TClientDC object, and not an actual TClientDC object, it isn't automatically destroyed when the TDrawWindow object is destroyed. You need to add a destructor to TDrawWindow to properly clean up. The only thing required is to call delete on DragDC. TDrawWindow should now look something like this:

class  TDrawWindow  :  public  TWindow
        TDrawWindow(TWindow  *parent  =  0);
~TDrawWindow()  {delete  DragDC;}

        TDC  *DragDC;

        //  Override  member  function  of  TWindow
        bool  CanClose();

        //  Message  response  functions
        void  EvLButtonDown(uint,  TPoint&);
        void  EvRButtonDown(uint,  TPoint&);
        void  EvMouseMove(uint,  TPoint&);
        void  EvLButtonUp(uint,  TPoint&);

Note that, because the tutorial application has now become somewhat useful, the name of the main window has been changed from "Sample ObjectWindows Program" to "Drawing Pad":

SetMainWindow(new  TFrameWindow(0,  "Drawing  Pad",  new  TDrawWindow));

Where to find more information

Here's a guide to where you can find more information on the topics introduced in this step:

[Contents] [Prev] [Next]