Shannon Shang-I think therefore I am

We came here, you and I, to this place and this profession, to be great, to do great things, and give form to great dreams - and we have

Make the text of a textbox to align middle vertical in VB

Use EM_GETRECT and EM_SETRECTNP  will come it true:

At fisrt put a commandbutton and  a TextBox to form and  set  multiLine property of that textbox to true.

then paste the following code to a fome:

Code:

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 Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

 

Private Const EM_SETRECT As Long = &HB3
Private Const EM_GETRECT As Long = &HB2
Private Const EM_SETRECTNP As Long = &HB4

Private Sub Command1_Click()

   Dim rc As RECT
   Dim tmpTop As Long
   Dim tmpBot As Long

   Text1.Text = "Centre this text"

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

   With Me.Font
      .Name = Text1.Font.Name
      .Size = Text1.Font.Size
      .Bold = Text1.Font.Bold
   End With

   tmpTop = ((rc.Bottom - rc.Top) - (Form1.TextHeight("H") \ Screen.TwipsPerPixelY)) \ 2
   tmpBot = ((rc.Bottom - rc.Top) + (Form1.TextHeight("H") \ Screen.TwipsPerPixelY)) \ 2

   rc.Top = tmpTop
   rc.Bottom = tmpBot

   Text1.Alignment = vbCenter

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

   Text1.Refresh

End Sub

 

Comments

ch21st said:

# September 3, 2004 2:49 PM

ch21st said:

# September 24, 2004 2:29 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)