我想在客户端区域使用鼠标进行4次点击。完成第4次点击后,应连接4个点以形成形状。我使用以下代码完成此操作。(我使用的是MFC)。
void CChildView::OnLButtonDown(UINT nFlags,CPoint point)
{
CDC* pDC=GetDC();
if(m_iPointCount==1)
{
m_FirstPoint.x=point.x;
m_FirstPoint.y=point.y;
pDC->TextOut(m_FirstPoint.x,m_FirstPoint.y,"1");
rgn1.CreateEllipticRgn((m_FirstPoint.x-50),(m_FirstPoint.y-50),(m_FirstPoint.x+50),(m_FirstPoint.y+50));
}
if(m_iPointCount==2)
{
m_SecondPoint.x=point.x;
m_SecondPoint.y=point.y;
pDC->TextOut(m_SecondPoint.x,m_SecondPoint.y,"2");
rgn2.CreateEllipticRgn((m_SecondPoint.x-50),(m_SecondPoint.y-50),(m_SecondPoint.x+50),(m_SecondPoint.y+50));
}
if(m_iPointCount==3)
{
m_ThirdPoint.x=point.x;
m_ThirdPoint.y=point.y;
pDC->TextOut(m_ThirdPoint.x,m_ThirdPoint.y,"3");
rgn3.CreateEllipticRgn((m_ThirdPoint.x-50),(m_ThirdPoint.y-50),(m_ThirdPoint.x+50),(m_ThirdPoint.y+50));
}
if(m_iPointCount==4)
{
m_FourthPoint.x=point.x;
m_FourthPoint.y=point.y;
pDC->TextOut(m_FourthPoint.x,m_FourthPoint.y,"4");
rgn4.CreateEllipticRgn((m_FourthPoint.x-50),(m_FourthPoint.y-50),(m_FourthPoint.x+50),(m_FourthPoint.y+50));
pDC->MoveTo(m_FirstPoint.x,m_FirstPoint.y);
pDC->LineTo(m_SecondPoint.x,m_SecondPoint.y);
pDC->LineTo(m_ThirdPoint.x,m_ThirdPoint.y);
pDC->LineTo(m_FourthPoint.x,m_FourthPoint.y);
pDC->LineTo(m_FirstPoint.x,m_FirstPoint.y);
}
m_iPointCount++;
}
每次点数增加。我还在4点左右创建了4个区域。我现在要做的是,当我点击我创建的任何区域并将其拖动到新位置时,该点应该移动到拖动后释放鼠标按钮的新位置。这样形状就改变了。这应该适用于所有4个地区。
我知道我必须使用Mousemove和LButtonUp函数,但我不知道该怎么做。请帮助。
答案 0 :(得分:0)
嗨,我终于想出了如何做到这一点。
OnPaint事件如下:
void CChildView::OnPaint()
{
CPaintDC dc(this); // device context for painting
{
dc.MoveTo(m_FirstPoint.x,m_FirstPoint.y);
dc.LineTo(m_SecondPoint.x,m_SecondPoint.y);
dc.LineTo(m_ThirdPoint.x,m_ThirdPoint.y);
dc.LineTo(m_FourthPoint.x,m_FourthPoint.y);
dc.LineTo(m_FirstPoint.x,m_FirstPoint.y);
}
}
LButtonDown如下:
void CChildView::OnLButtonDown(UINT nFlags,CPoint point)
{
CDC* pDC=GetDC();
if(m_iPointCount==1)
{
m_FirstPoint.x=point.x;
m_FirstPoint.y=point.y;
pDC->TextOut(m_FirstPoint.x,m_FirstPoint.y,"1");
rgn1.CreateEllipticRgn((m_FirstPoint.x-50),(m_FirstPoint.y-50),(m_FirstPoint.x+50),(m_FirstPoint.y+50));
}
if(m_iPointCount==2)
{
m_SecondPoint.x=point.x;
m_SecondPoint.y=point.y;
pDC->TextOut(m_SecondPoint.x,m_SecondPoint.y,"2");
rgn2.CreateEllipticRgn((m_SecondPoint.x-50),(m_SecondPoint.y-50),(m_SecondPoint.x+50),(m_SecondPoint.y+50));
}
if(m_iPointCount==3)
{
m_ThirdPoint.x=point.x;
m_ThirdPoint.y=point.y;
pDC->TextOut(m_ThirdPoint.x,m_ThirdPoint.y,"3");
rgn3.CreateEllipticRgn((m_ThirdPoint.x-50),(m_ThirdPoint.y-50),(m_ThirdPoint.x+50),(m_ThirdPoint.y+50));
}
if(m_iPointCount==4)
{
m_FourthPoint.x=point.x;
m_FourthPoint.y=point.y;
pDC->TextOut(m_FourthPoint.x,m_FourthPoint.y,"4");
rgn4.CreateEllipticRgn((m_FourthPoint.x-50),(m_FourthPoint.y-50),(m_FourthPoint.x+50),(m_FourthPoint.y+50));
InvalidateRect(0);
}
if(m_iPointCount>4)
{
if(PtInRegion(rgn1,point.x,point.y))
{
m_fflag=true;
}
if(PtInRegion(rgn2,point.x,point.y))
{
m_sflag=true;
}
if(PtInRegion(rgn3,point.x,point.y))
{
m_tflag=true;
}
if(PtInRegion(rgn4,point.x,point.y))
{
m_foflag=true;
}
}
m_iPointCount++;
}
MouseMove如下:
void CChildView::OnMouseMove(UINT nFlags,CPoint point)
{
if(m_iPointCount>4)
if((nFlags & MK_LBUTTON)==MK_LBUTTON)
{
if(m_fflag==true)
{
m_FirstPoint=point;
InvalidateRect(0);
}
if(m_sflag==true)
{
m_SecondPoint=point;
InvalidateRect(0);
}
if(m_tflag==true)
{
m_ThirdPoint=point;
InvalidateRect(0);
}
if(m_foflag==true)
{
m_FourthPoint=point;
InvalidateRect(0);
}
}
}
LButtonUp事件如下:
void CChildView::OnLButtonUp(UINT nFlags,CPoint point)
{
CDC* pDC=GetDC();
if(m_fflag==true)
{
m_fflag=false;
rgn1.DeleteObject();
rgn1.CreateEllipticRgn((m_FirstPoint.x-50),(m_FirstPoint.y-50),(m_FirstPoint.x+50),(m_FirstPoint.y+50));
}
if(m_sflag==true)
{
m_sflag=false;
rgn2.DeleteObject();
rgn2.CreateEllipticRgn((m_SecondPoint.x-50),(m_SecondPoint.y-50),(m_SecondPoint.x+50),(m_SecondPoint.y+50));
}
if(m_tflag==true)
{
m_tflag=false;
rgn3.DeleteObject();
rgn3.CreateEllipticRgn((m_ThirdPoint.x-50),(m_ThirdPoint.y-50),(m_ThirdPoint.x+50),(m_ThirdPoint.y+50));
}
if(m_foflag==true)
{
m_foflag=false;
rgn4.DeleteObject();
rgn4.CreateEllipticRgn((m_FourthPoint.x-50),(m_FourthPoint.y-50),(m_FourthPoint.x+50),(m_FourthPoint.y+50));
}
}
这是如何工作的:
1)整数m_iPointCount初始化为1.这用于存储点数。
2)最初将四个标志设置为假m_fflag = false; m_sflag = false; m_tflag = false; m_foflag = false;(四个标志对应多边形的四个点)。
3)当鼠标单击前4次时,4个点保存在m_Firstpoint,m_Secondpoint,m_Thirdpoint和m_Fourthpoint中。相应地创建了4个区域(rgn1,rgn2,rgn3,rgn4靠近4个点)。连接4个点以形成多边形。
4)当第五次点击时(在我们得到多边形之后),使用if条件进行检查以找出该点位于4个区域中的任何一个区域。因此标志设置为true 。(如果该点位于rgn1,则m_fflag设置为true,依此类推。)
5)考虑在rgn1中进行第5次单击,现在m_fflag为true,移动鼠标,此处更新用于在paint消息中绘制多边形的点,并使用InvalidateRect(0)调用paint方法。重绘了多边形.m_Firstpoint现在更新为新位置。
6)拖动后释放鼠标按钮时,标志设置为false,旧区域将被删除并在新点附近创建。因此该区域也会更新。
7)我已经解释了第一点,这适用于所有要点。
感谢。