迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

在 C++ 中使用 TextOut() 更新文本

作者:迹忆客 最近更新:2023/08/31 浏览次数:

C++ 中的 TextOut() 函数使用选定的字体、背景颜色和文本颜色在指定位置写入字符串。 它属于`#include <wingdi.h>`。

在本文中,您将学习如何使用 C++ 中的 TextOut() 函数更新任何文本。

每次 C++ 程序为指定设备调用 TextOut() 函数时,使用和更新当前位置的一个有趣的想法是使用 SetTextAlign 函数,并使用设置为 TA_UPDATECP 的 fMode 参数(它将使您能够允许系统)。 wingdi.h 头文件将 TextOut() 函数定义为别名,可自动检测操作系统或处理器的 ANSI 或 Unicode 版本,以选择该函数的合适版本。


使用 C++ 中的 TextOut() 函数使用文本字符串的源来更新文本

TextOut() 函数可以有五个参数,包括 [in] hdc 来处理设备上下文,[in] x 表示 x 坐标,[in] y 表示 y 坐标,[in] IpString 作为指向 字符串,[in] c 定义字符串的长度。 如果这个函数完美执行,它会返回一个非零值; 否则,将产生 null 或 0。

此外,参考点的解释是成正比的并且依赖于C++程序的最新(当前)文本对齐模式。 但是,您可以通过调用 GetTextAlign 函数来检索当前的文本对齐模式。

//displaying text
#include <windows.h>

// define `#include <wingdi.h>` separately or the `windows.h` is an alternative

DWORD text_repaintcount = 0; // to handle the color_code of the primary text

// a call back to the pre-defined function defined in the `windows.h` header file
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // initialize the handle, `typedef UNIT`, `typedef UNIT_PTR`, and `typeof LONG_PTR`

int WINAPI WinMain(HINSTANCE obj_instance_hi, HINSTANCE hInstance_prevent, LPSTR lp_cmd_imp, int show_cmd_new)
{
    WNDCLASSEX obj_windclass;
    MSG obj_showmessage;
    obj_windclass.cbSize = sizeof(WNDCLASSEX);
    obj_windclass.style = 0;
    obj_windclass.lpfnWndProc = WndProc;
    obj_windclass.cbClsExtra = 0;
    obj_windclass.cbWndExtra = 0;
    obj_windclass.hInstance = obj_instance_hi;
    obj_windclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    obj_windclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    obj_windclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    obj_windclass.lpszMenuName = NULL;
    obj_windclass.lpszClassName = TEXT("New Window!");
    obj_windclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    RegisterClassEx(&obj_windclass);
    CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("New Window!"), TEXT("Text Output"), WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 850, 350, NULL, NULL, obj_instance_hi, NULL);

    while (GetMessage(&obj_showmessage, NULL, 0, 0) > 0)
    {
        TranslateMessage(&obj_showmessage);
        DispatchMessage(&obj_showmessage);
    }

    return obj_showmessage.wParam;
}

LRESULT CALLBACK WndProc(HWND obj_hwnd_pri, UINT obj_message_show, WPARAM obj_wparma_sec, LPARAM obj_iparma_sec)
{

    PAINTSTRUCT obj_paint_structure;
    HDC obj_HDC_pri;
    SIZE text_updated_size;
    TCHAR obj_string[100] = TEXT("\0");
    HFONT text_update_font;
    HBRUSH text_update_hbrush;
    TEXTMETRIC text_metric;

    int temp_y = 0;
    int temp_x = 0;
    int text_update_length;

    switch (obj_message_show)
    {
    case WM_PAINT: // it traps the paint message

        // the following outputs the text using the default font and other settings
        obj_HDC_pri = BeginPaint(obj_hwnd_pri, &obj_paint_structure);
        int obj_hDC_last;
        obj_hDC_last=SaveDC(obj_HDC_pri); // save the current operating device context
        text_update_length = wsprintf(obj_string, TEXT("Stock font"));
        TextOut(obj_HDC_pri, 0, temp_y, obj_string, text_update_length);

        // updating text using the `ANSI_FIXED_PONT` and outputting the text
        GetTextMetrics(obj_HDC_pri, &text_metric);
        temp_y = temp_y + text_metric.tmHeight;//calculate new vertical coordinate using text metric

        text_update_hbrush = (HBRUSH)GetStockObject(ANSI_FIXED_FONT);

        SelectObject(obj_HDC_pri, text_update_hbrush);
        text_update_length = wsprintf(obj_string, TEXT("ANSI_FIXED_FONT") );
        TextOut(obj_HDC_pri, 0, temp_y, obj_string, text_update_length);

        // changing the primary color of the text and producing it as an output
        GetTextMetrics(obj_HDC_pri, &text_metric);
        temp_y = temp_y + text_metric.tmHeight;

        COLORREF textcolor_new;
        textcolor_new = COLORREF RGB(255, 0, 0);

        SetTextColor(obj_HDC_pri, textcolor_new);
        text_update_length = wsprintf(obj_string, TEXT("ANSI_FIXED_FONT, color red") );
        TextOut(obj_HDC_pri, temp_x, temp_y, obj_string, text_update_length);

        // changing the background color of the text to blue and producing it as an output
        GetTextMetrics(obj_HDC_pri, &text_metric);
        temp_y = temp_y + text_metric.tmHeight;

        COLORREF text_update_backgroundcolor;
        text_update_backgroundcolor = COLORREF RGB(0, 0, 255);

        SetBkColor(obj_HDC_pri, text_update_backgroundcolor);
        text_update_length = wsprintf(obj_string, TEXT("ANSI_FIXED_FONT, color red with blue background") );
        TextOut(obj_HDC_pri, temp_x, temp_y, obj_string, text_update_length);

        // set the background of the text transparent and produce it as an output
        GetTextMetrics(obj_HDC_pri, &text_metric);
        temp_y = temp_y + text_metric.tmHeight;
        SetBkMode(obj_HDC_pri, TRANSPARENT);

        SelectObject(obj_HDC_pri, text_update_hbrush);
        text_update_length = wsprintf(obj_string, TEXT("ANSI_FIXED_FONT,color red transparent background") );
        TextOut(obj_HDC_pri, 0, temp_y, obj_string, text_update_length);

        // changing the font of the text to `Arial` and changing its size to output
        GetTextMetrics(obj_HDC_pri, &text_metric);
        temp_y = temp_y + text_metric.tmHeight;
        text_update_font = CreateFont(20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, TEXT("Ariel"));

        SelectObject(obj_HDC_pri, text_update_font);
        text_update_length = wsprintf(obj_string, TEXT("ANSI_FIXED_FONT, color red, transparent background,arial 20") );
        TextOut(obj_HDC_pri, 0, temp_y, obj_string, text_update_length);

        // use the saved value to restore the original font size, style, and other settings
        GetTextMetrics(obj_HDC_pri, &text_metric);
        temp_y = temp_y + text_metric.tmHeight;

        RestoreDC(obj_HDC_pri, obj_hDC_last);
        text_update_length = wsprintf(obj_string, TEXT("stock font restored using SaveDC/RestoreDC") );
        TextOut(obj_HDC_pri, temp_x, temp_y, obj_string, text_update_length);
        GetTextMetrics(obj_HDC_pri, &text_metric);
        temp_y = temp_y + text_metric.tmHeight; // calculating the vertical co-ordinates using the `text_metric`

        //changing font to `new roman` and size to `30` and producing an output
        int obj_text_baseline;// calculate the baseline of the font
        text_update_font = CreateFont(30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, TEXT("Times New Roman"));

        SelectObject(obj_HDC_pri, text_update_font);
        GetTextMetrics(obj_HDC_pri, &text_metric);
        obj_text_baseline = text_metric.tmAscent;
        text_update_length = wsprintf(obj_string, TEXT("times new roman 30 statement 1") );
        TextOut(obj_HDC_pri, temp_x, temp_y, obj_string, text_update_length);
        GetTextExtentPoint32(obj_HDC_pri, obj_string, text_update_length, &text_updated_size);
        temp_x = temp_x + text_updated_size.cx;

        // change the font into `courier new` and the size to `20` and output the updated text
        int text_changefont_baselinecourier;
        text_update_font = CreateFont(20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, TEXT("Courier New"));

        SelectObject(obj_HDC_pri, text_update_font);
        GetTextMetrics(obj_HDC_pri, &text_metric);
        text_changefont_baselinecourier = obj_text_baseline-text_metric.tmAscent;
        text_update_length = wsprintf(obj_string, TEXT("Courier statement ") );
        TextOut(obj_HDC_pri, temp_x, temp_y + text_changefont_baselinecourier, obj_string, text_update_length);
        GetTextExtentPoint32(obj_HDC_pri, obj_string, lstrlen(obj_string), &text_updated_size);
        temp_x = temp_x + text_updated_size.cx;

        // changing the font to `ariel` and size to `20` and output the updated text
        int text_fontchange_baselinearial;
        text_update_font = CreateFont(10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,TEXT( "Ariel"));

        SelectObject(obj_HDC_pri, text_update_font);
        GetTextMetrics(obj_HDC_pri, &text_metric);
        text_fontchange_baselinearial = obj_text_baseline-text_metric.tmAscent;
        text_update_length = wsprintf(obj_string, TEXT("ariel 10 statement 3 ") );
        TextOut(obj_HDC_pri, temp_x, temp_y + text_fontchange_baselinearial, obj_string, text_update_length);

        EndPaint(obj_hwnd_pri, &obj_paint_structure);
        DeleteDC(obj_HDC_pri);
        DeleteObject(text_update_font);
        DeleteObject(text_update_hbrush);
        break;

    case WM_CLOSE:
        DestroyWindow(obj_hwnd_pri);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(obj_hwnd_pri, obj_message_show, obj_wparma_sec, obj_iparma_sec);
    }

    return 0;
}

输出:

https://drive.google.com/file/d/1YUs55n-gw1rYgOrBce0RAuuET5UPdTLa/view?usp=sharing

在 Windows C++ 应用程序上处理 WM_PAINT 时,使用 TextOut() 函数声明文本字符串的源以显示更新的文本。 本教程中的 C++ 代码不能单独运行,但可以在适当的 Windows 中使用 Visual Studio 或组合使用 .dsp、.dsw、.ncb 和 .opt 文件的用户界面,以在 Windows 上显示适当的对话框。

Windows API 随着新版本的 Windows 发生变化,当前版本需要 L 修饰符,如 L"wchar_ttext 中。此 C++ 代码是在 Windows 10 上使用 Visual Studio 2019 进行测试的。

此外,使用 wWinMain()myRegisterClass()InitInstance()WndProc() 生成 Windows 桌面 GUI 应用程序的框架,并使用 HDC 中的字体来绘制文本,并使用 TextOut() 函数来绘制文本。 使用新更新的字体的文本。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便