//混合
auto fun_mixture = [](wstring tmp, wstring head, float fontsize) {
Gdiplus::Bitmap* img;
//head=L"订单号:";
tmp = head + tmp;
//设置字体大小,
FontFamily fontFamily(L"Arial");
Font font(&fontFamily, fontsize, FontStyleBold, UnitPoint);
StringFormat stringFormat;
stringFormat.SetAlignment(StringAlignmentNear);
stringFormat.SetLineAlignment(StringAlignmentNear);
float descent = fontFamily.GetCellDescent(FontStyleRegular);
float lineSpacing = fontFamily.GetLineSpacing(FontStyleRegular);
//下降
float descentPixel = font.GetSize() * descent / fontFamily.GetEmHeight(FontStyleRegular);
//行高
float lineSpacingPixel = font.GetSize() * lineSpacing / fontFamily.GetEmHeight(FontStyleRegular);
设置总高度
fontsize = descentPixel + lineSpacingPixel;
//float b = (float)WIDTH / (fontsize * 2 / 3);
float height = (((float)tmp.length() + head.length()) * (fontsize * 2 / 3)) / (float)WIDTH;
int int_h = height; //float
if (int_h < height) {
int_h++;
}
RectF* rectF = new RectF(0.0f, 0.0f, WIDTH, int_h * fontsize);
img = new Gdiplus::Bitmap(WIDTH, int_h * fontsize);
SolidBrush solidBrush(Color(0, 0, 0));
Graphics* graphics = new Graphics(img);
const Gdiplus::SizeF layout_size(rectF->Width, font.GetHeight(graphics));
Gdiplus::SizeF size;
int kMaxLineCount = tmp.length();
int kLineHeight = fontsize;
std::wstring remain_text = tmp;
for (unsigned int i = 0; i < kMaxLineCount; ++i) {
int char_count = 0; // 当前行能显示的字符个数
if (i >= int_h) {
Gdiplus::Bitmap* imgcp;
int_h++;
imgcp = new Gdiplus::Bitmap(WIDTH, (int_h)*fontsize);
delete graphics;
graphics = new Graphics(imgcp);
graphics->DrawImage(img, 0, 0, img->GetWidth(), img->GetHeight());
int a = imgcp->GetHeight();
delete img;
img = imgcp;
delete rectF;
rectF = new RectF(0.0f, 0.0f, WIDTH, int_h * fontsize);
}
Gdiplus::Status status = graphics->MeasureString(
remain_text.c_str(),
remain_text.length(),
&font,
layout_size,
&stringFormat,
&size,
&char_count,
NULL);
if (char_count > 0) {
// 截取当前行可以显示的文本并显示
std::wstring substr = remain_text.substr(0, char_count);
Gdiplus::RectF subrc(rectF->X, rectF->Y + i * kLineHeight, layout_size.Width, layout_size.Height);
graphics->DrawString(substr.c_str(), substr.length(), &font, subrc, &stringFormat, &solidBrush);
// 更新剩余文本信息
remain_text.erase(0, char_count);
if (remain_text.empty())
break;
} else {
break;
}
}
delete rectF;
delete graphics;
return img;
};