Change caption and attributes of ShowMessage dialog

六眼飞鱼酱① 提交于 2021-02-07 07:11:40

问题


In Delphi can you change the caption of the ShowMessage dialog because by default it is taking my exe name.

And can I change the background color, size of the same?


回答1:


You can create your own custom dialogs by using delphi's CreateMessageDialog function.

Example below:

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;

and of course you can insert other components aswell into that form dynamically.




回答2:


The dialog will use the contents of Application.Title as the caption. So you could set this before calling ShowMessage.

However, if you want to show multiple dialogs with different captions, it would be more convenient to call the Windows MessageBox function. Certainly if you have an older version of Delphi this will result in a more native feel to your dialog.

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;

If you wish to control color and size then the most obvious option is to create your own dialog as a TForm descendent.




回答3:


Here's a bit of code I wrote, you might want to use it note for note.

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;

...

Put this in your Form Create where you want to make the funky message boxes

uses windows;

...

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

So, what this does is hook into Windows' dialog popup functions and you can get the context for the dialog and draw on it.



来源:https://stackoverflow.com/questions/7754322/change-caption-and-attributes-of-showmessage-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!