Delphi中显示自定义消息对话框的最佳方式是什么?

时间:2009-07-08 19:26:10

标签: delphi user-interface

我正在使用Delphi,我想在MessageDlg的按钮中显示自定义文本as described here。最好的方法是什么?

4 个答案:

答案 0 :(得分:11)

回答我自己的问题....我写了下面的单元,对我来说效果很好。

Delphi提供CreateMessageDialog()为您提供一个对话框模板,您可以在显示之前进行修改。我用它来创建一个名为MessageDlgCustom的函数,它接受与标准MessageDlg相同的参数,但为替换按钮标题添加了一个。

它可以正确处理自定义字体,并自动调整按钮以使其消息足够宽。如果按钮溢出对话框,那么它也会被调整。

使用该单位后,以下示例有效:

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

如果其他人知道更好的方法,请分享。

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.

答案 1 :(得分:3)

作为替代方案,您可以使用开源SynTaskDialog单元。 SynTaskDialog在较新的Windows版本上本机使用Windows TaskDialog API,并在旧版本上模拟它。你甚至可以use it with FireMonkey

有关可自定义的MessageDlg函数的示例,请查看this answer

答案 2 :(得分:2)

您可以查看GitHub(https://github.com/digao-dalpiaz/Dam)上可用的TDam组件。

该组件允许您使用格式文本(HTML文本)创建带有预定义按钮的自定义消息对话框,并允许自定义对话框的许多方面。

除此之外,您还可以将所有应用程序对话框管理到一个“容器”中,该容器将所有对话框存储为对象(TDamMsg)。

TDam Message Example

TDamMsg属性允许自定义消息对话框,例如:

  • 按钮1-按钮1标题
  • Button2-按钮2字幕
  • Button3-按钮3字幕

按钮:TDamMsgButtons =定义消息对话框中的按钮:

  • dbOK:定义一个按钮,确定
  • dbYesNo:定义两个按钮是/否
  • dbOne:通过Button1定义的标题定义一个按钮
  • dbTwo:通过Button1和Button2定义的标题定义两个按钮
  • dbThree:按Button1,Button2和Button3定义的标题定义三个按钮

答案 3 :(得分:1)

另外,请确保您的第三方控件也是如此 调用您的自定义消息dlg而不是标准 MessageDlg函数。那就是他们真的 使用它。第三方控制可能 不要使用Delphi messagedlg并调用 MessageBox API直接。如果是这样的话,你可能会 最终显示消息不一致 框。