void CBitmapView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
if (lHint == 0x7c) {
int* obj = (int*)pHint; // pHint保存方格的位置,和颜色
ASSERT(obj[0] >= 0 && obj[0] < 25 && obj[1] >= 0 && obj[1] < 40);
int x1 = obj[0] * 30 + 30;   //记算LEFT TOP X坐标
int y1 = obj[1] * -30 - 30;  //记算LEFT TOP Y坐标
int x2 = x1 + 30;            // RIGHT BOTTOM X坐标
int y2 = y1 - 30;            // RIGHT BOTTOM Y坐标
CRect rect(x1, y1, x2, y2);
CBrush brush(obj[2]);     
CClientDC dc(this);
OnPrepareDC(&dc);
dc.FillRect(rect, &brush);
InvalidateRect(&rect);
return;
} CView::OnUpdate(pSender, lHint, pHint);
}
其中CBitmapView如下定义
class CBitmapView : public CView {
      ...
}我要实现的功能是在鼠标左键按下时,其所对应的方格用选中的颜色填充,但是程序运行后并不能立即重绘,屏幕上一点变化都没有,但是如果把窗口最小化或用别的窗口将它覆盖后却又看到重绘了,请问这是怎么回事?

解决方案 »

  1.   

    就目前楼主的代码最简单的方法是发送WM_SIZE消息
    在OnUpdate中,语句CView::OnUpdate(pSender, lHint, pHint);之前
    SendMessage(WM_SIZE);
      

  2.   

    没有试过,我不知道行不行;
    如果不行,也可以试一下发消息WM_PAINT,
    在自己的OnPaint处理函数相应一下;
    目的是通知你的窗口 “我要重绘” 因为你目前代码没有机制告诉系统“我要重绘窗口”
      

  3.   

    我试了一下楼上的方法,还是不行,而且如果IF条件为真的话就返回了,在语句CView::OnUpdate(pSender, lHint, pHint);之前用SendMessage(WM_SIZE),它得不到执行的啊
      

  4.   

    我用InvalidateRect(&rect);
    能不能实现重绘?我不大清楚
    但看MSDN上面介绍这名应该可以实现重绘的
      

  5.   

    在OnLButtonDown中调用pDoc-》UpdateAllViews(NULL,0x7c,NULL);
      

  6.   

    CDocument::UpdateAllViews
    This method is called after the document has been modified. You should call this method after you call the SetModifiedFlag method. This method informs each view attached to the document, except for the view specified by pSender, that the document has been modified. You typically call the CDocument::UpdateAllViews method from your view class after the user has changed the document through a view.void UpdateAllViews(
    CView* pSender,
    LPARAM lHint = 0L,
    CObject* pHint = NULL );
      

  7.   

    其实我的错误很搞笑,在于我自己的一个疏忽
    我的程序是基于MM_LOENGLISH的映射模式,但在该消息处理中没有设置MM_LOENGLISH
    所以
    int x1 = obj[0] * 30 + 30;   //记算LEFT TOP X坐标
    int y1 = obj[1] * -30 - 30;  //记算LEFT TOP Y坐标
    int x2 = x1 + 30;            // RIGHT BOTTOM X坐标
    int y2 = y1 - 30;            // RIGHT BOTTOM Y坐标
    计算出来的坐标其实已经在窗口的外面了
    所以从我上面的代码是看不出来错误了
    这给了我一个教训,不能头痛医头,脚痛医脚
    多谢楼上各位花时间看了我的问题