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

时间:2011-10-13 12:44:23

标签: delphi delphi-2006

在Delphi中,您可以更改ShowMessage对话框的标题,因为默认情况下它会使用我的exe名称。

我可以更改背景颜色,大小相同吗?

3 个答案:

答案 0 :(得分:17)

您可以使用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;

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

答案 1 :(得分:5)

该对话框将使用Application.Title的内容作为标题。所以你可以在调用ShowMessage之前设置它。

但是,如果要显示具有不同标题的多个对话框,则调用Windows MessageBox函数会更方便。当然,如果你有一个旧版本的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后代。

答案 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的对话框弹出功能,你可以获得对话框的上下文并在其上绘制。