Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Sep 23, 1999

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 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 BS_LEFT As Long = &H100
Private Const BS_RIGHT As Long = &H200
Private Const BS_CENTER As Long = &H300
Private Const BS_TOP As Long = &H400
Private Const BS_BOTTOM As Long = &H800
Private Const BS_VCENTER As Long = &HC00

Private Const BS_ALLSTYLES = BS_LEFT Or BS_RIGHT _
	Or BS_CENTER Or BS_TOP Or BS_BOTTOM Or _
	BS_VCENTER

Private Const GWL_STYLE& = (-16)

Public Enum bsHorizontalAlignments
	bsLeft = BS_LEFT
	bsRight = BS_RIGHT
	bsCenter = BS_CENTER
End Enum

Public Enum bsVerticalAlignments
	bsTop = BS_TOP
	bsBottom = BS_BOTTOM
	bsVCenter = BS_VCENTER
End Enum

Public 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.Refresh
End Sub
Sam Huggill
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date