C++ Exercises(五)

本文介绍了一种在窗体上绘制可拖动圆形的方法。通过记录鼠标点击位置和相对于圆形左上角的距离,实现了鼠标拖动时圆形位置的更新及重绘。文中详细解释了实现过程中的关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题:在窗体上画一个圆,并且可以用鼠标拖动它在窗体上运动到任何位置


分析:
一个圆是由它的外接矩阵决定的,而外接矩阵可以由矩阵的左上角点和矩阵的高度和宽度表示,这里显然要求外接矩阵的大小是不变的,变化的仅仅是外接矩阵的左上角点,所以要处理的就是矩阵左上角点的变化。
为此设置类的成员变量如下:

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> CPointm_pointTopLeft; // 外接矩形左上角点
const CSizem_sizeEcllipse; // 外接矩形大小
CSizem_sizeOffSet; // 光标距离左上角点距离
bool m_bCaptured; // 鼠标是否处于被捕获状态

其中矩阵大小是常量,在初始化的时候指明,这里我指定左上角点为坐标原点(0,0),高度100,宽度100,初始的鼠标光标和原点重合(即两者距离都是0),并且鼠标未被捕获。

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> CTestView::CTestView():m_sizeEcllipse( 100 , 100 ),m_pointTopLeft( 0 , 0 ),m_sizeOffSet( 0 , 0 )
{
this -> m_bCaptured = FALSE; // 初始状态为未捕获
}

接下来就是在OnDraw里面画圆了,因为m_pointTopLeftm_sizeEcllipse已经唯一决定了圆的位置与大小,因此直接利用这两个变量进行绘图就可以了。

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> void CTestView::OnDraw(CDC * pDC)
{
CE05Doc
* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CBrushbrushHatch(HS_DIAGCROSS,RGB(
255 , 0 , 0 ));
pDC
-> SelectObject( & brushHatch); // 选择刷子
pDC -> Ellipse(CRect(m_pointTopLeft, this -> m_sizeEcllipse));
}

最后就是如何响应鼠标的三个事件:down,move,up了,再这里主要就是处理左上角点的变化,在down的时候,由于第一次点击圆内的点时就确定了鼠标光标与初始左上角点的距离,在以后的move中只需要用当前所在的point减去确定的m_sizeOffSet就可以确定此时的新左上角点,并且在move中利用前后两次的外接矩阵进行重画就可以了,最后在up中把鼠标的捕获释放掉。

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> void CTestView::OnLButtonDown(UINTnFlags,CPointpoint)
{
CRectrt(
this -> m_pointTopLeft, this -> m_sizeEcllipse);
CRgnrg;
CClientDCdc(
this );
OnPrepareDC(
& dc);
dc.LPtoDP(
& rt); // 转换到设备坐标
rg.CreateEllipticRgnIndirect( & rt);
if (rg.PtInRegion(point))
{
// 在圆内单击左键了
this -> SetCapture(); // 捕获光标
CPointpointTopLeft( this -> m_pointTopLeft); // 前一次的左上角
this -> m_sizeOffSet = point - pointTopLeft; // 第一次点左键时光标距离左上角的距离
this -> m_bCaptured = TRUE; // 捕获标志设为true
::SetCursor(::LoadCursor(NULL,IDC_CROSS)); // 更改光标形状
}
}
void CTestView::OnLButtonUp(UINTnFlags,CPointpoint)
{
if ( this -> m_bCaptured)
{
// 光标处于捕获状态中
this -> m_bCaptured = FALSE; // 捕获标志设为false
::ReleaseCapture(); // 释放鼠标捕获
}
}
void CTestView::OnMouseMove(UINTnFlags,CPointpoint)
{
if ( this -> m_bCaptured)
{
// 光标处于捕获状态中
CClientDCdc( this );
OnPrepareDC(
& dc);
CRectoldRect(
this -> m_pointTopLeft, this -> m_sizeEcllipse); // 老的外接矩形
dc.LPtoDP( & oldRect);
this -> InvalidateRect( & oldRect,TRUE); // 强制重画
this -> m_pointTopLeft = point - this -> m_sizeOffSet; // 新的左上角点
CRectnewRect( this -> m_pointTopLeft, this -> m_sizeEcllipse); // 新外接矩形
dc.LPtoDP( & newRect);
this -> InvalidateRect( & newRect,TRUE); // 强制重画
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值