Align Text on a Command Button

If you’ve ever wanted to align text on a command button and found you can do it only by using spaces in the caption, there are a couple of constants that can help you do this with the SetWindowLong API Call. Add this code to a standard BAS module, then call it at will, passing the command button and the desired combination of vertical and horizontal alignment values:

 Option ExplicitPrivate Declare Function GetWindowLong Lib _	"user32" Alias "GetWindowLongA" (ByVal hWnd _	As Long, ByVal nIndex As Long) As LongPrivate Declare Function SetWindowLong Lib _	"user32" Alias "SetWindowLongA" (ByVal hWnd _	As Long, ByVal nIndex As Long, ByVal _	dwNewLong As Long) As LongPrivate Const BS_LEFT As Long = &H100Private Const BS_RIGHT As Long = &H200Private Const BS_CENTER As Long = &H300Private Const BS_TOP As Long = &H400Private Const BS_BOTTOM As Long = &H800Private Const BS_VCENTER As Long = &HC00Private Const BS_ALLSTYLES = BS_LEFT Or BS_RIGHT _	Or BS_CENTER Or BS_TOP Or BS_BOTTOM Or _	BS_VCENTERPrivate Const GWL_STYLE& = (-16)Public Enum bsHorizontalAlignments	bsLeft = BS_LEFT	bsRight = BS_RIGHT	bsCenter = BS_CENTEREnd EnumPublic Enum bsVerticalAlignments	bsTop = BS_TOP	bsBottom = BS_BOTTOM	bsVCenter = BS_VCENTEREnd EnumPublic Sub AlignButtonText(cmd As CommandButton, _	Optional ByVal HStyle As _	bsHorizontalAlignments = bsCenter, _	Optional ByVal VStyle As _	bsVerticalAlignments = bsVCenter)	Dim oldStyle As Long	' get current style	oldStyle = GetWindowLong(cmd.hWnd, GWL_STYLE)	' clear existing alignment setting(s)	oldStyle = oldStyle And (Not BS_ALLSTYLES)	' set new style and refresh button	Call SetWindowLong(cmd.hWnd, GWL_STYLE, _		oldStyle Or HStyle Or VStyle)	cmd.RefreshEnd Sub
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: