Tutorials

 

Home
About
How it Works
Documentation
Tutorials

Examples

Links
Contact

 

 

Menu of tutorials

Tutorial 1:   The Simplest Window
Tutorial 2:   Using Classes and Inheritance
Tutorial 3:   Using Messages to Create a Scribble Window
Tutorial 4:   Repainting the Window
Tutorial 5:   Wrapping a Frame around our Scribble Window
Tutorial 6:   Customising Window Creation
Tutorial 7:   Customising the ToolBar
Tutorial 8:   Loading and Saving Files
Tutorial 9:   Printing
Tutorial 10: Print Previewing
Tutorial 11: Finishing Touches

Tutorial 4:  Repainting the Window

The previous example fails to repaint the window. Repainting the window is required whenever the window is resized, restored from minimized, or when part of the window is revealed after being covered by another window.

The Windows API handles repainting automatically. When part of a window needs repainting, windows sends a WM_PAINT message to the application. Typically you would respond to this message with the code to redraw the entire client area of the window. You don't need to concern yourself with which parts of the client area need to be redrawn, as windows handles this part for you automatically.

Win32++ already contains the code to handle the WM_PAINT message in CWnd::WndProc. All we need to do is write the OnDraw function.  For our application here, we can store the various points in a vector, and have the OnDraw function draw the lines again.  The function to store the points looks like this.

void CView::StorePoint(int x, int y, bool penDown)
{
  PlotPoint p1;
  p1.x = x;
  p1.y = y;
  p1.PenDown = penDown;

  m_points.push_back(p1); //Add the point to the vector
}

Our OnDraw function looks like this.

void CView::OnDraw(CDC& dc)
{
  if (m_points.size() > 0)
  {
    bool draw = false;  //Start with the pen up

    for (unsigned int i = 0 ; i < m_points.size(); i++)
    {
      if (draw) 
        dc.LineTo(m_points[i].x, m_points[i].y);
      else
        dc.MoveTo(m_points[i].x, m_points[i].y);

      draw = m_points[i].PenDown;
    }	
  }
}

The source code for this tutorial is located within the Tutorial folder of the software available from SourceForge at http://sourceforge.net/projects/win32-framework.