跳到主要内容

文本框(Text)

文本框控件可用于显示文本,其宽高自动跟随文本,可以利用其样式变体指定样式。

在 HiEasyX 中,文本框及其变体的原型函数定义如下:

void Text(const HXString &Title, bool AutoAlign = true);
void Text(const HXString &Title, TextProfile &Profile, bool AutoAlign = true);

参数

对于原文本框控件

void Text(const HXString &Title, bool AutoAlign = true);
参数含义默认取值
Title文本框显示的文字
AutoAlign是否开启文本同行自动对齐,true 为开启,false 为关闭false

对于文本框控件的样式变体

void Text(const HXString &Title, TextProfile &Profile, bool AutoAlign = true);
参数含义默认取值
Title文本框显示的文字
Profile文本框的样式资料结构体
AutoAlign是否开启文本同行自动对齐,true 为开启,false 为关闭false

自动对齐

对于没有开启自动对齐的文本框,文本会按照基线(baseline)排列,这会导致文本和其他控件看起来并不是很协调,就像这样:

但是自动对齐只会和文本框前方的控件对齐,而不会考虑后方的控件,因此自动对齐不总是完美的,但是能够胜任大部分场景。

资料结构体

对于样式变体的文本框资料结构体定义如下:

struct TextProfile {
HXFont Font;
HXGInt Height = 18;
HXColor Color;

TextProfile();
};
变量含义默认取值
Font文本框的字体样式参见对象 - 字体样式对象
Height文本框的字体高度18
Color文本框的字体颜色白色({ 255, 255, 255, 255 }

示例

该示例代码同样可以在代码仓库/example/EasyX/Text.cpp 中找到。

#include <include/hex.h>
#include <include/impl/EasyX/hex_impl_easyx.h>

int main() {
initgraph(1240, 480);

BeginBatchDraw();

HX::HXInitForEasyX();
HX::SetBuffer(GetWorkingImage());

setbkcolor(RGB(180, 180, 180));

HX::WindowProfile windowProfile;
windowProfile.Size = {1200, 400};

while (true) {
cleardevice();

HX::HXBegin();

ExMessage message{};
while (peekmessage(&message)) {
HX::PushMessage(HX::GetHXMessage(&message));
}

HX::Window(HXStr("文本框控件示例"), windowProfile);

HX::BeginSameLine();
HX::Text(HXStr("普通文本,宽高自动调整"));
HX::Text(HXStr("普通文本,宽高自动调整"));
HX::Text(HXStr("普通文本,宽高自动调整"));
HX::Text(HXStr("普通文本,宽高自动调整"));
HX::EndSameLine();

HX::BeginSameLine();
HX::TextProfile styledFont;
styledFont.Font.Family = "SongTi";
styledFont.Font.Style = HXFontStyle::Bold;
styledFont.Height = 30;

HX::Text(HXStr("样式文本变体,宽高自动调整"), styledFont);

styledFont.Height = 20;

HX::Text(HXStr("样式文本变体,宽高自动调整"), styledFont);
HX::Text(HXStr("普通文本,宽高自动调整"));
HX::Text(HXStr("普通文本,宽高自动调整"));
HX::EndSameLine();

styledFont.Color = HXColor{ 255, 0, 0, 255 };

HX::BeginSameLine();

styledFont.Height = 30;
HX::Text(HXStr("样式文本变体,宽高自动调整,关闭自动对齐"), styledFont, false);
styledFont.Height = 20;
HX::Text(HXStr("样式文本变体,宽高自动调整,关闭自动对齐"), styledFont, false);
HX::Text(HXStr("普通文本,宽高自动调整,关闭自动对齐"), false);
HX::Text(HXStr("普通文本,宽高自动调整,关闭自动对齐"), false);
HX::EndSameLine();

HX::End();
HX::Render();

FlushBatchDraw();

Sleep(1);
}

return 0;
}