GDI+重新绘制 Vista 时没有清除我的窗口

发布于 2024-09-01 00:54:36 字数 1071 浏览 4 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫卆 2024-09-08 00:54:36

当窗口大小调整时,您显式调用 paint() 函数。但是,您的窗口并未失效,因此系统可能将您的绘制工作限制在标记为“脏”的区域。

与其直接调用 paint(),最好使用 InvalidateRgn 触发重绘。这将导致发送WM_PAINT,该消息将由您的应用程序以正常方式处理。作为奖励,您还可以告诉 InvalidateRgn 为您擦除背景

InvalidateRgn(hWnd, NULL, TRUE);

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 use InvalidateRgn to trigger a repaint. This will cause a WM_PAINT to be sent which will be handled by your application in the normal way. As a bonus, you can also tell InvalidateRgn to erase the background for you

InvalidateRgn(hWnd, NULL, TRUE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文