dim r as rect
dim rt as long 
rt=drawtext(pic.hdc,"........",-1,r,DT_CALCRECT)drawtext并没有给r赋值,rt为35,真是奇怪,在MSDN里查了,没有我碰到的这种情况。要怎么样才计算字符串并把高度值给r?

解决方案 »

  1.   

    //drawtext并没有给r赋值,rt为35,真是奇怪,在MSDN里查了,没有我碰到的这种情况。要怎么样才计算字符串并把高度值给r奇怪什么,rt返回35说明函数执行成功(是没显示你想输出的字符吗?)
    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
      

  2.   

    //由lpRect参数指定的矩形会载入计算出来的值我知道函数是执行成功,但lpRect参数指定的矩形并没有载入计算出来的值,我要得到的它的值。
      

  3.   

    //我知道函数是执行成功,但lpRect参数指定的矩形并没有载入计算出来的值,我要得到的它的值我上面的程序你看了吗?rt = DrawText(Picture1.hdc, "123", -1, r, DT_CALCRECT)
    之后,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
      

  4.   

    r这个结构体是用byref引用的,执行drawText后,会把计算的到的适合的文字大小放在里面
    下面就简单了,矩形的右边-左边,下边-上边就行了
      

  5.   

    //我上面的程序你看了吗?
    //rt = DrawText(Picture1.hdc, "123", -1, r, DT_CALCRECT)
    //之后,r会传回你需要的数据:
    我知道,但是我调用drawtext的时候,r并没有传回我要的值。
    r.top=0
    r.left=0
    r.right=0
    r.bottom=0
    所以好奇怪,我已经不是第一次碰到了。
      

  6.   

    用我给的例子也无法显示的话,估计可能是系统的问题把你的程序发给我,帮你看看:[email protected]
      

  7.   

    在我以为一切都已成定局时,不经意间看到了墙角那一丝的希望。谢谢rainstormmaster(rainstormmaster)的帮助。我安装的是VB6+sp5.删了重新安装操作系统,这回装了个英文版的VB6,就不会发生那种情况了。是系统的问题,太可怕了,往往在路上绊脚的不是你所注意到的石头,而是那地上的小草。对于VB和API,我只能更加小心了。