我是新手,按照示例做了个在单文档中绘制矩形的例子,能够正常执行,再绘制第二个矩形时第一个就消失了。
疑问:绘制完后,再次点击鼠标左键为什么绘制的矩形就消失了?怎么样可以让它不消失?
代码如下:
//// 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);
}

解决方案 »

  1.   

    画图的操作应该放到OnDraw函数中去执行,因为当对程序进行一些操作后,
    比如移动窗口等,应用程序会自动调用视类中的OnDraw函数对视图进行重
    新绘制,因为你的画图函数不是写再OnDraw函数中,所以当调用Ondraw函数
    时,应用程序只是把背景擦去,而没有重新画上你之前画的图。你没有给分啊,一般都没什么人回答的...现在的人都很现实~