C++ WinAPI TextOut() 更新文本

标签 c++ windows winapi wm-paint textout

我正在使用 WinAPI 创建一个 Windows 应用程序。在处理窗口的 WM_PAINT 消息时,我正在使用 TextOut() 函数向用户显示更新的文本。

case WM_PAINT:
{
     PAINTSTRUCT ps;
     HDC hdc;
     hdc = BeginPaint(hwnd, &ps);
     SelectObject(hdc, hfDefault);

     // display the user data in the window
     TextOut(hdc,10,70, "Points: 0", 9);
     TextOut(hdc,10,85, "Level: 0", 8);

     // ...
     EndPaint(hwnd, &ps);
}
break;

如何在调用函数后更改 TextOut() 打印的文本以及确定打印文本长度的最后一个参数?

我发现的关于使用 TextOut() 的一切都是关于文本字体的。

最佳答案

也许是这样的......

// I'll assume hwnd is global
void OnSomeActionToRefreshValues()
{
    HDC hdc = ::GetDc(hwnd);
    DrawValues(hdc, 88, 99);
    ReleaseDC(hdc);
}

void DrawValues(HDC hdc, int points, int level)
{
    // Might need a rectangle here to overwrite old text
    SelectObject(hdc, hfDefault);    // I assume hfDefault is global
    TCHAR text[256];
    swprintf_s(text, 256, L"Points: %d", points);
    TextOut(hdc, 10, 70, text, wcslen(text));
    swprintf_s(text, 256, L"Level: %d", level);
    TextOut(hdc, 10, 85, text, wcslen(text));
}

在你赢得过程中:

case WM_PAINT:
    PAINTSTRUCT ps;
    HDC hdc;
    hdc = BeginPaint(hwnd,&ps);
    DrawValues(hdc, 88, 99);
    EndPaint(hwnd,&ps);
    break;

关于C++ WinAPI TextOut() 更新文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22050749/

相关文章:

c++ - 在 Quick FIX 4.2 中自定义实现多边期权定单

c# - 如何启动一个进程并重定向 stdin,但不重定向 stdout?

windows - 如何以编程方式判断 Windows PE 文件是控制台子系统还是 Windows 子系统?

c++ - 文件大小比应有的大,添加了额外的新行

c++ - C : x86 Intel Intrinsics usage of _mm_log2_ps() -> error: incompatible type 'int' ?

c++ - 从此指针复制后尝试访问基类的成员时出现段错误

c++ - Qt5 应用程序不运行

c++ - 字符串 vector 的高效组合

c++ - 将参数传递给 Windows 中的窗口调用

.net - 如何在 C++ 中存储 IP 地址?