Click here to Skip to main content
12,757,424 members (39,860 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 14 Jan 2007

Dynamic Line Drawing Activex Control

, 15 Jan 2007
Rate this:
Please Sign up or sign in to vote.
Activex Control for Line Drawing at runtime

Sample Image - TestLine_Activex.gif


For simple ActiveX Control’s example, I have seen, provides design time drawing of various shapes. In this example I tried to do in a very simple way to create a Line at runtime when the ActiveX control’s class is used, i.e. drawing a line in your application at runtime like it happens in MS Word using drawing tool. You can resize the line and move the line. A simple example for using this ActiveX is also attached herewith. This example can be extended to any shapes.


When I have to create simple lines on the graph for various calculation of curves thought to create a simple line drawing ActiveX control which would help the user to draw the line at run time and get the results based on the line and curve intersections, projection etc.

Using the Code:

Download the ActiveX demo and compile the TestLine.dsw in VC 6.0 it will create the TestLine ActiveX control and then compile the TestDlg.dsw in VC 6.0 to see the demo of this ActiveX controls working. Click on the “Line” button to press the left button and move the mouse as you like and release the same a TestLine Control is created base on this operation. Double clicking on the line will provide option to resize of move the line.

Source Code Description

When the mouse is double clicked over the line, then:

void CTestLineCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
 // node_points variable is set so that nodal points can be drawn
 // for resizing or moving operation
 if (!node_points)
     node_points = true;

If the left mouse button is pressed on the middle of the line then moved the line is moved else if the left mouse button pressed on any of the ends of the line then the line is redrawn till the point where the mouse is released.

void CTestLineCtrl::OnLButtonDown(UINT nFlags, CPoint point)
 if (node_points)
     // moving the control....!
     if (nodeRectMid.PtInRect(point))
       moveLine = true;
       //save current cursor coordinate
       mPoint1 = point;
       ClientToScreen(&mPoint1); //pMsg->pt;
       mPoint2 = mPoint1;
     } else {// re-drawing the line..
         if (nodeRect1.PtInRect(point))
           //node_points = false;
           lPoint1 = lPoint2;
           lPoint2 = point;
           drawLine = true;
         } else if (nodeRect2.PtInRect(point)) {
           //node_points = false;
           drawLine = true;

If drawLine variable is set then on Mouse move draw a line on the parent by removing the old once the mouse is release move the control to the new position and erase the line.

If moveLine variable is set then move the line to the new position.

CTestLineCtrl::OnMouseMove(UINT nFlags, CPoint point)
 if (drawLine)
     CWnd* prnt = GetParent();
     // Draw the dotted line..! on the parent...!
     CPoint p1, p2, p3;
     // Get the first point and
     // then get the new point
     p1 = lPoint1;
     p2 = lPoint3;
     p3 = point;
     // store this point in terms
     // of screen for next use..!
     lPoint3 = p3;

     // Make the point for the parent.
     if (prnt)
     // Get the device context of the parent
     CDC *pdc = prnt->GetDC();
     // Redraw the old region
     int xEpsilon = 0;
     int yEpsilon = 0;
     CPoint pts[5];
     if (p2.x > p1.x )
      xEpsilon = -2;
     } else {
      xEpsilon = 2;
     if (p1.y > p1.y)
      yEpsilon = -2;
     } else {
      yEpsilon = 2;

 pts[0].x = p1.x + xEpsilon;
 pts[0].y = p1.y ;

 pts[1].x = p1.x + xEpsilon;
 pts[1].y = p1.y + yEpsilon;

 pts[2].x = p2.x - xEpsilon;
 pts[2].y = p2.y + yEpsilon;

 pts[3].x = p2.x - xEpsilon;
 pts[3].y = p2.y ;

 pts[4].x = pts[0].x;
 pts[4].y = pts[0].y;

 CRgn *rgn;
 rgn = new CRgn();
 rgn->CreatePolygonRgn(pts, 5, WINDING);

 // Draw the line..!

 CPen *myPen = new CPen(PS_DASH, 1, RGB(0, 0, 0));
 CPen* OldPen = pdc->SelectObject(myPen);

 pdc->MoveTo(p1.x, p1.y);
 pdc->LineTo(p3.x, p3.y);

 delete rgn;
 delete myPen;

  CWnd* prnt = GetParent();
  CPoint temp;
  temp.x = MainRect.left ;
  temp.y = ;

  if (prnt)

  int chX = (mPoint2.x - mPoint1.x);
  int chY = (mPoint2.y - mPoint1.y);

  mPoint1 = mPoint2;
  mPoint2 = point;
  ClientToScreen(&mPoint2); //pMsg->pt;

  MoveWindow(temp.x + chX,
  //count the relative position
  temp.y + chY,
  //if the width doesn’t change
  //if the height doesn’t change

Once the left button is release set the drawLine or moveLine variable to false if they were previously set and set the region of Control window to that of line.

// // Now the rect is found change the window position and size<BR>// // Set the window<BR>// MoveWindow(temp.x,<BR>// temp.y,<BR>// w,<BR>// h,<BR>// FALSE);<BR>//<BR>// ...<BR>//<BR>// SetTheLineRgn();<BR>

Draw the line and node_point At runtime onDraw will not be called onPaint will be called for drawing on the control.

// dc.MoveTo(lPoint1.x, lPoint1.y);<BR>// dc.LineTo(lPoint2.x, lPoint2.y);<BR>//<BR>//<BR>// if (node_points)<BR>// {<BR>// if ((lPoint1.x != lPoint2.x) || (lPoint1.y != lPoint2.y))<BR>// {<BR>//<BR>// CalculateNodeAreas();<BR>// dc.Rectangle(nodeRect1);<BR>// dc.Rectangle(nodeRectMid);<BR>// dc.Rectangle(nodeRect2);<BR>// }<BR>// 

Sample application to use the code: TestDlg:<BR>
1. Create a Button called Line: On clicking on the button set the drawLine variable to true:

<BR>//OnDrawline()<BR>//{<BR>// // preparing to draw the line<BR>// <code>drawLine = true;</code><BR>//} 

2. On left button down record the first point

//void CTestDlg::OnLButtonDown(UINT nFlags, CPoint point)<BR>//{<BR>// // Record the first point.<BR>// if (drawLine)<BR>// {<BR>// <code>p1 = point;</code><BR>// }<BR>//...<BR>//}

3. On mouse move by pressing the left button record the second point.

//void CTestDlg::OnMouseMove(UINT nFlags, CPoint point)<BR>//{<BR>// if (drawLine && nFlags == MK_LBUTTON)<BR>// {<BR>// <code>p2 = point;</code><BR>// Invalidate();<BR>// }<BR>//<BR>//...<BR>//}

4 Now create the control with those to specified point

//void CTestDlg::OnLButtonUp(UINT nFlags, CPoint point)<BR>//{<BR>// if (drawLine)<BR>// {<BR>// drawLine = false;<BR>// p2 = point;<BR>// if (lineIndex < MAX_NO_OF_LINE)<BR>// {<BR>// myLine[lineIndex] = new CTestLine();<BR>// CTestLine* tLine = myLine[lineIndex];<BR>// lineIndex++;<BR>//<BR>// int x1, x2, y1, y2;<BR>// CRect rect;<BR>// if (p2.x > p1.x)<BR>// {<BR>// rect.left = p1.x;<BR>// rect.right = p2.x;<BR>// x1 = 0;<BR>// x2 = p2.x - p1.x;<BR>// } else {<BR>// rect.left = p2.x;<BR>// rect.right = p1.x;<BR>// x1 = 0;<BR>// x2 = p1.x - p2.x;<BR>// }<BR>//<BR>// if (p2.y > p1.y)<BR>// {<BR>// = p1.y;<BR>// rect.bottom = p2.y;<BR>// y1 = 0;<BR>// y2 = p2.y - p1.y;<BR>// } else {<BR>// = p2.y;<BR>// rect.bottom = p1.y;<BR>// y1 = 0;<BR>// y2 = p1.y - p2.y;<BR>// }<BR>//<BR>// tLine->Create("testLine", WS_CHILD | WS_VISIBLE, rect, this, 10, NULL);<BR>// tLine->ShowWindow(SW_SHOWNORMAL);<BR>// ClientToScreen(&p1);<BR>// ClientToScreen(&p2);<BR>// tLine->ScreenToClient(&p1);<BR>// tLine->ScreenToClient(&p2);<BR>// tLine->SetPoint1(p1.x, p1.y);<BR>// tLine->SetPoint2(p2.x, p2.y);<BR>// tLine->Invalidate();<BR>//<BR>// } else {<BR>// MessageBox("Unable to draw Line. Increase the No of Line!");<BR>// }<BR>//<BR>// Invalidate();<BR>//<BR>// }<BR>// CDialog::OnLButtonUp(nFlags, point);<BR>//}

Most important thing I learnt that understating the user operation and creating the combined region and setting the same for that line control’s window. This is my quick try to create the control as simple way as it can be. It might encounter to some bugs as it is not tested in all the environments. Most important thing I learnt that understating the user operation and creating the combined region and setting the same for that line control’s window.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
India India
No Biography provided

You may also be interested in...


Comments and Discussions

Questionthe zipped folders can not download Pin
Jeffrey_Martin4-Aug-12 7:26
memberJeffrey_Martin4-Aug-12 7:26 
GeneralMy vote of 5 Pin
manoj kumar choubey20-Feb-12 20:54
membermanoj kumar choubey20-Feb-12 20:54 
Generalerror executing the demo project Pin
xupi12-Jul-07 2:32
memberxupi12-Jul-07 2:32 
Generalno files to download Pin
Gouda Sharp17-May-07 0:59
memberGouda Sharp17-May-07 0:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 15 Jan 2007
Article Copyright 2007 by SandeepBera
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid