Hello.
I would like to show a multiline caption on a button, but with the second line in bold. Can somebody do it?
Currently I have a multiline caption, but don't know how to draw text in different font styles (color, style, size, etc.) within one button.
(Delphi 2006)

Thanks in advance, have a nice day.

Recommended Answers

All 2 Replies

procedure TForm1.Button1Click(Sender: TObject);
begin
  Button1.WordWrap:=True;
  Button1.Caption:='Hello'+sLineBreak+'World';
end;

Hello.
I would like to show a multiline caption on a button, but with the second line in bold. Can somebody do it?
Currently I have a multiline caption, but don't know how to draw text in different font styles (color, style, size, etc.) within one button.
(Delphi 2006)

Thanks in advance, have a nice day.

If you feel upto the task of editing a .DFM directly, here is how you can do it:

  1. First create your button and size it so it can take two lines of text
  2. You are not going to use the intrinsic button caption so you can simply set it to a null string.
  3. Now create two label controls, anywhere in your form, place them one below the other and assign their Caption Font Alignment and Layout properties so the captions appear pretty much the way you would want them to appear on your button.
  4. Remember to set the AutoSize property to False or else the labels will resize to occupy just as much space as they need when the form is redrawn.
  5. Do Alt + F12 to see the .DFM
  6. Locate the code for your two label controls, cut it and paste it inside the code for the button control.
  7. Do Alt + F12 to see the form in the Delphi IDE
  8. Adjust the Top Left Height and Width properties so the button boundaries are not obscured. This is necessary to ensure that users continue to see a "normal" button which responds to mouse moves and shows up WindowsXP style edge effects.
  9. Almost done - all you need to do now is to assign an OnClick event to your two label controls so as far as the user is concerned he/she has just clicked on the button as always.

This technique works because TButton controls are in fact TWinControl descendants - which means they can act as containers for other controls. Trouble is that the way Borland has defined TButton means that the ControlStyle set property does not include csAcceptsControls as a result of which you cannot simply create controls inside the button in the visual IDE. Editing the DFM directly gives you a way to do this.

Here is the .DFM code that I created to test this

object btnTest: TButton
    Left = 404
    Top = 260
    Width = 221
    Height = 85
    TabOrder = 0
    OnClick = DoClick
    object lblOne: TLabel
      Left = 6
      Top = 4
      Width = 210
      Height = 22
      Alignment = taCenter
      AutoSize = False
      Caption = 'Hello'
      FocusControl = btnTest
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -12
      Font.Name = 'MS Sans Serif'
      Font.Style = [fsBold]
      ParentFont = False
      Transparent = True
      Layout = tlBottom
      OnClick = WhenClicked
    end
    object lblTwo: TLabel
      Left = 6
      Top = 26
      Width = 210
      Height = 52
      Alignment = taCenter
      AutoSize = False
      Caption = 'World!'
      FocusControl = btnTest
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clRed
      Font.Height = -13
      Font.Name = 'MS Sans Serif'
      Font.Style = [fsBold]
      ParentFont = False
      Transparent = True
      Layout = tlCenter
      OnClick = WhenClicked
    end
  end

You can copy and paste it directly into your Delphi form - no mess about with the DFM. You might need to play around with it a bit to get it work just the way you want. This technique has one, very small, limitation - the button does not offer the WindowsXP style mouse move response quite as well as a "normal" button if the mouse move is very fast. I suspect this is because having to redraw the child label controls slows down the button redraw but I am not sure.

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.