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

前端之家收集整理的这篇文章主要介绍了Delphi中显示自定义消息对话框的最佳方法是什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用的是Delphi,我想在MessageDlg,as described here的按钮中显示自定义文本.最好的方法是什么?

解决方法

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

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

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

使用该单元后,以下示例工作:

  1. 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
  2. of
  3. mrYes:
  4. begin
  5. SaveChanges;
  6. CloseTheForm;
  7. end; //mrYes (save & close)
  8. mrNo:
  9. begin
  10. CloseForm;
  11. end; //mrNo (close w/o saving)
  12. mrCancel:
  13. begin
  14. //do nothing
  15. end; //mrCancel (neither save nor close)
  16. end; //case

如果有人知道更好的方法,请分享.

  1. unit CustomDialog;
  2.  
  3. interface
  4.  
  5. uses
  6. Dialogs,Forms,Graphics,StdCtrls;
  7.  
  8. function MessageDlgCustom(const Msg: string; DlgType: TMsgDlgType;
  9. Buttons: TMsgDlgButtons; ToCaptions: array of string;
  10. customFont: TFont) : integer;
  11. procedure ModifyDialog(var frm: TForm; ToCaptions : array of string;
  12. customFont : TFont = nil);
  13.  
  14.  
  15. implementation
  16.  
  17. uses
  18. Windows,SysUtils;
  19.  
  20. function GetTextWidth(s: string; fnt: TFont; HWND: THandle): integer;
  21. var
  22. canvas: TCanvas;
  23. begin
  24. canvas := TCanvas.Create;
  25. try
  26. canvas.Handle := GetWindowDC(HWND);
  27. canvas.Font := fnt;
  28. Result := canvas.TextWidth(s);
  29. finally
  30. ReleaseDC(HWND,canvas.Handle);
  31. FreeAndNil(canvas);
  32. end; //try-finally
  33. end;
  34.  
  35. function MessageDlgCustom(const Msg: string;
  36. DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; ToCaptions: array of string;
  37. customFont: TFont): integer;
  38. var
  39. dialog : TForm;
  40. begin
  41. try
  42. dialog := CreateMessageDialog(Msg,DlgType,Buttons);
  43. dialog.Position := poScreenCenter;
  44. ModifyDialog(dialog,ToCaptions,customFont);
  45. Result := dialog.ShowModal;
  46. finally
  47. dialog.Release;
  48. end; //try-finally
  49. end;
  50.  
  51. procedure ModifyDialog(var frm: TForm; ToCaptions: array of string;
  52. customFont: TFont);
  53. const
  54. c_BtnMargin = 10; //margin of button around caption text
  55. var
  56. i,oldButtonWidth,newButtonWidth,btnCnt : integer;
  57. begin
  58. oldButtonWidth := 0;
  59. newButtonWidth := 0;
  60. btnCnt := 0;
  61. for i := 0 to frm.ComponentCount - 1 do begin
  62. //if they asked for a custom font,assign it here
  63. if customFont <> nil then begin
  64. if frm.Components[i] is TLabel then begin
  65. TLabel(frm.Components[i]).Font := customFont;
  66. end;
  67. if frm.Components[i] is TButton then begin
  68. TButton(frm.Components[i]).Font := customFont;
  69. end;
  70. end;
  71. if frm.Components[i] is TButton then begin
  72. //check buttons for a match with a "from" (default) string
  73. //if found,replace with a "to" (custom) string
  74. Inc(btnCnt);
  75.  
  76. //record the button width *before* we changed the caption
  77. oldButtonWidth := oldButtonWidth + TButton(frm.Components[i]).Width;
  78.  
  79. //if a custom caption has been provided use that instead,//or just leave the default caption if the custom caption is empty
  80. if ToCaptions[btnCnt - 1]<>'' then
  81. TButton(frm.Components[i]).Caption := ToCaptions[btnCnt - 1];
  82.  
  83. //auto-size the button for the new caption
  84. TButton(frm.Components[i]).Width :=
  85. GetTextWidth(TButton(frm.Components[i]).Caption,TButton(frm.Components[i]).Font,frm.Handle) + c_BtnMargin;
  86.  
  87. //the first button can stay where it is.
  88. //all other buttons need to slide over to the right of the one b4.
  89. if (1 < btnCnt) and (0 < i) then begin
  90. TButton(frm.Components[i]).Left :=
  91. TButton(frm.Components[i-1]).Left +
  92. TButton(frm.Components[i-1]).Width + c_BtnMargin;
  93. end;
  94.  
  95. //record the button width *after* changing the caption
  96. newButtonWidth := newButtonWidth + TButton(frm.Components[i]).Width;
  97. end; //if TButton
  98. end; //for i
  99.  
  100. //whatever we changed the buttons by,widen / shrink the form accordingly
  101. frm.Width := Round(frm.Width + (newButtonWidth - oldButtonWidth) +
  102. (c_BtnMargin * btnCnt));
  103. end;
  104.  
  105. end.

猜你在找的Delphi相关文章