'随着文本内容自动调整高度的文本框 '窗体上需要有一个名为Text1的文本框Option ExplicitPrivate Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Const EM_GETLINECOUNT = &HBA Private Const WM_GETFONT = &H31 Private Const EM_GETRECT = &HB2Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End TypePrivate Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As LongPrivate Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long Private Type TEXTMETRIC tmHeight As Long tmAscent As Long tmDescent As Long tmInternalLeading As Long tmExternalLeading As Long tmAveCharWidth As Long tmMaxCharWidth As Long tmWeight As Long tmOverhang As Long tmDigitizedAspectX As Long tmDigitizedAspectY As Long tmFirstChar As Byte tmLastChar As Byte tmDefaultChar As Byte tmBreakChar As Byte tmItalic As Byte tmUnderlined As Byte tmStruckOut As Byte tmPitchAndFamily As Byte tmCharSet As Byte End Type Private Declare Function GetTextMetrics Lib "gdi32" Alias "GetTextMetricsA" (ByVal hdc As Long, lptm As TEXTMETRIC) As Long Private Sub Text1_Change() With Text1 Dim dc As Long, tm As TEXTMETRIC, oft As Long, rct As RECT dc = GetDC(.hwnd): oft = SelectObject(dc, SendMessage(.hwnd, WM_GETFONT, 0&, ByVal 0&)) GetTextMetrics dc, tm: SelectObject dc, oft: ReleaseDC .hwnd, dc SendMessage .hwnd, EM_GETRECT, 0&, rct .Height = Me.ScaleY((tm.tmHeight) _ * SendMessage(.hwnd, EM_GETLINECOUNT, 0&, ByVal 0&) _ + 6, vbPixels, Me.ScaleMode) End With End Sub
'窗体上需要有一个名为Text1的文本框Option ExplicitPrivate Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const EM_GETLINECOUNT = &HBA
Private Const WM_GETFONT = &H31
Private Const EM_GETRECT = &HB2Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End TypePrivate Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As LongPrivate Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Type TEXTMETRIC
tmHeight As Long
tmAscent As Long
tmDescent As Long
tmInternalLeading As Long
tmExternalLeading As Long
tmAveCharWidth As Long
tmMaxCharWidth As Long
tmWeight As Long
tmOverhang As Long
tmDigitizedAspectX As Long
tmDigitizedAspectY As Long
tmFirstChar As Byte
tmLastChar As Byte
tmDefaultChar As Byte
tmBreakChar As Byte
tmItalic As Byte
tmUnderlined As Byte
tmStruckOut As Byte
tmPitchAndFamily As Byte
tmCharSet As Byte
End Type
Private Declare Function GetTextMetrics Lib "gdi32" Alias "GetTextMetricsA" (ByVal hdc As Long, lptm As TEXTMETRIC) As Long
Private Sub Text1_Change()
With Text1
Dim dc As Long, tm As TEXTMETRIC, oft As Long, rct As RECT
dc = GetDC(.hwnd): oft = SelectObject(dc, SendMessage(.hwnd, WM_GETFONT, 0&, ByVal 0&))
GetTextMetrics dc, tm: SelectObject dc, oft: ReleaseDC .hwnd, dc
SendMessage .hwnd, EM_GETRECT, 0&, rct
.Height = Me.ScaleY((tm.tmHeight) _
* SendMessage(.hwnd, EM_GETLINECOUNT, 0&, ByVal 0&) _
+ 6, vbPixels, Me.ScaleMode)
End With
End Sub