问题:在窗体上画一个圆,并且可以用鼠标拖动它在窗体上运动到任何位置
分析:
一个圆是由它的外接矩阵决定的,而外接矩阵可以由矩阵的左上角点和矩阵的高度和宽度表示,这里显然要求外接矩阵的大小是不变的,变化的仅仅是外接矩阵的左上角点,所以要处理的就是矩阵左上角点的变化。
为此设置类的成员变量如下:
<!--<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_pointTopLeft和m_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);//强制重画
}
}
分享到:
相关推荐
C++ PRIMER EXERCISES
C++的一些练习题,《C++语言程序设计(第四版)
Templates: The Hard Way Templates: The C++ Way Function Specialization Class Templates Class Specialization Implementation Details Advanced Features Summary Programming Exercises 25. Standard ...
The C++ Programming Language(4th)Exercises 英文无水印pdf 第4版 pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如...
C, C++ and Python Code for Exercises and Solutions.zip
C++类的练习(C + + class exercises)
1.1 INTRODUCTION TO C++ 2 Origins of the C++ Language 2 C++ and Object-Oriented Programming 3 The Character of C++ 3 C++ Terminology 4 ...ANSWERS TO SELF-TEST EXERCISES 39 PROGRAMMING PROJECTS 41
more examples of c++ exercises
Starting out with C++,英文原版,适合初学者
Learn the Standard Template Library and the containers and algorithms used in most real-world C++ applications * Learn how to build effective programs in C++ with hands-on exercises * Get expert tips ...
Mathematical Objects in C++: Computational Tools in A Unified Object-Oriented Approach (Chapman & Hall/CRC Numerical Analysis and Scientific Computing Series) By 作者: Yair Shapira ISBN-10 书号: ...
Features numerous working examples and exercises that help build programming skills Programmer Forums Join our Programmer to Programmer forums to ask and answer programming questions about this book,...
Author: Josh Lospinoso Pub Date: 2019 ISBN: 978-1593278885 ...With well over 500 code samples and nearly 100 exercises, C++ Crash Course is sure to help you build a strong C++ foundation.
You’ll hone real-world skills through practical exercises, and deepen your expertise through start-to-finish projects that grow in complexity as you build your skills. Throughout, Madhav pays ...
sections in each chapter and a multitude of new and updated exercises. All syntax is explained thoroughly and reinforced through extensive examples and diagrams, and each chapter is full of helpful ...
Syntax examples are clearly marked for handy reference. <br>To help you become more proficient, each lesson ends with a set of common questions and answers, exercises, and a quiz. You can check ...
In addition, it now includes * Even more figures, examples, and exercises * Even more puzzles and games * An expanded 75-page language reference * Instructions for downloading free C++ software ...
全英文的哦。。还可以练英语 1.1 INTRODUCTION TO C++ 2 Origins of the C++ Language 2 C++ and Object-Oriented Programming 3 The Character of C++ 3...ANSWERS TO SELF-TEST EXERCISES 39 PROGRAMMING PROJECTS 41