函数本体:
void TextInRect(LPCTSTR str, int x, int y, int padding = 5) {
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;
int rectWidth = strWidth + padding * 2;
int rectHeight = textHeight + padding * 2;
fillrectangle(x, y, x + rectWidth, y + rectHeight);
outtextxy(x + padding, y + padding, str);
}
使用例子:
TextInRect("文本内容", 5, 5);
结果图