Change the title and attributes of the ShowMessage dialog box

In Delphi, you can change the title of the dialog box ShowMessage, because by default it accepts my exe name.

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

+5
source share
3 answers

You can create your own dialog boxes using the delphi function CreateMessageDialog.

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 dynamically insert other components into this form.

+16
source

The dialog box will use the contents as the title Application.Title. Therefore, you can set this before calling ShowMessage.

, , 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.

+5

, , .

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, .

0

All Articles