DrawText只显示第一次调用

发布于 2024-10-18 07:31:31 字数 1039 浏览 3 评论 0原文

我在 Win32 程序中使用 DrawText 函数在屏幕顶部中心显示“Local”,在中心显示“Server”。当我运行该程序时,它显示“本地”,但不显示“服务器”。这是我的消息循环中的代码:

case WM_PAINT:
        {
            RECT localLabel;
            localLabel.left = 0;
            localLabel.top = 0;
            localLabel.right = 270;
            localLabel.bottom = 20;
            PAINTSTRUCT localPs;
            HDC localHandle = BeginPaint(hwnd, &localPs);
            DrawText(localHandle, "Local", -1, &localLabel, DT_CENTER);
            EndPaint(hwnd, &localPs);

            PAINTSTRUCT serverPs;
            RECT serverLabel;
            serverLabel.left = 0;
            serverLabel.top = 100;
            serverLabel.right = 270;
            serverLabel.bottom = 20;
            HDC serverHandle = BeginPaint(hwnd, &serverPs);
            DrawText(serverHandle, "Server", -1, &serverLabel, DT_CENTER);
            EndPaint(hwnd, &serverPs);
        }
        break;

我尝试使用相同的 PAINTSTRUCT 但这没有帮助。我尝试使用相同的 HDC,但这也没有帮助。如何才能将两者同时显示在屏幕上?

谢谢。

I'm using the DrawText function in a Win32 program to display "Local" at the top center of the screen and "Server" in the center. When I run the program it displays "Local" but not "Server". Here is the code in my message loop:

case WM_PAINT:
        {
            RECT localLabel;
            localLabel.left = 0;
            localLabel.top = 0;
            localLabel.right = 270;
            localLabel.bottom = 20;
            PAINTSTRUCT localPs;
            HDC localHandle = BeginPaint(hwnd, &localPs);
            DrawText(localHandle, "Local", -1, &localLabel, DT_CENTER);
            EndPaint(hwnd, &localPs);

            PAINTSTRUCT serverPs;
            RECT serverLabel;
            serverLabel.left = 0;
            serverLabel.top = 100;
            serverLabel.right = 270;
            serverLabel.bottom = 20;
            HDC serverHandle = BeginPaint(hwnd, &serverPs);
            DrawText(serverHandle, "Server", -1, &serverLabel, DT_CENTER);
            EndPaint(hwnd, &serverPs);
        }
        break;

I tried using the same PAINTSTRUCT but that didn't help. I tried using the same HDC but that didn't help either. How can I display both on the screen?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

倾听心声的旋律 2024-10-25 07:31:31

您的第二个矩形无效(bottom 应该是 120 而不是 20,因为它是实际的底部坐标,而不是高度)。另外,您必须在调用 EndPaint() 之前渲染这两个字符串:

PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);

RECT localLabel;
localLabel.left = 0;
localLabel.top = 0;
localLabel.right = 270;
localLabel.bottom = 20;
DrawText(hdc, "Local", -1, &localLabel, DT_CENTER);

RECT serverLabel;
serverLabel.left = 0;
serverLabel.top = 100;
serverLabel.right = 270;
serverLabel.bottom = 120;
DrawText(hdc, "Server", -1, &serverLabel, DT_CENTER);

EndPaint(hwnd, &ps);

最后,顺便说一句,您可能不想将所有代码留在窗口过程的 case 中代码> 语句。考虑将其移至其自己的函数中以提高可读性(和可维护性)。

Your second rectangle is not valid (bottom should be 120 instead of 20 because it's the actual bottom coordinate, not the height). Also, you have to render both strings before calling EndPaint():

PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);

RECT localLabel;
localLabel.left = 0;
localLabel.top = 0;
localLabel.right = 270;
localLabel.bottom = 20;
DrawText(hdc, "Local", -1, &localLabel, DT_CENTER);

RECT serverLabel;
serverLabel.left = 0;
serverLabel.top = 100;
serverLabel.right = 270;
serverLabel.bottom = 120;
DrawText(hdc, "Server", -1, &serverLabel, DT_CENTER);

EndPaint(hwnd, &ps);

Finally, as an aside, you probably don't want to leave all that code in one of your window procedure's case statements. Consider moving it into its own function to improve readability (and maintainability).

烟若柳尘 2024-10-25 07:31:31

首先,你的bottom坐标位于你的top坐标之上,这是故意的吗?

然后,您应该为收到的每个 WM_PAINT 调用一次 BeginPaint/EndPaint。它通常是这样的:

case WM_PAINT:
{
    PAINTSTRUCT ps;
    HDC localHandle = BeginPaint(hwnd, &ps);
    // do *all* the drawing
    EndPaint(hwnd, &ps);
}
break;

First of all, your bottom coordinate is over your top one, is that intentional?

Then, you should call BeginPaint/EndPaint just once for each WM_PAINT you receive. It typically goes like this:

case WM_PAINT:
{
    PAINTSTRUCT ps;
    HDC localHandle = BeginPaint(hwnd, &ps);
    // do *all* the drawing
    EndPaint(hwnd, &ps);
}
break;
尘世孤行 2024-10-25 07:31:31

“底部”正是矩形的底部。你使用它就好像它是高度一样。

serverLabel.bottom = serverLabel.top + 20;

"bottom" is exactly that, the bottom of the rect. You're using it as if it was the height.

serverLabel.bottom = serverLabel.top + 20;
ゞ花落谁相伴 2024-10-25 07:31:31

在我看来 serverLabel.bottom = 20;应该是 serverLabel.bottom = 120;

Seems to me that serverLabel.bottom = 20; should be serverLabel.bottom = 120;

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