恼火!高分求助用CreateFontIndirect自定义字体时字体尺寸的问题!
本帖最后由 yxr_2008 于 2011-11-06 12:57:06 编辑 因为我要在PictureBox中显示纵向的文字,所以用CreateFontIndirect自定义字体后用TextOut显示上去,核心代码如下:
Private Sub ShowStr(nObj As Object, nStr As String, ByVal X As Long, ByVal Y As Long, Optional J As Single)
'在对象 nObj 上用自定义字体显示字符串 nStr
'nObj:可以是 Form,PictureBox 等拥有 hdc 属性的对象
'J:旋转角度
Dim mFont As Long, nFont As Long, LogF As LOGFONT, s As Long
Dim X1 As Long, Y1 As Long
'设置自定义字体的属性
LogF.lfEscapement = J * 10 '旋转角度:单位为 1/10 度,所以要乘10
LogF.lfHeight = nObj.TextHeight("A"): LogF.lfWidth = nObj.TextWidth("A")
LogF.lfWeight = nObj.Font.Weight '字符的权重:默认400,粗体和斜粗体是 700。设置值>550 转换为700,否则转换为400
LogF.lfFaceName = nObj.Font.Name & vbNullChar '字体名
LogF.lfItalic = nObj.Font.Italic '斜体
LogF.lfUnderline = nObj.Font.UnderLine '下划线
LogF.lfStrikeOut = nObj.Font.Strikethrough '删除线
LogF.lfCharSet = nObj.Font.Charset '字符集
s = CreateFontIndirect(LogF) '用 LogF 创建一种逻辑字体 nFont
nFont = SelectObject(nObj.hdc, s) '将字体 mFont 选入到对象 nObj
s = LenB(StrConv(nStr, vbFromUnicode)) '中英混合字符串字节数
Zhuan1 ctP180 - J / 180 * ctP180, X, Y, X, Y - LogF.lfHeight * 0.5, X1, Y1 'x1, y1 返回旋转后字符串左上角坐标
TextOut nObj.hdc, X1, Y1, nStr, s '显示字符串
s = SelectObject(nObj.hdc, nFont) '选中字体 nFont
DeleteObject s '删除字体 nFont,即:恢复对象的原字体
End Sub