Tips mengatur posisi caption objek CommandButton

Posted by Kamarudin • 1 minute read • Comments

Salah satu objek standar VB 6 yang kurang beruntung adalah CommandButton :grin:, sejak lahirnya memang tidak mewarisi properties Alignment seperti saudaranya yg lainnya (ex. TextBox, Label, de el el) untungnya bisa memilih teman yg tepat dan bisa diandalkan :grin: siapa lagi kalo bukan fungsi API, sehingga kekurangan ini pun akhirinya bisa ditutupi.

Oke, tambahkan sebuah modul di project Anda, kemudian copy paste kode berikut :

Public Enum AlignButton
    A_CENTER = &H300&
    A_TOP = &H400&
    A_TOPLEFT = &H500&
    A_TOPRIGHT = &H600&
    A_BOTTOM = &H800&
    A_BOTTOMLEFT = &H900&
    A_BOTTOMRIGHT = &HA00&
    A_LEFT = &H100&
    A_RIGHT = &H200&
End Enum

Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_STYLE& = (-16)

Public Sub setAlignButton(ByVal btn As CommandButton, ByVal alignment As AlignButton)
    Dim ret     As Long

    ret = GetWindowLong(btn.hwnd, GWL_STYLE) And Not 0
    If ret Then
       ret = SetWindowLong(btn.hwnd, GWL_STYLE, ret Or alignment)
       If ret Then btn.Refresh
    End If
End Sub

Kemudian pada form tambahkan beberapa objek CommandButton dan ketik kode berikut pada event Form_Load :

Private Sub Form_Load()
    Call setAlignButton(Command1, A_LEFT)
    Call setAlignButton(Command2, A_TOP)
    Call setAlignButton(Command3, A_RIGHT)
    Call setAlignButton(Command4, A_BOTTOM)
End Sub

dan hasilnya

Selamat mencoba :blush:

Comments