Drawing Simple Lines and Shapes (C++ 4.0 and later)

Take the following steps to add code that lets you draw a line, rectangle and ellipse on the bitmap.

1. Start with the project that you created in Loading and Displaying an Image.

2. Edit TUTORDLG.H and insert the following lines in the definition of CTutorDlg after DECLARE_MESSAGE_MAP():

int DrawObject; // The object we are drawing
long StartX; // Starting X position
long StartY; // Starting Y position
long EndX; // Ending X position
long EndY; // Ending Y position
BOOL Drawing;

3. Edit the OnLoadlead() function to add the following code to initialize the form-level variables when you load an image:

DrawObject = 0;
Drawing = FALSE;

4. Code the LEAD control's MouseDown event as follows. This code selects a different drawing object each time the event occurs.

void CDrawtestDlg::OnMouseDownLead1(short Button, short Shift, long x, long y) 
{
  //temp. Disable Scrolling
  m_Lead1.SetEnableKeyboard(FALSE);
  m_Lead1.SetEnableScroll(TRUE);
  Drawing = TRUE;
  // Save the starting position.
  StartX = x;
  StartY = y;
  EndX = x;
  EndY = y;

  // Cycle through the types of drawing objects.
  switch (DrawObject)
  {
    case 0:
      DrawObject = 1;   // Line
      break;
    case 1:
      DrawObject = 2;   // Rectangle
      break;
    case 2:
      DrawObject = 0;   // Ellipse
      break;
  }
}

5. Code the LEAD control's MouseMove event as follows. This code uses DRAWMODE_INVERT for the DrawMode, which means that pixel colors are inverted. Thus, the drawing methods can erase the previous object and draw a new one.

void CDrawtestDlg::OnMouseMoveLead1(short Button, short Shift, long x, long y) 
{
  int OldEndX, OldEndY;
  long OldDrawX, OldDrawY, OldWidth, OldHeight;
  long DrawX, DrawY, NewWidth, NewHeight;

  if (Drawing == TRUE)
  {
    // Set the drawing styles.
    m_Lead1.SetDrawPenStyle(DRAWPENSTYLE_SOLID);
    m_Lead1.SetDrawMode(DRAWMODE_INVERT);
    m_Lead1.SetDrawFillStyle(DRAWFILLSTYLE_TRANSPARENT);
    m_Lead1.SetDrawPersistence(FALSE); // On the window, not the bitmap

    // Save the previous ending mouse position.
    OldEndX = EndX;
    OldEndY = EndY;

    // Get the current mouse position.
    EndX = x;
    EndY = y;

    // Calculate the origin of the current object.
    if (EndX > StartX)
       DrawX = StartX;
    else
       DrawX = EndX;
    if (EndY > StartY)
       DrawY = StartY;
    else
       DrawY = EndY;

    // Calculate the origin of the previous object.
    if (OldEndX > StartX)
       OldDrawX = StartX;
    else
       OldDrawX = OldEndX;
    if (OldEndY > StartY)
       OldDrawY = StartY;
    else
       OldDrawY = OldEndY;

    // Calculate the height and width of the current object.
    NewHeight = abs(StartY - EndY);
    NewWidth = abs(StartX - EndX);

    // Calculate the height and width of the previous object.
    OldHeight = abs(StartY - OldEndY);
    OldWidth = abs(StartX - OldEndX);

    // Erase the old object and draw the new one.
    switch (DrawObject)
    {
       case 0:   // Ellipse
         m_Lead1.DrawEllipse(OldDrawX, OldDrawY, OldWidth, OldHeight);
         m_Lead1.DrawEllipse(DrawX, DrawY, NewWidth, NewHeight);
         break;
       case 1:   // Line
         m_Lead1.DrawLine(StartX, StartY, OldEndX, OldEndY);
         m_Lead1.DrawLine(StartX, StartY, EndX, EndY);
         break;
       case 2:   // Rectangle
         m_Lead1.DrawRectangle(OldDrawX, OldDrawY, OldWidth, OldHeight);
         m_Lead1.DrawRectangle(DrawX, DrawY, NewWidth, NewHeight);
         break;
    }
  }
}

6. Code the LEAD control's MouseUp event as follows. This code sets the drawing style and draws the object on the bitmap.

void CDrawtestDlg::OnMouseUpLead1(short Button, short Shift, long x, long y) 
{
  long DrawX, DrawY, NewWidth, NewHeight;

  //Restore scrolling
  m_Lead1.SetEnableKeyboard(TRUE);
  m_Lead1.SetEnableScroll(TRUE);
  Drawing = FALSE;

  // Set the drawing style.
  m_Lead1.SetDrawPenStyle(DRAWPENSTYLE_SOLID);
  m_Lead1.SetDrawPenWidth(2);
  m_Lead1.SetDrawPenColor(RGB(255, 0, 0));    // Red
  m_Lead1.SetDrawMode(DRAWMODE_COPY_PEN);
  m_Lead1.SetDrawFillColor(RGB(0, 255, 0));   // Green
  m_Lead1.SetDrawFillStyle(DRAWFILLSTYLE_HORIZONTAL_LINE);
  m_Lead1.SetDrawPersistence(TRUE);           // On the bitmap

  // Get the current mouse position
  EndX = x;
  EndY = y;

  // Determine the origin of the object.
  if (EndX > StartX)
   DrawX = StartX;
  else
   DrawX = EndX;
  if (EndY > StartY)
   DrawY = StartY;
  else
   DrawY = EndY;

  // Determine the height and width of the object.
  NewHeight = abs(StartY - EndY);
  NewWidth = abs(StartX - EndX);

  // Draw the object
  switch (DrawObject)
  {
   case 0:   // Ellipse
     m_Lead1.DrawEllipse(DrawX, DrawY, NewWidth, NewHeight);
     break;
   case 1:   // Line
     m_Lead1.DrawLine(StartX, StartY, EndX, EndY);
     break;
   case 2:   // Rectangle
     m_Lead1.DrawRectangle(DrawX, DrawY, NewWidth, NewHeight);
     break;
  }
}

7. Run your program to test it.