MFC OnSize()问题与Invalidate

时间:2014-10-30 19:09:59

标签: c++ windows mfc invalidation

我刚刚开始学习MFC的基础知识,所以我写了一个小练习程序,它只跟踪相对于窗口的坐标和每次移动时的屏幕并调整大小,除此之外一切都很好:

enter image description here

我如何才能获得它,以便在调整窗口大小时没有留下拖尾的颜料?

这是我的onSize()代码:

void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
CFrameWnd::OnSize(nType, cx, cy);

// TODO: Add your message handler code here

// Get the window
CDC* dc;
dc = GetDC();
CRect rect;
GetWindowRect(rect);
InvalidateRect(rect);

// This if statement just makes sure new red text coordinates are not printed immediately upon starting the program
if (numSizeCalls > 1){ 
    // Set up the font for future text output
    CFont myfont;
    VERIFY(myfont.CreateFont(
        20, // nHeight in points
        0,  // nWidth
        0,  // nEscapement
        0,  // nOrientation
        FW_NORMAL,  // nWeight
        TRUE,   // bItalic
        FALSE,  // bUnderline
        0,  // cStrikeOut
        ANSI_CHARSET,   // nCharSet
        OUT_DEFAULT_PRECIS,        // nOutPrecision
        CLIP_DEFAULT_PRECIS,        // nClipPrecision
        DEFAULT_QUALITY,              // nQuality
        DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
        _T("Arial")));  // lpszFacename 
    dc->SetTextColor(RGB(255, 0, 0));
    dc->SelectObject(myfont);
    DeleteObject(myfont);

    // Set window coordinates relative to client area
    ScreenToClient(&rect);

    // Format them for TextOut
    CString tl;
    tl.Format(L"%d %d ; ", rect.top, rect.left);
    CString tr;
    tr.Format(L"%d %d ; ", rect.top, rect.right);
    CString bl;
    bl.Format(L"%d %d ; ", rect.bottom, rect.left);
    CString br;
    br.Format(L"%d %d", rect.bottom, rect.right);

    // Get position for TextOut
    TextOutYPosition += 20;

    // Print them
    dc->TextOut(5, TextOutYPosition, tl + tr + bl + br, _tcslen(tl) + _tcslen(tr) + _tcslen(bl) + _tcslen(br));

    // Set coords relative to the screen
    GetWindowRect(&rect);

    // Format them for TextOut
    tl.Format(L"%d %d ; ", rect.top, rect.left);
    tr.Format(L"%d %d ; ", rect.top, rect.right);
    bl.Format(L"%d %d ; ", rect.bottom, rect.left);
    br.Format(L"%d %d", rect.bottom, rect.right);

    // Get position for TextOut
    TextOutYPosition += 20;

    // Print them
    dc->TextOut(5, TextOutYPosition, tl + tr + bl + br, _tcslen(tl) + _tcslen(tr) + _tcslen(bl) + _tcslen(br));
}
numSizeCalls++;
}

1 个答案:

答案 0 :(得分:0)

InvalidateRect客户端坐标中采用矩形。您正在传递GetWindowRect的结果,该结果会返回屏幕坐标。窗口没有正确无效,因此不会被删除。

请注意,当窗口被删除时,您在OnSize功能中执行的绘制也将被删除。您应该为OnPaint函数保留所有绘画以防止出现此问题。