Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to draw a line by dragging a mouse actually i want to know that how to code for mouse move while mouse left button down in MFC (VC++6.0)
Posted

Here is the code to draw a line by mouse dragging in MFC
I am not putting entire code if anyone wants please let me know.
Please use the logic from the below code.
//Mouse Move Event
void CMouseDlg::OnMouseMove(UINT nFlags, CPoint point)
{

if((nFlags & MK_LBUTTON)==MK_LBUTTON)
{
CClientDC dc(this);

if(m_bRedPen == 1) //flag if you select red pen
{
CPen rPen(PS_SOLID,2,RGB(255,0,0));
dc.SelectObject(&rPen);
dc.MoveTo(m_iPrevX,m_iPrevY);
dc.LineTo(point.x,point.y);
}

if(m_bGreenPen == 1)
{
CPen gPen(PS_SOLID,2,RGB(0,255,0));
dc.SelectObject(&gPen);
dc.MoveTo(m_iPrevX,m_iPrevY);
dc.LineTo(point.x,point.y);
}

if(m_bBluePen == 1) //flag if you select blue pen
{
CPen bPen(PS_SOLID,2,RGB(0,0,255));
dc.SelectObject(&bPen);
dc.MoveTo(m_iPrevX,m_iPrevY);
dc.LineTo(point.x,point.y);
}
if(m_iEraser == 1)
{
CPen erPen(PS_SOLID,10,RGB(255,255,255));
dc.SelectObject(&erPen);
dc.MoveTo(m_iPrevX,m_iPrevY);
dc.LineTo(point.x,point.y);
}

m_iPrevX=point.x;
m_iPrevY=point.y;
}

CDialog::OnMouseMove(nFlags, point);
}
//Mouse left button click capture the current co-ordinate
void CMouseDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
m_iPrevX=point.x;
m_iPrevY=point.y;

CDialog::OnLButtonDown(nFlags, point);
}
//Red Pen Function Definition
void CMouseDlg::OnPenRedpen()
{
m_bRedPen=1;
m_bBluePen=0;
m_bGreenPen=0;
m_iEraser = 0;
}
//Blue Pen Function Definition
void CMouseDlg::OnPenBluepen()
{
m_bRedPen=0;
m_bBluePen=1;
m_bGreenPen=0;
m_iEraser = 0;
}
//Green Pen Function Definition
void CMouseDlg::OnPenGreenpen()
{
m_bRedPen=0;
m_bBluePen=0;
m_bGreenPen=1;
m_iEraser = 0;
}
//Exit the Application
void CMouseDlg::OnFileExit()
{
OnCancel();
}
void CMouseDlg::OnDisable()
{
m_bRedPen=0;
m_bBluePen=0;
m_bGreenPen=0;
}

void CMouseDlg::OnEraser()
{
m_iEraser = 1;
m_bRedPen=0;
m_bBluePen=0;
m_bGreenPen=0;

}
 
Share this answer
 
Comments
Member 10063818 3-Jun-13 9:10am    
thank u for ur logic...if u provide full code it will more helpful..!!
Member 10584180 8-Apr-14 4:25am    
After drawing the line if i minimize the dialog all the drawing vanishes....can u help on this???
1. Check the section "SKETCH Application" in the following link,
http://www.cs.binghamton.edu/~reckert/360/9.html[^] OR

2. Download the book "http://www.geoviet.vn/Teaching/SW-Books/TeachYourselfVisualC++6In21Days.pdf" and check the example "Understanding the mouse events" in the chapter "Day 3 Allowing user interaction".This example is exactly what you want.
 
Share this answer
 
Comments
[no name] 15-Mar-11 1:36am    
Hey nice link
Thank you,I used these links when i started learning MFC
[no name] 15-Mar-11 4:56am    
so take my 5+
saqib.akhter 16-Mar-11 1:26am    
thanks!!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900