C++ Exercises(五)
来源:互联网 发布:魔兽世界地图数据库 编辑:程序博客网 时间:2024/05/20 09:11
问题:在窗体上画一个圆,并且可以用鼠标拖动它在窗体上运动到任何位置
分析:
一个圆是由它的外接矩阵决定的,而外接矩阵可以由矩阵的左上角点和矩阵的高度和宽度表示,这里显然要求外接矩阵的大小是不变的,变化的仅仅是外接矩阵的左上角点,所以要处理的就是矩阵左上角点的变化。
为此设置类的成员变量如下:
CPoint m_pointTopLeft;//外接矩形左上角点
const CSize m_sizeEcllipse;//外接矩形大小
CSize m_sizeOffSet;//光标距离左上角点距离
bool m_bCaptured;//鼠标是否处于被捕获状态
const CSize m_sizeEcllipse;//外接矩形大小
CSize m_sizeOffSet;//光标距离左上角点距离
bool m_bCaptured;//鼠标是否处于被捕获状态
其中矩阵大小是常量,在初始化的时候指明,这里我指定左上角点为坐标原点(0,0),高度100,宽度100,初始的鼠标光标和原点重合(即两者距离都是0),并且鼠标未被捕获。
CTestView::CTestView():m_sizeEcllipse(100,100),m_pointTopLeft(0,0),m_sizeOffSet(0,0)
{
this->m_bCaptured = FALSE;//初始状态为未捕获
}
{
this->m_bCaptured = FALSE;//初始状态为未捕获
}
接下来就是在OnDraw里面画圆了,因为m_pointTopLeft和m_sizeEcllipse已经唯一决定了圆的位置与大小,因此直接利用这两个变量进行绘图就可以了。
void CTestView::OnDraw(CDC* pDC)
{
CE05Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CBrush brushHatch(HS_DIAGCROSS, RGB(255, 0, 0));
pDC->SelectObject(&brushHatch);//选择刷子
pDC->Ellipse(CRect(m_pointTopLeft, this->m_sizeEcllipse));
}
{
CE05Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CBrush brushHatch(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中把鼠标的捕获释放掉。
void CTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
CRect rt(this->m_pointTopLeft,this->m_sizeEcllipse);
CRgn rg;
CClientDC dc(this);
OnPrepareDC(&dc);
dc.LPtoDP(&rt);//转换到设备坐标
rg.CreateEllipticRgnIndirect(&rt);
if(rg.PtInRegion(point))
{//在圆内单击左键了
this->SetCapture();//捕获光标
CPoint pointTopLeft(this->m_pointTopLeft);//前一次的左上角
this->m_sizeOffSet = point-pointTopLeft;//第一次点左键时光标距离左上角的距离
this->m_bCaptured = TRUE;//捕获标志设为true
::SetCursor(::LoadCursor(NULL,IDC_CROSS));//更改光标形状
}
}
void CTestView::OnLButtonUp(UINT nFlags, CPoint point)
{
if(this->m_bCaptured)
{//光标处于捕获状态中
this->m_bCaptured = FALSE; //捕获标志设为false
::ReleaseCapture();//释放鼠标捕获
}
}
void CTestView::OnMouseMove(UINT nFlags, CPoint point)
{
if(this->m_bCaptured)
{//光标处于捕获状态中
CClientDC dc(this);
OnPrepareDC(&dc);
CRect oldRect(this->m_pointTopLeft,this->m_sizeEcllipse);//老的外接矩形
dc.LPtoDP(&oldRect);
this->InvalidateRect(&oldRect,TRUE);//强制重画
this->m_pointTopLeft = point-this->m_sizeOffSet;//新的左上角点
CRect newRect(this->m_pointTopLeft,this->m_sizeEcllipse);//新外接矩形
dc.LPtoDP(&newRect);
this->InvalidateRect(&newRect,TRUE); //强制重画
}
}
{
CRect rt(this->m_pointTopLeft,this->m_sizeEcllipse);
CRgn rg;
CClientDC dc(this);
OnPrepareDC(&dc);
dc.LPtoDP(&rt);//转换到设备坐标
rg.CreateEllipticRgnIndirect(&rt);
if(rg.PtInRegion(point))
{//在圆内单击左键了
this->SetCapture();//捕获光标
CPoint pointTopLeft(this->m_pointTopLeft);//前一次的左上角
this->m_sizeOffSet = point-pointTopLeft;//第一次点左键时光标距离左上角的距离
this->m_bCaptured = TRUE;//捕获标志设为true
::SetCursor(::LoadCursor(NULL,IDC_CROSS));//更改光标形状
}
}
void CTestView::OnLButtonUp(UINT nFlags, CPoint point)
{
if(this->m_bCaptured)
{//光标处于捕获状态中
this->m_bCaptured = FALSE; //捕获标志设为false
::ReleaseCapture();//释放鼠标捕获
}
}
void CTestView::OnMouseMove(UINT nFlags, CPoint point)
{
if(this->m_bCaptured)
{//光标处于捕获状态中
CClientDC dc(this);
OnPrepareDC(&dc);
CRect oldRect(this->m_pointTopLeft,this->m_sizeEcllipse);//老的外接矩形
dc.LPtoDP(&oldRect);
this->InvalidateRect(&oldRect,TRUE);//强制重画
this->m_pointTopLeft = point-this->m_sizeOffSet;//新的左上角点
CRect newRect(this->m_pointTopLeft,this->m_sizeEcllipse);//新外接矩形
dc.LPtoDP(&newRect);
this->InvalidateRect(&newRect,TRUE); //强制重画
}
}
- C++ Exercises(五)
- Accelerated C++Exercises' Resolutions
- c++primer exercises 2.9
- c++primer exercises 7.0
- C++ Exercises(十九)
- C++ Exercises(十八)
- C++ Exercises(十四)
- C++ Exercises(十三)
- C++ Exercises(十二)
- C++ Exercises(十一)
- C++ Exercises(十)
- C++ Exercises(九)
- C++ Exercises(八)
- C++ Exercises(七)
- C++ Exercises(六)
- C++ Exercises(八)
- C++ Exercises(七)
- C++ Exercises(六)
- SWT Designer 6.0安装小记
- complex的小困惑
- C++ Exercises(六)
- [转]VC++技术内幕(第四版)笔记(第7章)
- [转]VC++技术内幕(第四版)笔记(第6章)
- C++ Exercises(五)
- [转]VC++技术内幕(第四版)笔记(第5章)
- 一个小问题
- [转]VC++技术内幕(第四版)笔记(第4章)
- [转]VC++技术内幕(第四版)笔记(第3章)
- [转]VC++技术内幕(第四版)笔记(1~2章)
- 整合ActiveMQ和JBoss
- OnDraw()和OnPaint()的区别
- 解决“显示桌面”快捷方式的丢失