ボタンのキャプションの配置(Align)を設定する

VisualBasic6用
ボタンのキャプションの配置(Align)を設定する

' ボタンの文字の配置(Align)を設定する
'  *** この内容を標準モジュールに作成してください ***
' objCommandButton : ボタンオブジェクト
' ButtonStyle      : ボタンのキャプションの配置
'                    bsLeft:左寄せ / bsRight:右寄せ / bsCenter:中央寄せ
'                    bsTop:上寄せ  / bsBottom:下寄せ
Option Explicit

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)

Private Const BS_LEFT = &H100
Private Const BS_RIGHT = &H200
Private Const BS_CENTER = &H300
Private Const BS_TOP = &H400
Private Const BS_BOTTOM = &H800


Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
Private Const SM_CXBORDER = 5
Private Const SM_CYBORDER = 6
Private Const SM_CXEDGE = 45
Private Const SM_CYEDGE = 46

Public Enum ButtonAlignConstants
    bsLeft = BS_LEFT        ' 左寄せ
    bsRight = BS_RIGHT      ' 右寄せ
    bsCenter = BS_CENTER    ' 中央寄せ
    bsTop = BS_TOP          ' 上寄せ
    bsBottom = BS_BOTTOM    ' 下寄せ
End Enum

Public Sub SetButtonAlign(objCommandButton As CommandButton, ByVal ButtonStyle As ButtonAlignConstants)
    SetWindowLong objCommandButton.hwnd, GWL_STYLE, _
                  ButtonNoStyle(objCommandButton.hwnd) Or ButtonStyle
    objCommandButton.Refresh
End Sub

Private Function ButtonNoStyle(ByVal hwnd As Long) As Long
    Dim lngStyle    As Long
    lngStyle = GetWindowLong(hwnd, GWL_STYLE)
    If lngStyle And BS_LEFT Then lngStyle = lngStyle Xor BS_LEFT
    If lngStyle And BS_RIGHT Then lngStyle = lngStyle Xor BS_RIGHT
    If lngStyle And BS_CENTER Then lngStyle = lngStyle Xor BS_CENTER
    If lngStyle And BS_TOP Then lngStyle = lngStyle Xor BS_TOP
    If lngStyle And BS_BOTTOM Then lngStyle = lngStyle Xor BS_BOTTOM
    ButtonNoStyle = lngStyle
End Function