dim r as rect
dim rt as long
rt=drawtext(pic.hdc,"........",-1,r,DT_CALCRECT)drawtext并没有给r赋值,rt为35,真是奇怪,在MSDN里查了,没有我碰到的这种情况。要怎么样才计算字符串并把高度值给r?
dim rt as long
rt=drawtext(pic.hdc,"........",-1,r,DT_CALCRECT)drawtext并没有给r赋值,rt为35,真是奇怪,在MSDN里查了,没有我碰到的这种情况。要怎么样才计算字符串并把高度值给r?
DT_CALCRECT 的说明
象下面这样计算格式化矩形:多行绘图时矩形的底边根据需要进行延展,以便容下所有文字;单行绘图时,延展矩形的右侧。(注意这句)不描绘文字。由lpRect参数指定的矩形会载入计算出来的值详细的看下面的程序:
一个例子:(文字滚动的,可试着改变Picture1.FontSize = 14这句,看看效果)
Option ExplicitPrivate Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long
Const DT_BOTTOM As Long = &H8
Const DT_CALCRECT As Long = &H400
Const DT_CENTER As Long = &H1
Const DT_WORDBREAK As Long = &H10
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Const scrolltext As String = "滚动字幕示例" & vbCrLf & vbCrLf & "演示DrawText函数的使用"
Dim isend As BooleanPrivate Sub Command1_Click()
If isend = False Then
isend = True
Else
isend = False
Scroll_Text
End If
End SubPrivate Sub Form_Activate()
Scroll_Text
End SubPrivate Sub Form_Load()
Picture1.ScaleMode = 3
Picture1.AutoRedraw = True
Picture1.ForeColor = vbGreen
Picture1.FontSize = 14
isend = False
End SubPrivate Sub Scroll_Text()
Dim LastFrameTime As Long
'设置时间间隔,即滚动速度
Const IntervalTime As Long = 10
Dim rt As Long
Dim DrawingRect As RECT
'设置所画矩形的左边位置。
Dim tmpX As Long, tmpY As Long
Dim RectHeight As Long
'显示窗体
'frmAbout.Refresh
'获得所画矩形的尺寸
rt = DrawText(Picture1.hdc, scrolltext, -1, DrawingRect, DT_CALCRECT)
If rt = 0 Then
MsgBox "出错", vbExclamation
isend = True
Else
'设置矩形的位置
DrawingRect.Top = Picture1.ScaleHeight
DrawingRect.Left = 0
DrawingRect.Right = Picture1.ScaleWidth
'设置矩形的高度
RectHeight = DrawingRect.Bottom
DrawingRect.Bottom = DrawingRect.Bottom + Picture1.ScaleHeight
End If
Do While Not isend
If GetTickCount() - LastFrameTime > IntervalTime Then
Debug.Print "yes"
Picture1.Cls
DrawText Picture1.hdc, scrolltext, -1, DrawingRect, DT_CENTER Or DT_WORDBREAK
DrawingRect.Top = DrawingRect.Top - 1
DrawingRect.Bottom = DrawingRect.Bottom - 1
'控制文本的循环滚动
If DrawingRect.Top < -(RectHeight) Then
DrawingRect.Top = Picture1.ScaleHeight
DrawingRect.Bottom = RectHeight + Picture1.ScaleHeight
End If
Picture1.Refresh
LastFrameTime = GetTickCount()
End If
DoEvents
LoopEnd SubPrivate Sub Form_Unload(Cancel As Integer)
isend = True
End
End Sub
之后,r会传回你需要的数据:Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long
Const DT_BOTTOM As Long = &H8
Const DT_CALCRECT As Long = &H400
Const DT_CENTER As Long = &H1
Const DT_WORDBREAK As Long = &H10
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End TypePrivate Sub Command1_Click()
Dim r As RECT
Dim rt As Long
rt = DrawText(Picture1.hdc, "123", -1, r, DT_CALCRECT)
'MsgBox r.Top
'MsgBox r.Bottom
MsgBox r.Bottom - r.Top
'MsgBox r.Right
'MsgBox r.Left
MsgBox r.Right - r.LeftEnd SubPrivate Sub Form_Load()
Picture1.ScaleMode = 3
Picture1.AutoRedraw = True
Picture1.ForeColor = vbGreen
Picture1.FontSize = 14
End Sub
下面就简单了,矩形的右边-左边,下边-上边就行了
//rt = DrawText(Picture1.hdc, "123", -1, r, DT_CALCRECT)
//之后,r会传回你需要的数据:
我知道,但是我调用drawtext的时候,r并没有传回我要的值。
r.top=0
r.left=0
r.right=0
r.bottom=0
所以好奇怪,我已经不是第一次碰到了。