要求很简单:
用鼠标画圆圈,鼠标按下开始画,鼠标移动调整大小,鼠标弹起就画好了.
找了两天的GOOGLE,居然没找到,画圆的API又不太好用啊,累死人,不过问题是难不到我的
还是找到点资料,不过是画矩形的,有矩形了当然圆就不远了.
用鼠标画圆圈,鼠标按下开始画,鼠标移动调整大小,鼠标弹起就画好了.
找了两天的GOOGLE,居然没找到,画圆的API又不太好用啊,累死人,不过问题是难不到我的
还是找到点资料,不过是画矩形的,有矩形了当然圆就不远了.
CODE:
class CDrawcirleView : public CView
{
protected: // create from serialization only
CDrawcirleView();
DECLARE_DYNCREATE(CDrawcirleView)
CRect rect; //定义个矩形变量
bool m_bCaptured; //截获鼠标标志
bool bInvert; //转换标志
CBrush *m_pBrushOld; //画刷指针
int m_countShapes; //计数,不要也可
.......
}
//初始化变量
CDrawcirleView::CDrawcirleView()
{
m_bCaptured=false;
bInvert=false;
m_countShapes=0;
m_pBrushOld=NULL;
// TODO: add construction code here
}
//转换函数
void CDrawcirleView::InvertShape(CDC *pDC, CRect &s, bool bInvert)
{
ASSERT(pDC!=NULL); //是否有效
int nModeOld; //旧模式
if(bInvert) //判断
{
nModeOld=pDC->SetROP2(R2_NOT); //与背景相反
}
SetPenBrush(pDC); //设置画刷
pDC->Ellipse(s); //根据矩形画圆
if(bInvert) //再判断
{
pDC->SetROP2(nModeOld); //恢复原来的模式
}
ResetPenBrush(pDC); //重新设置恢复画刷
}
//设置画刷
void CDrawcirleView::SetPenBrush(CDC *pDC)
{
ASSERT(pDC!=NULL);
m_pBrushOld=(CBrush*)pDC->SelectStockObject(NULL_BRUSH);
}
//重新设置画刷
void CDrawcirleView::ResetPenBrush(CDC *pDC)
{
ASSERT(pDC!=NULL);
ASSERT(m_pBrushOld!=NULL);
pDC->SelectObject(m_pBrushOld);
m_pBrushOld=NULL;
}
//鼠标左键按下响应
void CDrawcirleView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
Invalidate(); //每次点左键就刷新一次
SetCapture(); //捕获鼠标
m_bCaptured=true; //设置标志
rect.left=rect.right=point.x; //设置矩形起点为鼠标点击处
rect.top=rect.bottom=point.y;
CView::OnLButtonDown(nFlags, point);
}
//鼠标滑动响应
void CDrawcirleView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bCaptured) //要捕获鼠标标志为真才执行
{
ClientDC dc(this); //设备装置
InvertShape(&dc,rect,TRUE); //转换
rect.bottom=point.y; //设置右下脚坐标
rect.right=point.x;
InvertShape(&dc,rect,TRUE);
}
CView::OnMouseMove(nFlags, point);
}
//鼠标弹起
void CDrawcirleView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bCaptured) //判断
{
::ReleaseCapture(); //释放
m_bCaptured=false; //设置标志为假
CClientDC dc(this);
InvertShape(&dc,rect,TRUE); //转换
rect.right=point.x;
rect.bottom=point.y;
InvertShape(&dc,rect,false); //转换
// m_countShapes++; //计数
}
CView::OnLButtonUp(nFlags, point);
}
主要作用应该在转换函数上,我不解的是
以鼠标单击处为坐标原点
万一鼠标停在了其第二,三象限处,为何还能正确画出圆???
高手指教!!!
{
protected: // create from serialization only
CDrawcirleView();
DECLARE_DYNCREATE(CDrawcirleView)
CRect rect; //定义个矩形变量
bool m_bCaptured; //截获鼠标标志
bool bInvert; //转换标志
CBrush *m_pBrushOld; //画刷指针
int m_countShapes; //计数,不要也可
.......
}
//初始化变量
CDrawcirleView::CDrawcirleView()
{
m_bCaptured=false;
bInvert=false;
m_countShapes=0;
m_pBrushOld=NULL;
// TODO: add construction code here
}
//转换函数
void CDrawcirleView::InvertShape(CDC *pDC, CRect &s, bool bInvert)
{
ASSERT(pDC!=NULL); //是否有效
int nModeOld; //旧模式
if(bInvert) //判断
{
nModeOld=pDC->SetROP2(R2_NOT); //与背景相反
}
SetPenBrush(pDC); //设置画刷
pDC->Ellipse(s); //根据矩形画圆
if(bInvert) //再判断
{
pDC->SetROP2(nModeOld); //恢复原来的模式
}
ResetPenBrush(pDC); //重新设置恢复画刷
}
//设置画刷
void CDrawcirleView::SetPenBrush(CDC *pDC)
{
ASSERT(pDC!=NULL);
m_pBrushOld=(CBrush*)pDC->SelectStockObject(NULL_BRUSH);
}
//重新设置画刷
void CDrawcirleView::ResetPenBrush(CDC *pDC)
{
ASSERT(pDC!=NULL);
ASSERT(m_pBrushOld!=NULL);
pDC->SelectObject(m_pBrushOld);
m_pBrushOld=NULL;
}
//鼠标左键按下响应
void CDrawcirleView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
Invalidate(); //每次点左键就刷新一次
SetCapture(); //捕获鼠标
m_bCaptured=true; //设置标志
rect.left=rect.right=point.x; //设置矩形起点为鼠标点击处
rect.top=rect.bottom=point.y;
CView::OnLButtonDown(nFlags, point);
}
//鼠标滑动响应
void CDrawcirleView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bCaptured) //要捕获鼠标标志为真才执行
{
ClientDC dc(this); //设备装置
InvertShape(&dc,rect,TRUE); //转换
rect.bottom=point.y; //设置右下脚坐标
rect.right=point.x;
InvertShape(&dc,rect,TRUE);
}
CView::OnMouseMove(nFlags, point);
}
//鼠标弹起
void CDrawcirleView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bCaptured) //判断
{
::ReleaseCapture(); //释放
m_bCaptured=false; //设置标志为假
CClientDC dc(this);
InvertShape(&dc,rect,TRUE); //转换
rect.right=point.x;
rect.bottom=point.y;
InvertShape(&dc,rect,false); //转换
// m_countShapes++; //计数
}
CView::OnLButtonUp(nFlags, point);
}
主要作用应该在转换函数上,我不解的是
以鼠标单击处为坐标原点
万一鼠标停在了其第二,三象限处,为何还能正确画出圆???
高手指教!!!
1492

被折叠的 条评论
为什么被折叠?



