GDI+重新绘制 Vista 时没有清除我的窗口
在 WM_PAINT 上我执行以下操作:
//RectF mNameRect;
//WCHAR* mName;
//HWND mWin; // this is the window handle
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(mWin, &ps);
Graphics g(hdc);
g.Clear(Color::White);
StringFormat stringForm;
stringForm.SetLineAlignment(StringAlignmentCenter);
stringForm.SetAlignment(StringAlignmentCenter);
// set the rectangle to the size of the whole window
mNameRect.Width = static_cast<float>(size.cx);
mNameRect.Height = static_cast<float>(size.cy);
g.DrawString(mName, -1, &mNameFont, mNameRect, &stringForm, &mNameBrush);
EndPaint(mWin, &ps);
}
在 XP 中这工作正常,mName 显示在窗口中间。然而在 Vista 上,文本不会移动,无论我如何调整窗口大小,它都会保持在原来的位置。 g.Clear(Color::White)
似乎没有任何区别。当窗口隐藏在另一个窗口后面并且焦点需要再次重新绘制时,文本甚至不会改变位置。
如何更改 mName 在 Vista 中的位置?
编辑: 绘制代码通过 WM_PAINT 和 WM_SIZE 按以下方式调用:
// WndProc function
switch (msg){
case WM_SIZE:
// intentionally call paint when WM_SIZE is triggered
case WM_PAINT:
paint();
break;
on WM_PAINT i do the following:
//RectF mNameRect;
//WCHAR* mName;
//HWND mWin; // this is the window handle
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(mWin, &ps);
Graphics g(hdc);
g.Clear(Color::White);
StringFormat stringForm;
stringForm.SetLineAlignment(StringAlignmentCenter);
stringForm.SetAlignment(StringAlignmentCenter);
// set the rectangle to the size of the whole window
mNameRect.Width = static_cast<float>(size.cx);
mNameRect.Height = static_cast<float>(size.cy);
g.DrawString(mName, -1, &mNameFont, mNameRect, &stringForm, &mNameBrush);
EndPaint(mWin, &ps);
}
In XP this works fine, the mName is displayed in the middle of the window. However on Vista the text doesn't move, it stays in its location no matter how I resize the window. the g.Clear(Color::White)
doesn't seem to do any difference. The text doesn't even change position when the window is hidden behind another window and on focus needs to be repainted again.
How do I make mName change position in Vista?
Edit:
The paint code gets called via WM_PAINT and via WM_SIZE in the following manner:
// WndProc function
switch (msg){
case WM_SIZE:
// intentionally call paint when WM_SIZE is triggered
case WM_PAINT:
paint();
break;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当窗口大小调整时,您显式调用
paint()
函数。但是,您的窗口并未失效,因此系统可能将您的绘制工作限制在标记为“脏”的区域。与其直接调用
paint()
,最好使用InvalidateRgn
触发重绘。这将导致发送WM_PAINT
,该消息将由您的应用程序以正常方式处理。作为奖励,您还可以告诉InvalidateRgn
为您擦除背景You are explicitly calling your
paint()
function when the window is resized. However, your window is not invalidated, so it could be that the system is restricting your painting efforts to the region marked "dirty".Instead of calling
paint()
directly, it is better to useInvalidateRgn
to trigger a repaint. This will cause aWM_PAINT
to be sent which will be handled by your application in the normal way. As a bonus, you can also tellInvalidateRgn
to erase the background for you