1

我正在尝试在WM_COMMAND案例中打印文本,因为我需要在按下按钮后打印文本。

这是我的代码:

switch(msg)
{
default:
    return DefWindowProc(hwnd, msg, wParam, lParam);
case WM_COMMAND:
    switch (LOWORD(wParam))
    {
    case 1:
        PAINTSTRUCT ps;
        HDC         hDC;
        hDC = BeginPaint(hwnd, &ps);
        {
            TextOut(hDC, 10, 50, "hello", 5);
        }
        EndPaint(hwnd, &ps);
        UpdateWindow(hwnd);
        break;
    }
    break;
}

可悲的是它没有打印任何东西。

编辑:

我可以TextOut()WM_COMMAND这种情况下使用:

HDC         hDC;
hDC = GetDC(hwnd);
TextOut(hDC, 10, ypos, "Warnings: ", 10);
UpdateWindow(hwnd);
4

2 回答 2

7

最好对程序进行结构化,以便所有绘画都在 WM_PAINT 中执行。

因此您可以将其更改为:

LRESULT CALLBACK WndProc(/*blah blah blah*/) 
{
    static wchar_t my_text[] = L"hello";
    static BOOL show_btn_text = FALSE;
    HDC dc;
    PAINTSTRUCT ps;
    switch (msg) {
          case WM_COMMAND:
               switch (LOWORD(wParam)) {
                  case 1:
                       show_btn_text = !show_btn_text;
                       InvalidateRect(hwnd, NULL, TRUE); //tells windows that the whole client area needs to be repainted
                       break;
               }
               return 0;
           case WM_PAINT:
                dc = BeginPaint(hwnd, &ps);
                if (show_btn_text) {
                    TextOut(dc, 0, 0, my_text, wcslen(my_text));
                }
                EndPaint(hwnd, &ps);

            return 0;
            /*the rest of the window procedure
    }
}
于 2011-04-12T08:14:52.383 回答
5
  • BeginPaint用于在 WM_PAINT: /内绘制EndPaint
  • GetDC用于 WM_PAINT: /之外的绘画ReleaseDC
于 2011-04-12T08:10:25.673 回答