立即调用OnMove()而不是等待Window的实际移动

时间:2014-11-04 23:27:04

标签: c++ visual-c++ visual-studio-2013 mfc

我的MFC应用程序出现问题 它应该显示窗口的坐标,然后当您移动窗口时,显示带有新坐标的新行。我的问题是OnMove()函数似乎立即被调用,并且所有内容总是一次显示。

这是我的OnPaint()代码:

void CMainFrame::OnPaint()
{

CRect rect;
HWND hWnd;
CPaintDC dc(this); // device context for painting
GetClientRect(&rect);

CString topLeftx, topLefty, topRightx, topRighty;
CString bottomLeftx, bottomLefty, bottomRightx, bottomRighty;
CString topLeft, topRight, bottomLeft, bottomRight;
CString topLeftxL, topLeftyL, topRightxL, topRightyL;
CString bottomLeftxL, bottomLeftyL, bottomRightxL, bottomRightyL;
CString topLeftL, topRightL, bottomLeftL, bottomRightL;


topLeftx.Format(_T("%d"), (rect.TopLeft().x));
topLefty.Format(_T("%d"), rect.TopLeft().y);
topLeft = topLeftx + "," + topLefty;

topRightx.Format(_T("%d"), rect.TopLeft().x + rect.Width());
topRighty.Format(_T("%d"), rect.TopLeft().y);
topRight = topRightx + "," + topRighty;

bottomLeftx.Format(_T("%d"), rect.BottomRight().x - rect.Width());
bottomLefty.Format(_T("%d"), rect.BottomRight().y);
bottomLeft = bottomLeftx + "," + bottomLefty;

bottomRightx.Format(_T("%d"), rect.BottomRight().x);
bottomRighty.Format(_T("%d"), rect.BottomRight().y);
bottomRight = bottomRightx + "," + bottomRighty;

GetWindowRect(&rect);

topLeftxL.Format(_T("%d"), rect.left);
topLeftyL.Format(_T("%d"), rect.top);
topLeftL = topLeftxL + "," + topLeftyL;

topRightxL.Format(_T("%d"), rect.TopLeft().x + rect.Width());
topRightyL.Format(_T("%d"), rect.TopLeft().y);
topRightL = topRightxL + "," + topRightyL;

bottomLeftxL.Format(_T("%d"), rect.BottomRight().x - rect.Width());
bottomLeftyL.Format(_T("%d"), rect.BottomRight().y);
bottomLeftL = bottomLeftxL + "," + bottomLeftyL;

bottomRightxL.Format(_T("%d"), rect.BottomRight().x);
bottomRightyL.Format(_T("%d"), rect.BottomRight().y);
bottomRightL = bottomRightxL + "," + bottomRightyL;




dc.TextOutW(0, 20, _T("Hello from ROSSSSSSS!!!!"));
dc.TextOutW(0, 40, _T("TopLeft:"));
dc.TextOutW(60, 40, topLeft);
dc.TextOutW(130, 40, _T("TopRight:"));
dc.TextOutW(200, 40, topRight);
dc.TextOutW(280, 40, _T("BottomLeft:"));
dc.TextOutW(365, 40, bottomLeft);
dc.TextOutW(445, 40, _T("BottomRight:"));
dc.TextOutW(550, 40, bottomRight);
dc.TextOutW(0, 60, _T("TopLeft:"));
dc.TextOutW(60, 60, topLeftL);
dc.TextOutW(130, 60, _T("TopRight:"));
dc.TextOutW(200, 60, topRightL);
dc.TextOutW(280, 60, _T("BottomLeft:"));
dc.TextOutW(365, 60, bottomLeftL);
dc.TextOutW(445, 60, _T("BottomRight:"));
dc.TextOutW(550, 60, bottomRightL);



Invalidate();
UpdateWindow();
}

这是OnMove

void CMainFrame::OnMove(int x, int y)

{
CFrameWnd::OnMove(x, y);
CDC *dc;

dc = GetDC();
Invalidate();

dc->TextOutW(0, 80, _T("TEST"));

UpdateWindow();

}

在我运行应用程序而不是等待移动时,知道为什么单词TEST会出现?

1 个答案:

答案 0 :(得分:2)

因为窗口的初始创建和显示会导致对OnMove()的调用。另一个提示 - 只需在OnPaint()函数中绘制文本,就可以省去很多麻烦。其他消息处理程序应该捕获数据以供OnPaint()以后使用,而不是立即执行绘制。