Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 270823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:03:48+00:00 2026-05-12T00:03:48+00:00

I am using Delphi, and I want to show custom text in the buttons

  • 0

I am using Delphi, and I want to show custom text in the buttons of a MessageDlg, as described here. What is the best way to do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:03:48+00:00Added an answer on May 12, 2026 at 12:03 am

    Answering my own question…. I wrote the below unit which works well for me.

    Delphi provides CreateMessageDialog() to give you a dialog template, which you can modify before displaying. I used that to create a function I called MessageDlgCustom, which takes the same parameters as a standard MessageDlg, but adds one more for replacement button titles.

    It correctly handles custom fonts and automatically adjusts buttons to be wide enough for their message. If the buttons overflow the dialog, then that gets adjusted too.

    After using that unit, the below sample works:

    case MessageDlgCustom('Save your changes?',mtConfirmation,
      [mbYes,mbNo,mbCancel],
      ['&Yes, I would like to save them with this absurdly long button',
      '&No, I do not care about my stupid changes',
      '&Arg! What are you talking about?  Do not close the form!'],
      nil)  //nil = no custom font
    of
      mrYes:   
        begin
          SaveChanges;
          CloseTheForm;
        end;  //mrYes (save & close)
      mrNo: 
        begin
          CloseForm;
        end;  //mrNo (close w/o saving)
      mrCancel:
        begin
          //do nothing
        end;  //mrCancel (neither save nor close)
    end;  //case
    

    If someone else knows a better way, please share it.

    unit CustomDialog;
    
    interface
    
    uses
      Dialogs, Forms, Graphics, StdCtrls;
    
    function MessageDlgCustom(const Msg: string; DlgType: TMsgDlgType;
      Buttons: TMsgDlgButtons; ToCaptions: array of string;
      customFont: TFont) : integer;
    procedure ModifyDialog(var frm: TForm; ToCaptions : array of string;
      customFont : TFont = nil);
    
    
    implementation
    
    uses
      Windows, SysUtils;
    
    function GetTextWidth(s: string; fnt: TFont; HWND: THandle): integer;
    var
      canvas: TCanvas;
    begin
      canvas := TCanvas.Create;
      try
        canvas.Handle := GetWindowDC(HWND);
        canvas.Font := fnt;
        Result := canvas.TextWidth(s);
      finally
        ReleaseDC(HWND,canvas.Handle);
        FreeAndNil(canvas);
      end;  //try-finally
    end;
    
    function MessageDlgCustom(const Msg: string;
      DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; ToCaptions: array of string;
      customFont: TFont): integer;
    var
      dialog : TForm;
    begin
      try
        dialog := CreateMessageDialog(Msg, DlgType, Buttons);
        dialog.Position := poScreenCenter;
        ModifyDialog(dialog,ToCaptions,customFont);
        Result := dialog.ShowModal;
      finally
        dialog.Release;
      end;  //try-finally
    end;
    
    procedure ModifyDialog(var frm: TForm; ToCaptions: array of string;
      customFont: TFont);
    const
      c_BtnMargin = 10;  //margin of button around caption text
    var
      i,oldButtonWidth,newButtonWidth,btnCnt : integer;
    begin
      oldButtonWidth := 0;
      newButtonWidth := 0;
      btnCnt := 0;
      for i := 0 to frm.ComponentCount - 1 do begin
        //if they asked for a custom font, assign it here
        if customFont <> nil then begin
          if frm.Components[i] is TLabel then begin
            TLabel(frm.Components[i]).Font := customFont;
          end;
          if frm.Components[i] is TButton then begin
            TButton(frm.Components[i]).Font := customFont;
          end;
        end;
        if frm.Components[i] is TButton then begin
          //check buttons for a match with a "from" (default) string
          //if found, replace with a "to" (custom) string
          Inc(btnCnt);
    
          //record the button width *before* we changed the caption
          oldButtonWidth := oldButtonWidth + TButton(frm.Components[i]).Width;
    
          //if a custom caption has been provided use that instead,
          //or just leave the default caption if the custom caption is empty
          if ToCaptions[btnCnt - 1]<>'' then
            TButton(frm.Components[i]).Caption := ToCaptions[btnCnt - 1];
    
          //auto-size the button for the new caption
          TButton(frm.Components[i]).Width :=
            GetTextWidth(TButton(frm.Components[i]).Caption,
              TButton(frm.Components[i]).Font,frm.Handle) + c_BtnMargin;
    
          //the first button can stay where it is.
          //all other buttons need to slide over to the right of the one b4.
          if (1 < btnCnt) and (0 < i) then begin
            TButton(frm.Components[i]).Left :=
              TButton(frm.Components[i-1]).Left +
              TButton(frm.Components[i-1]).Width + c_BtnMargin;
          end;
    
          //record the button width *after* changing the caption
          newButtonWidth := newButtonWidth + TButton(frm.Components[i]).Width;
        end;  //if TButton
      end;  //for i
    
      //whatever we changed the buttons by, widen / shrink the form accordingly
      frm.Width := Round(frm.Width + (newButtonWidth - oldButtonWidth) +
        (c_BtnMargin * btnCnt));
    end;
    
    end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm using delphi,i have a text and i want to fit it into a
I`m using Delphi 2009 and want to operate some XML data. I heard that
HI, Im using Delphi and I want to make an application that can do
I'm using Delphi 7, and I want to create a custom warning message (which
How would I go about extracting text between 2 html tags using delphi? Here
I want to write Arabic text in an Excel Sheet by using Delphi. When
I want to monitor the copy file function using Delphi. I can do it
I am using Delphi 2010, and when I created a console application that prints
I'm using Delphi 2009 and want to decode an HTML encoded string, for example:
I am using Delphi XE2 with help installed. I want to see the hints

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.