Dim LastLine As Long '最后的行数 Dim LineHeight '每行的高度Private Sub Form_Load() Set Me.Font = Text1.Font LineHeight = Me.TextHeight("TT") End Sub '查询行数,并及时修改 Private Sub Text1_Change() Dim Ret As Long Ret = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0, 0&) '取行数 If Ret <> LastLine Then If Text1.Height + Text1.Top + LineHeight > Me.ScaleHeight And _ Ret > 1 Then
If LastLine <= Ret - 1 Then Exit Sub '如果是最大高度,保持不变 End If LastLine = Ret - 1 '超过最大高度 Else LastLine = Ret End If Text1.Height = LastLine * LineHeight '修改高度 End If Debug.Print Me.ScaleHeight End Sub Private Sub Text1_GotFocus() LastLine = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0, 0&) End Sub
上面还要加 modoul1Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Any) As Long Public Const EM_GETLINECOUNT = &HBA
Dim LineHeight '每行的高度Private Sub Form_Load()
Set Me.Font = Text1.Font
LineHeight = Me.TextHeight("TT")
End Sub
'查询行数,并及时修改
Private Sub Text1_Change()
Dim Ret As Long
Ret = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0, 0&) '取行数
If Ret <> LastLine Then
If Text1.Height + Text1.Top + LineHeight > Me.ScaleHeight And _
Ret > 1 Then
If LastLine <= Ret - 1 Then
Exit Sub
'如果是最大高度,保持不变
End If
LastLine = Ret - 1 '超过最大高度
Else
LastLine = Ret
End If
Text1.Height = LastLine * LineHeight '修改高度
End If
Debug.Print Me.ScaleHeight
End Sub
Private Sub Text1_GotFocus()
LastLine = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0, 0&)
End Sub
modoul1Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Public Const EM_GETLINECOUNT = &HBA