Align Caption On Command Button

'Add a module to your project (In the menu choose Project -> Add Module, Then click Open)
'Add 1 Command Button to your form.
'Insert this code to the module :

'Alignment constants
Public Const A_CENTER = &H300&
Public Const A_TOP = &H400&
Public Const A_TOPLEFT = &H500&
Public Const A_TOPRIGHT = &H600&
Public Const A_BOTTOM = &H800&
Public Const A_BOTTOMLEFT = &H900&
Public Const A_BOTTOMRIGHT = &HA00&
Public Const A_LEFT = &H100&
Public Const A_RIGHT = &H200&
Public Const GWL_STYLE& = (-16)
Declare Function GetWindowLong& Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long)
Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)

'Insert this code to your form:

Private Sub Command1_Click()
Dim tmpValue&
Dim Align&
Dim ret&
'This will align the caption to top. To align to different direction, change the 'A_TOP'
'below with different alignment constant. You can see all alignment constants above,
'in the module code.

fAlignment& = A_TOP
tmpValue& = GetWindowLong&(Command1.hwnd, GWL_STYLE) And Not BS_RIGHT
ret& = SetWindowLong&(Command1.hwnd, GWL_STYLE, tmpValue& Or fAlignment&)
Command1.Refresh
End Sub

Go Back