在MFC中通过鼠标拖动绘制一条线 [英] Drawing a Line by mouse drag in MFC

查看:94
本文介绍了在MFC中通过鼠标拖动绘制一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上必须通过拖动鼠标来画一条线,我想知道如何在MFC(VC ++ 6.0)中按下鼠标左键的同时进行鼠标移动的编码.

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)

推荐答案

1.检查以下链接中的"SKETCH应用程序"部分,
http://www.cs.binghamton.edu/~reckert/360/9.html [< ^ ] OR

2.下载书籍"http://www.geoviet.vn/Teaching/SW-Books/TeachYourselfVisualC++6In21Days.pdf",并在第3天允许用户交互"一章中检查示例了解鼠标事件".此示例正是您想要的.
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.


以下是通过在MFC中拖动鼠标来绘制线条的代码
如果有人要,请不要告诉我整个代码.
请使用以下代码中的逻辑.
//鼠标移动事件
void CMouseDlg :: OnMouseMove(UINT nFlags,CPoint点)
{

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

if(m_bRedPen == 1)//如果选择红笔则标记
{
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)//如果选择蓝色笔则标记
{
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);
}
//鼠标左键单击捕获当前坐标
void CMouseDlg :: OnLButtonDown(UINT nFlags,CPoint点)
{
m_iPrevX = point.x;
m_iPrevY = point.y;

CDialog :: OnLButtonDown(nFlags,point);
}
//红笔功能定义
void CMouseDlg :: OnPenRedpen()
{
m_bRedPen = 1;
m_bBluePen = 0;
m_bGreenPen = 0;
m_iEraser = 0;
}
//蓝笔功能定义
void CMouseDlg :: OnPenBluepen()
{
m_bRedPen = 0;
m_bBluePen = 1;
m_bGreenPen = 0;
m_iEraser = 0;
}
//绿笔功能定义
void CMouseDlg :: OnPenGreenpen()
{
m_bRedPen = 0;
m_bBluePen = 0;
m_bGreenPen = 1;
m_iEraser = 0;
}
//退出应用程序
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;

}
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;

}


这篇关于在MFC中通过鼠标拖动绘制一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆