在 WM_COMMAND 中使用 TextOut() [英] Use TextOut() in WM_COMMAND

查看:31
本文介绍了在 WM_COMMAND 中使用 TextOut()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to print a text in the WM_COMMAND case, because I need a text to be printed after a button was pushed.

这是我的代码:

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;
}

遗憾的是它没有打印任何内容.

Sadly it doesn't print anything.

我可以在 WM_COMMAND 期间使用 TextOut() 那样:

I can use TextOut() during WM_COMMAND that way:

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

推荐答案

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

It's best to structure your program so that all painting is performed in WM_PAINT.

所以你可以把它改成这样:

so you could change it to be something like:

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
    }
}

这篇关于在 WM_COMMAND 中使用 TextOut()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆