Delphi   发布时间:2022-04-10  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Delphi中显示自定义消息对话框的最佳方法是什么?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用的是Delphi,我想在messageDlg,as described here的按钮中显示自定义文本.最好的方法是什么?

解决方法

回答我自己的问题….我写下面这个对我有用的单位.

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 @R_450_8913@.
      //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.

大佬总结

以上是大佬教程为你收集整理的Delphi中显示自定义消息对话框的最佳方法是什么?全部内容,希望文章能够帮你解决Delphi中显示自定义消息对话框的最佳方法是什么?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。