DrawText只显示第一次调用
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的第二个矩形无效(
bottom
应该是120
而不是20
,因为它是实际的底部坐标,而不是高度)。另外,您必须在调用EndPaint()
之前渲染这两个字符串:最后,顺便说一句,您可能不想将所有代码留在窗口过程的
case
中代码> 语句。考虑将其移至其自己的函数中以提高可读性(和可维护性)。Your second rectangle is not valid (
bottom
should be120
instead of20
because it's the actual bottom coordinate, not the height). Also, you have to render both strings before callingEndPaint()
: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).首先,你的
bottom
坐标位于你的top
坐标之上,这是故意的吗?然后,您应该为收到的每个
WM_PAINT
调用一次BeginPaint
/EndPaint
。它通常是这样的:First of all, your
bottom
coordinate is over yourtop
one, is that intentional?Then, you should call
BeginPaint
/EndPaint
just once for eachWM_PAINT
you receive. It typically goes like this:“底部”正是矩形的底部。你使用它就好像它是高度一样。
"bottom" is exactly that, the bottom of the rect. You're using it as if it was the height.
在我看来 serverLabel.bottom = 20;应该是 serverLabel.bottom = 120;
Seems to me that serverLabel.bottom = 20; should be serverLabel.bottom = 120;