`
phinecos
  • 浏览: 344724 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

C++ Exercises(五)

 
阅读更多

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


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

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->CPointm_pointTopLeft;//外接矩形左上角点
constCSizem_sizeEcllipse;//外接矩形大小
CSizem_sizeOffSet;//光标距离左上角点距离
boolm_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>-->voidCTestView::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>-->voidCTestView::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));//更改光标形状
}
}
voidCTestView::OnLButtonUp(UINTnFlags,CPointpoint)
{
if(this->m_bCaptured)
{
//光标处于捕获状态中
this->m_bCaptured=FALSE;//捕获标志设为false
::ReleaseCapture();//释放鼠标捕获
}
}
voidCTestView::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);//强制重画
}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics