自定义TextBox控件对齐方式为为垂直居中

Private 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_GETRECT = &HB2
Private Const EM_SETRECTNP = &HB4
Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type

Private Function SetVCenterText(userdefineTextBox As TextBox)

   Dim rc As RECT

   Dim UserControlTop As Long

   Dim UserControlBottom As Long

   If userdefineTextBox.MultiLine = False Then Exit Function

   Call SendMessage(userdefineTextBox.hwnd, EM_GETRECT, 0, rc)

   UserControlTop = ((rc.Bottom - rc.Top) - (UserControl.Parent.TextHeight("H") \ Screen.TwipsPerPixelY)) \ 2

   UserControlBottom = ((rc.Bottom - rc.Top) + (UserControl.Parent.TextHeight("H") \ Screen.TwipsPerPixelY)) \ 2

   rc.Top = UserControlTop

   rc.Bottom = UserControlBottom

   userdefineTextBox.Alignment = vbCenter

   Call SendMessage(userdefineTextBox.hwnd, EM_SETRECTNP, 0&, rc)

   userdefineTextBox.Refresh
End Function

Private Sub UserControl_Resize()
    Text1.Top = 0
    Text1.Left = 0
    Text1.Height = UserControl.Height
    Text1.Width = UserControl.Width
   
    SetVCenterText Text1
End Sub

posted on 2005-08-08 14:36  虫子  阅读(9639)  评论(0编辑  收藏  举报

导航