概述
源代码下载
多边形也就是由一些连续的直线组成的,所以绘图的代码还是前面的画直线的那两种算法,只不过在鼠标移动的过程中要特别注意记录终点的位置而已,代码中还是存在着bug,画多边形的时候出现了黄色的阴影线,真是奇怪的问题。为了判别是否是多边形的第一条边,加入了一个新变量:bool m_bIsFirstDone;//是否第一条边
void
CMyDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CString str;
CDC * pDC = GetDC();
// 状态栏显示坐标值
str.Format( " X:%d,Y:%d " ,point.x,point.y);
if ( ! m_pwndStatusBar)
m_pwndStatusBar = ((CMainFrame * )AfxGetMainWnd()) -> GetStatusBar();
m_pwndStatusBar -> SetPaneText( 2 ,str, 1 );
if (m_dsDrawSort == dsNULL)
return ;
switch (m_dsDrawSort)
{
case dsLine:
SetCursor(m_hDLCursor);
break ;
case dsCircle:
SetCursor(m_hDCCursor);
break ;
}
// 鼠标放开了
if ( ! m_bIsMoseDown)
return ;
pDC -> SetROP2(R2_NOTXORPEN);
switch (m_dsDrawSort)
{
case dsLine:
{ // 直线
Line * l1 = new Line(m_startPoint,m_endPoint);
l1 -> SetDrawLineSort(m_dlsDrawLineSort);
l1 -> SetPenColor(m_lPenColor);
Line * l2 = new Line(m_startPoint,point);
l2 -> SetDrawLineSort(m_dlsDrawLineSort);
l2 -> SetPenColor(m_lPenColor);
l1 -> Draw(pDC);
l2 -> Draw(pDC);
break ;
}
case dsCircle:
{ // 圆
Line * l1 = new Line(m_startPoint,m_endPoint);
l1 -> SetDrawLineSort(m_dlsDrawLineSort);
l1 -> SetPenColor(m_lPenColor);
Line * l2 = new Line(m_startPoint,point);
l2 -> SetDrawLineSort(m_dlsDrawLineSort);
l2 -> SetPenColor(m_lPenColor);
l1 -> Draw(pDC);
l2 -> Draw(pDC);
Circle * c1 = new Circle(m_startPoint,Distance(m_startPoint,m_endPoint));
c1 -> SetPenColor(m_lPenColor);
c1 -> Draw(pDC);
Circle * c2 = new Circle(m_startPoint,Distance(m_startPoint,point));
c2 -> SetPenColor(m_lPenColor);
c2 -> Draw(pDC);
break ;
}
case dsPolyGon:
{ // 折线
Line * l1 = new Line(m_startPoint,m_endPoint);
l1 -> SetDrawLineSort(m_dlsDrawLineSort);
l1 -> SetPenColor(m_lPenColor);
Line * l2 = new Line(m_startPoint,point);
l2 -> SetDrawLineSort(m_dlsDrawLineSort);
l2 -> SetPenColor(m_lPenColor);
l1 -> Draw(pDC);
l2 -> Draw(pDC);
break ;
}
}
m_endPoint = point; // 记录下终点,时刻刷新最后的点
ReleaseDC(pDC);
CView::OnMouseMove(nFlags, point);
}
{
// TODO: Add your message handler code here and/or call default
CString str;
CDC * pDC = GetDC();
// 状态栏显示坐标值
str.Format( " X:%d,Y:%d " ,point.x,point.y);
if ( ! m_pwndStatusBar)
m_pwndStatusBar = ((CMainFrame * )AfxGetMainWnd()) -> GetStatusBar();
m_pwndStatusBar -> SetPaneText( 2 ,str, 1 );
if (m_dsDrawSort == dsNULL)
return ;
switch (m_dsDrawSort)
{
case dsLine:
SetCursor(m_hDLCursor);
break ;
case dsCircle:
SetCursor(m_hDCCursor);
break ;
}
// 鼠标放开了
if ( ! m_bIsMoseDown)
return ;
pDC -> SetROP2(R2_NOTXORPEN);
switch (m_dsDrawSort)
{
case dsLine:
{ // 直线
Line * l1 = new Line(m_startPoint,m_endPoint);
l1 -> SetDrawLineSort(m_dlsDrawLineSort);
l1 -> SetPenColor(m_lPenColor);
Line * l2 = new Line(m_startPoint,point);
l2 -> SetDrawLineSort(m_dlsDrawLineSort);
l2 -> SetPenColor(m_lPenColor);
l1 -> Draw(pDC);
l2 -> Draw(pDC);
break ;
}
case dsCircle:
{ // 圆
Line * l1 = new Line(m_startPoint,m_endPoint);
l1 -> SetDrawLineSort(m_dlsDrawLineSort);
l1 -> SetPenColor(m_lPenColor);
Line * l2 = new Line(m_startPoint,point);
l2 -> SetDrawLineSort(m_dlsDrawLineSort);
l2 -> SetPenColor(m_lPenColor);
l1 -> Draw(pDC);
l2 -> Draw(pDC);
Circle * c1 = new Circle(m_startPoint,Distance(m_startPoint,m_endPoint));
c1 -> SetPenColor(m_lPenColor);
c1 -> Draw(pDC);
Circle * c2 = new Circle(m_startPoint,Distance(m_startPoint,point));
c2 -> SetPenColor(m_lPenColor);
c2 -> Draw(pDC);
break ;
}
case dsPolyGon:
{ // 折线
Line * l1 = new Line(m_startPoint,m_endPoint);
l1 -> SetDrawLineSort(m_dlsDrawLineSort);
l1 -> SetPenColor(m_lPenColor);
Line * l2 = new Line(m_startPoint,point);
l2 -> SetDrawLineSort(m_dlsDrawLineSort);
l2 -> SetPenColor(m_lPenColor);
l1 -> Draw(pDC);
l2 -> Draw(pDC);
break ;
}
}
m_endPoint = point; // 记录下终点,时刻刷新最后的点
ReleaseDC(pDC);
CView::OnMouseMove(nFlags, point);
}
void
CMyDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_bIsMoseDown = FALSE; // 鼠标未按下
if (m_dsDrawSort == dsNULL)
return ;
CDC * pDC = GetDC();
Line lTemp;
switch (m_dsDrawSort)
{
case dsLine:
case dsPolyGon:
{ // 画直线或折线
Line * pL1 = new Line(m_startPoint,point);
pL1 -> SetDrawLineSort(m_dlsDrawLineSort);
pL1 -> SetPenColor(m_lPenColor);
pL1 -> Draw(pDC);
lTemp.SetStartPoint(m_startPoint); // 设置起点
lTemp.SetEndPoint(point); // 设置终点
lTemp.SetDrawLineSort(m_dlsDrawLineSort); // 设置画直线方式
lTemp.SetPenColor(m_lPenColor); // 设置画笔颜色
m_listAllLines.push_back(lTemp); // 保存此直线
if (m_dsDrawSort == dsPolyGon)
{
m_startPoint = point; // 把鼠标up的那一点作为新的起点
if ( ! m_bIsFirstDone)
{ // 第一条边完成
m_bIsFirstDone = TRUE;
}
}
SetCursor(m_hDLCursor);
break ;
}
case dsCircle:
{ // 画圆
Circle cTemp;
double radius = Distance(m_startPoint,point); // 计算半径大小
Circle * pC1 = new Circle(m_startPoint,radius);
pC1 -> SetPenColor(m_lPenColor);
pC1 -> Draw(pDC);
cTemp.SetRadis(radius); // 设置半径
cTemp.SetMidPoint(m_startPoint); // 设置圆心
cTemp.SetPenColor(m_lPenColor); // 设置画笔颜色
m_listAllCircles.push_back(cTemp); // 保存此圆
pDC -> SetROP2(R2_NOTXORPEN); // Pixel is the inverse of the R2_XORPEN color (final pixel = NOT(pen XOR screen pixel)).
Line * pL1 = new Line(m_startPoint,point);
pL1 -> SetPenColor(m_lPenColor);
pL1 -> Draw(pDC);
SetCursor(m_hDCCursor);
break ;
}
}
ReleaseDC(pDC);
CView::OnLButtonUp(nFlags, point);
}
{
// TODO: Add your message handler code here and/or call default
m_bIsMoseDown = FALSE; // 鼠标未按下
if (m_dsDrawSort == dsNULL)
return ;
CDC * pDC = GetDC();
Line lTemp;
switch (m_dsDrawSort)
{
case dsLine:
case dsPolyGon:
{ // 画直线或折线
Line * pL1 = new Line(m_startPoint,point);
pL1 -> SetDrawLineSort(m_dlsDrawLineSort);
pL1 -> SetPenColor(m_lPenColor);
pL1 -> Draw(pDC);
lTemp.SetStartPoint(m_startPoint); // 设置起点
lTemp.SetEndPoint(point); // 设置终点
lTemp.SetDrawLineSort(m_dlsDrawLineSort); // 设置画直线方式
lTemp.SetPenColor(m_lPenColor); // 设置画笔颜色
m_listAllLines.push_back(lTemp); // 保存此直线
if (m_dsDrawSort == dsPolyGon)
{
m_startPoint = point; // 把鼠标up的那一点作为新的起点
if ( ! m_bIsFirstDone)
{ // 第一条边完成
m_bIsFirstDone = TRUE;
}
}
SetCursor(m_hDLCursor);
break ;
}
case dsCircle:
{ // 画圆
Circle cTemp;
double radius = Distance(m_startPoint,point); // 计算半径大小
Circle * pC1 = new Circle(m_startPoint,radius);
pC1 -> SetPenColor(m_lPenColor);
pC1 -> Draw(pDC);
cTemp.SetRadis(radius); // 设置半径
cTemp.SetMidPoint(m_startPoint); // 设置圆心
cTemp.SetPenColor(m_lPenColor); // 设置画笔颜色
m_listAllCircles.push_back(cTemp); // 保存此圆
pDC -> SetROP2(R2_NOTXORPEN); // Pixel is the inverse of the R2_XORPEN color (final pixel = NOT(pen XOR screen pixel)).
Line * pL1 = new Line(m_startPoint,point);
pL1 -> SetPenColor(m_lPenColor);
pL1 -> Draw(pDC);
SetCursor(m_hDCCursor);
break ;
}
}
ReleaseDC(pDC);
CView::OnLButtonUp(nFlags, point);
}
void
CMyDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_bIsMoseDown = TRUE; // 鼠标按下了
if (m_dsDrawSort == dsNULL) // 画图类型未选择
return ;
CDC * pDC = GetDC();
pDC -> SetROP2(R2_NOTXORPEN);
Line lTemp;
if (m_dsDrawSort == dsPolyGon && m_bIsFirstDone)
{
Line * pL1 = new Line(m_startPoint,point);
pL1 -> Draw(pDC);
lTemp.SetStartPoint(m_startPoint);
lTemp.SetEndPoint(point);
m_listAllLines.push_back(lTemp);
}
{
// TODO: Add your message handler code here and/or call default
m_bIsMoseDown = TRUE; // 鼠标按下了
if (m_dsDrawSort == dsNULL) // 画图类型未选择
return ;
CDC * pDC = GetDC();
pDC -> SetROP2(R2_NOTXORPEN);
Line lTemp;
if (m_dsDrawSort == dsPolyGon && m_bIsFirstDone)
{
Line * pL1 = new Line(m_startPoint,point);
pL1 -> Draw(pDC);
lTemp.SetStartPoint(m_startPoint);
lTemp.SetEndPoint(point);
m_listAllLines.push_back(lTemp);
}
最后
以上就是忐忑鲜花为你收集整理的画图板---画多边形代码的全部内容,希望文章能够帮你解决画图板---画多边形代码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复