函数本体:
void TextInRect(LPCTSTR str, int x, int y, int width, int height, bool horizontal = true, bool vertical = true) {
LOGFONT font;
gettextstyle(&font);
int textHeight = font.lfHeight;
int textWidth = textHeight;
int strWidth = 0;
int strNum = lstrlen(str);
for (int i = 0; i < strNum; ++i)
strWidth += (str[i] > 127) ? textHeight : textHeight / 2;
if (strWidth >= width || textHeight >= height) {
outtextxy(x, y, str);
return;
}
if (horizontal)
x += (width - strWidth) / 2;
if (vertical)
y += (height - textHeight) / 2;
outtextxy(x, y, str);
}
使用例子:
fillrectangle(5, 5, 10 + 80, 5 + 40);
TextInRect("现实", 5, 5, 80, 40);
结果图