Extended Delphi Buttons
The Delphi TButton class does not allow multiline captions, or captions that contain mixed fonts, styles, sizes or colors. This is not such a big problem as it might seem at first sight - since TButton is a TWinControl descendant one should be able to simply insert labels inside the button and arrange them to obtain the desired effect.
Easy right? Well yes, except for the fact that the TButton constructor leaves out csAcceptsControls value from the ControlStyle set property. This being the case it is not possible to drag and drop other controls onto a TButton in the Delphi form designer. However, there is nothing to stop you from directly editing the .dfm file to have controls owned by an instance of TButton. Here are the steps:
- Create an instance of the TButton control by dragging it onto your form in the form designer.
- The button caption will never be seen so set it to a null string.
- Adjust the Height, Left, Top and Width properties of the button
- Create a temporary TPanel control and give it the same dimensions as your target button control.
- Now create two, or more, instances of the TLabel control inside this panel.
- For convenience, you should stack the labels vertically.
- Assign the Caption and Font properties of each label.
- Now set AutoSize :=false and Transparent := true for all the labels.
- Set Left := 5 and Top := 5 for the first label. Adjust the Width of all the labels so as to ensure a 5 pixel gap between the righthand border of the labels and their container.
- Adjust the Alignment, Height and Layout properties of each label and the Top property of all but the first label until you get the desired button caption layout.
- With these steps completed use Alt + F12 to view the .dfm file.
- Locate your label controls in the .dfm, cut them and move them inside the declaration for the button control created above - the best place is just before the end statement that terminates the button control definition.
- Use Alt + F12 to switch back to the form designer. The labels will now appear inside the button.
- Assign the OnClick event handler for each of the labels as you would normally have done for the parent button control.
- One final step - discard the panel control created to temporarily host the labels.
You must ensure that the labels do not obscure any of the button borders or else the button will appear unresponsive to the user. A 5 pixel gap all around is all that is required to ensure that WindowsXP style button effects are visible at all screen resolutions. This technique works perfectly. However, as the number of controls hosted inside the button increases you may notice that the button redraw slows down.
Download