在单文档中绘制多个矩形的问题

slyrabbit 2009-06-13 07:52:11
我是新手,按照示例做了个在单文档中绘制矩形的例子,能够正常执行,再绘制第二个矩形时第一个就消失了。
疑问:绘制完后,再次点击鼠标左键为什么绘制的矩形就消失了?怎么样可以让它不消失?
代码如下:
//// Ch5Demo4View.h
public:
BOOL fDowned;
CPoint ptDown;
CPoint ptUp;
//// Ch5Demo4View.cpp
CCh5Demo4View::CCh5Demo4View()
{
// TODO: add construction code here
fDowned=false;
ptDown.x=0;
ptDown.y=0;
ptUp.x=0;
ptUp.y=0;

}
void CCh5Demo4View::DrawRect()
{
CClientDC dc(this);
CRect rect;
GetClientRect(rect);
CBrush brush(RGB(255,255,255));
dc.FillRect(rect,&brush);
dc.Rectangle(ptDown.x,ptDown.y,ptUp.x,ptUp.y);
}

void CCh5Demo4View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//fDowned=true;
ptDown=point;
fDowned=true;
CView::OnLButtonDown(nFlags, point);
}

void CCh5Demo4View::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (fDowned)
{
ptUp=point;
DrawRect();
fDowned=false;
}
CView::OnLButtonUp(nFlags, point);
}

void CCh5Demo4View::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (fDowned)
{
ptUp=point;
DrawRect();
}
CView::OnMouseMove(nFlags, point);
}
...全文
124 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
slyrabbit 2009-06-15
  • 打赏
  • 举报
回复
非常感谢,世界上还是像你这样的好人多啊
hdjfeng2009 2009-06-13
  • 打赏
  • 举报
回复
画图的操作应该放到OnDraw函数中去执行,因为当对程序进行一些操作后,
比如移动窗口等,应用程序会自动调用视类中的OnDraw函数对视图进行重
新绘制,因为你的画图函数不是写再OnDraw函数中,所以当调用Ondraw函数
时,应用程序只是把背景擦去,而没有重新画上你之前画的图。

你没有给分啊,一般都没什么人回答的...现在的人都很现实~

19,464

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧