MSMVPS.COM
The Ultimate Destination for Blogs by Current and Former Microsoft Most Valuable Professionals.

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

 


Posted Nov 04 2003, 03:06 PM by ch21st

Comments

ch21st wrote re: Make the text of a textbox to align middle vertical in VB
on 09-03-2004 14:49
ch21st wrote re: Make the text of a textbox to align middle vertical in VB
on 09-24-2004 2:29

Add a Comment

(required)  
(optional)
(required)  
Remember Me?


Copyright © is the original authors. Blog site is an independent site not sponsored by Microsoft. The Yoda blog server and the Brianna SQL server would like to thank www.ownwebnow.com and www.exchangedefender.com. They wouldn't be here and broadcasting without the generosity of Vlad Mazek and his companies.

Powered by Community Server (Commercial Edition), by Telligent Systems