在Windows中显示多色文本的最快方法 [英] Fastest way to show multicolor text in Windows

查看:129
本文介绍了在Windows中显示多色文本的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个非常简单的应用程序,它可以显示随机形状和文本.我希望文本具有不同的颜色和字体类型.现在,我正在使用Direct2D创建矩形和文本.不幸的是,诸如d2d1WriteFactory之类的Direct2D库似乎不如GDI DrawText函数那么快.

例如,使用Direct2D,您将执行以下操作来创建字符数组.请注意,有两种方法可以执行此操作,即使用一种称为TextLayout的方法或另一种称为DrawText的函数.

I am developing a very simple application which displays random shapes and text. I want the text to have different colors, and font types. Right now I am using Direct2D to create rectangles and the text. Unfortunately, the Direct2D libraries such as d2d1WriteFactory don''t seem to be as fast as the GDI DrawText function.

For example, using Direct2D, you would do something like the following to create an array of characters. Note that there are 2 ways to do this, using something called a TextLayout, or a different function called DrawText.

bool USE_TEXT_LAYOUT = true;
p_textColor->SetColor(font_color);
p_dWriteFactory->CreateTextFormat(
    font_name,
    NULL,
    DWRITE_FONT_WEIGHT_NORMAL,
    DWRITE_FONT_STYLE_NORMAL,
    DWRITE_FONT_STRETCH_NORMAL,
    font_size,
    L"", //locale
    &p_textFormat
    );

if (USE_TEXT_LAYOUT){
    int width  = text_position.right - text_position.left;
    int height = text_position.bottom - text_position.top;
    p_dWriteFactory->CreateTextLayout(
        char_array,      // The string to be laid out and formatted.
        char_array_size,  // The length of the string.
        p_textFormat,  // The text format to apply to the string (contains font information, etc).
        width,         // The width of the layout box.
        height,        // The height of the layout box.
        &p_textLayout  // The IDWriteTextLayout interface pointer.
        );
    p_renderTarget->DrawTextLayout(
        D2D1::Point2F(text_position.left, text_position.bottom),
        p_textLayout,
        p_textColor,
        D2D1_DRAW_TEXT_OPTIONS_NONE);
}
else {
    p_renderTarget->DrawText(
        char_array,
        char_array_size - 1,
        //ARRAYSIZE(char_array) - 1,
        p_textFormat,
        text_position),
        p_textColor);
}



另一方面,如果您要坚持使用GDI库,则可以执行以下似乎更快的操作:



On the other hand, if you want to stick to GDI libraries, you would do the following which seems to be faster:

// Set the character.
int char_num   = int(255*rand()/RAND_MAX);
WCHAR text_char[2];
//text_char[0] = (WCHAR)G_RAMP[char_num];
text_char[0] = (WCHAR)char_num;
text_char[1] = 0;

// Get the font width and height.
G_LOG_FONT.lfHeight = font_height;
G_LOG_FONT.lfWidth  = font_width;
StringCchCopy(G_LOG_FONT.lfFaceName, 9, font_name);

// Set the color.
int font_red_byte   = int(255*rand()/RAND_MAX);
int font_green_byte = int(255*rand()/RAND_MAX);
int font_blue_byte  = int(255*rand()/RAND_MAX);
int font_size       = max(font_width, font_height);
DWORD font_color    = RGB(font_red_byte, font_green_byte, font_blue_byte);

COLORREF text_color       = font_color;
COLORREF background_color = RGB(font_green_byte, font_red_byte, font_blue_byte);
SetTextColor(h_dc, text_color);
SetBkColor(h_dc, background_color);
SetBkMode(h_dc, TRANSPARENT);

// Set the font.
G_H_FONT = CreateFontIndirect(&G_LOG_FONT);
SelectObject(h_dc, G_H_FONT);

DrawText(h_dc, (LPCWSTR)text_char, -1, &position, DT_NOCLIP |
    DT_NOPREFIX | DT_CALCRECT | DT_CENTER | DT_VCENTER);
DrawText(h_dc, (LPCWSTR)text_char, -1, &position, DT_NOPREFIX |
    DT_NOCLIP | DT_CENTER | DT_VCENTER);
DeleteObject(G_H_FONT);



有谁知道其他比显示这些功能更快的方法来显示多色字符串吗?



Does anyone know any other approach that would be faster for displaying multicolor strings than these functions?

推荐答案

使用WPF.
设置ForeColor属性.
Use WPF.
Set ForeColor Property.


这篇关于在Windows中显示多色文本的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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