Question

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.

Was it helpful?

Solution

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).

OTHER TIPS

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;

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

serverLabel.bottom = serverLabel.top + 20;

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top