如何获取一串文本的高度和长度的像素值
使用TextOut向picturebox输出文本串后,想在文本四周加上矩形边框,但不知如何取得文本的高度和长度?
输出文本时用到了LOGFONT结构。
[解决办法]
GetTextExtentPoint32
[解决办法]
TextHeight 方法
TextWidth 方法
来用以返回按 Form,PictureBox 或 Printer 的当前字体被打印的文本字符串的高度和宽度。
代码实例:
Private Sub Form_Click () Dim HalfHeight, HalfWidth, Msg ' 声明变量。 AutoRedraw = -1 ' 打开AutoRedraw。 BackColor = QBColor(4) ' 设置背景颜色。 ForeColor = QBColor(15) ' 设置前景颜色。 Msg = "Visual Basic" ' 创建信息。 FontSize = 48 ' 设置字体大小。 HalfWidth = TextWidth(Msg) / 2 ' 计算半宽。 HalfHeight = TextHeight(Msg) / 2 ' 计算半高。 CurrentX = ScaleWidth / 2 - HalfWidth ' 设置 X。 CurrentY = ScaleHeight / 2 - HalfHeight ' 设置Y。 Print Msg ' 打印信息。End Sub