更改ShowMessage对话框的标题和属性

8
在 Delphi 中,您能否更改 ShowMessage 对话框的标题?因为默认情况下它会取我的 exe 名称。
同样,我能否更改相同对话框的背景颜色和大小?
3个回答

18
你可以使用Delphi的CreateMessageDialog函数创建自己的自定义对话框。
下面是一个例子:
var
  Dlg: TForm;
begin
  Dlg := CreateMessageDialog('message', mtInformation, [mbOk], mbOK);
  // Treat Dlg like any other form

  Dlg.Caption := 'Hello World';

  try
    // The message label is named 'message'
    with TLabel(Dlg.FindComponent('message')) do
    begin
      Font.Style := [fsUnderline];

      // extraordinary code goes here
    end;

    // The icon is named... icon
    with TPicture(Dlg.FindComponent('icon')) do
    begin
      // more amazing code regarding the icon
    end;

    Dlg.ShowModal;
  finally
    Dlg.Free;
  end;

当然,您也可以动态地将其他组件插入到该表单中。


我从来不知道 FindComponent 的存在。+1! - David

8
对话框将使用Application.Title的内容作为标题。因此,在调用ShowMessage之前,您可以设置这个属性。
然而,如果您想显示带有不同标题的多个对话框,则更方便的方法是调用WindowsMessageBox函数。当然,如果您使用较旧的Delphi版本,这样做会使您的对话框具有更本地的感觉。
procedure MyShowMessage(const Msg, Caption: string);
begin
  MessageBox(GetParentWindowHandleForDialog, PChar(Msg), PChar(Caption), MB_OK);
end;

function GetParentWindowHandleForDialog: HWND;
begin
  //we must be careful that the handle we use here doesn't get closed while the dialog is showing
  if Assigned(Screen.ActiveCustomForm) then begin
    Result := Screen.ActiveCustomForm.Handle;
  end else if Assigned(Application.MainForm) then begin
    Result := Application.MainFormHandle;
  end else begin
    Result := Application.Handle;
  end;
end;

如果您想控制颜色和尺寸,那么最明显的选择是创建自己的对话框作为TForm的派生类。


我在BDS 2006中找不到MianWindowHandle,似乎将自定义窗体作为对话框是一个选项。 - Shirish11
我记错了,Delphi 中是 MainFormHandle。我回答了太多关于 WinForms 的问题了! - David Heffernan
抱歉,我无法理解该注释。 - David Heffernan
Windows的MessageBox也是模态的。通过使用ShowModal方法,TForm的派生类可以设置为模态。我提供的两个选项都可以以模态方式运行。 - David Heffernan
让我们在聊天中继续这个讨论 - Shirish11
显示剩余2条评论

0

这是我写的一段代码,你可能想逐字使用它。

function SetHook(Code : Integer; wparam : Integer; LParam : Integer) : Longint;    stdcall;
function HookWndProc(wnd : HWND ;uMsg : UINT;  wParam : WPARAM; lParam : LPARAM ) :   LRESULT; stdcall;
var
  CaptHook : HHOOK;
  GHookProc : TFNWndProc;
  GOldHookProc : TFNWndProc;
implementation

uses Messages, Types, Graphics;

  function SetHook(Code : Integer; wparam : Integer; LParam : Integer) : Longint; stdcall;
 var
   pwp : CWPSTRUCT;
 begin
 if Code = HC_ACTION then
 begin
   pwp := CWPStruct(Pointer(LParam)^);
   if pwp.message = WM_INITDIALOG then
   begin
     GOldHookProc := TFnWndProc(SetWindowLong(pwp.hwnd, GWL_WNDPROC, LongInt(GHookProc)));
   end;
  end;

 result := CallNextHookEx(CaptHook, Code, wparam, lparam);

end;

function HookWndProc(wnd : HWND ;uMsg : UINT;  wParam : WPARAM; lParam : LPARAM ) : LRESULT;
var
  DC : HDC;
  WndRect : Trect;
  BR: HBRUSH;
  WndText : array[1..20] of  char;
begin

 result := CallWindowProc(GOldHookProc, wnd, uMsg, wParam, lParam );
 if uMsg = WM_ERASEBKGND then
 begin
    GetWindowText(wnd, @wndText, 20);

    //do stuff here (I colored the button red)
    DC := GetDC(wnd);
    WndRect := Rect(0, 0, 200,200);
    BR := CreateSolidBrush(clRed);
    FillRect(dc, WndRect, BR);
    DeleteObject(BR);
    ReleaseDC(wnd, dc);
 end;
end;

...

将此代码放在您想要创建时酷炫的消息框的表单创建中。
uses windows;

...

 CaptHook := SetWindowsHookEx(WH_CALLWNDPROC, @SetHook, 0, GetCurrentThreadId);
 GHookProc := @HookWndProc;

所以,这个程序会钩入 Windows 的对话框弹出函数,你可以获取对话框的上下文并在其上绘制。

@David,我不得不为内部应用程序编写这个功能,因为这里的一些技术人员希望在重要的警告消息上有红色按钮。它本质上是一个钩子,可以在弹出的对话框的“画布”上绘制。您可以在评论所在的位置绘制。 - Peter Turner

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接