Mega Code Archive

 
Categories / Delphi / Examples
 

Playing with style Part 4

Title: Playing with style - Part 4 Question: How do I change a the alignment of a button caption? Answer: Back again, still playing with style bits. Although the TCheckBox and TRadioButton owns an Alignment property, that property simply changes the position of the check or radio to the left or right of the caption; normally is not possible to change the text alignment of a TButton. Normally... Applying the same old technique of changing the style bits, you can use the BS_LEFT, BS_RIGHT or BS_CENTER bit to the styles of an existing button, check box or radio button. As the name suggests, these bits are used to change the horizontal text alignment. I emphasized the horizontal word because they do exist three more styles to vertically align the text in a button. These bits are BS_TOP, BS_BOTTOM and BS_VCENTER. Every button created in Windows have the BS_CENTER and BS_VCENTER styles set by default, CheckBoxes and RadioButtons have the BS_LEFT and BS_VCENTER styles set by default. Add this code to your application to experiment with the alignment styles: type THAlignment = (haLeft, haCenter, haRight); TVAlignment = (vaTop, vaCenter, vaBottom); ... implementation procedure HorizontalAlignment(theControl: TWinControl; theStyle: THAlignment); var dwStyle: Longint; const aStyles: array[THAlignment] of DWORD = (BS_LEFT, BS_CENTER, BS_RIGHT); begin dwStyle := GetWindowLong(theControl.handle, GWL_STYLE) or aStyles[theStyle]; SetWindowLong(theControl.Handle, GWL_STYLE, dwStyle); end; procedure VerticalAlignment(theControl: TWinControl; theStyle: TVAlignment); var dwStyle: Longint; const aStyles: array[TVAlignment] of DWORD = (BS_TOP, BS_VCENTER, BS_BOTTOM); begin dwStyle := GetWindowLong(theControl.handle, GWL_STYLE) or aStyles[theStyle]; SetWindowLong(theControl.Handle, GWL_STYLE, dwStyle); end; Also in this case you can derive a new button class to expose alignment properties, but this time I leave this as homework for the bravehearted ;DD Enjoy!