2014-10-30 72 views
0

我刚开始学习MFC的基础知识,因此我写了一个小练习程序,它只跟踪相对于窗口和每次移动屏幕的坐标并调整大小,一切正常除了这一点:MFC OnSize()问题无效

enter image description here

我怎样才能得到它,以便有留下当我调整窗口的大小没有结尾的油漆?

这里是我的筛上部分()代码:

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++; 
} 
+0

你的OnSize代码是什么样的? – 2014-10-30 19:12:42

+0

@MarkRansom我已更新我的问题,代码为 – Riptyde4 2014-10-30 19:14:19

回答

0

InvalidateRect需要一个矩形客户坐标。您正在通过它的结果GetWindowRect,它返回屏幕坐标。该窗口没有被正确无效,因此没有被擦除。

请注意,当窗口被删除时,您在OnSize功能中执行的绘画也将被删除。你应该真的保留所有你的绘画的OnPaint函数来防止这个问题。

+0

我在InvalidateRect之前将GetWindowRect更改为GetClientRect,并且尾随的paint仍然存在:/ – Riptyde4 2014-10-30 19:28:42

+0

@Riptyde4您的OnEraseBkgnd是什么样的? – 2014-10-30 19:33:19

+0

我第一次听到这个消息时,我需要在处理程序的内部做些什么? – Riptyde4 2014-10-30 20:14:16