Getting a link to the dialog box form (ShowMessage, MsgDialog, etc.)

Is there any event that I could use, so I would catch the moment when it appears on the screen ShowMessage? I also need to pass a link to TFormthat showed the message.

So far I tried OnDeactivate, but it ShowMessagedoesn't seem to call it ...

In .NET there is a method on Application that catches every MessageBox (Application.AddFilterMessage or smth, like this), I need something like this in delphi

I am trying to achieve: I have to catch the moment until a dialog box appears (or just a modal window, but this is not so convenient). Then I need to follow a couple of instructions. The purpose of these instructions is to give me an answer to the DialogWindow just received, so that I can, for example, get a few buttons that lie on it.

+5
source share
4 answers

In modern versions of Delphi, in modern versions of Windows, the Windows ShowMessagedialog box appears. You can use the hook WH_CBTto catch the activation of this dialog box.

function CBTProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
  wnd: HWND;
  ClassName: string;
begin
  if nCode=HCBT_ACTIVATE then
  begin
    wnd := wParam;
    SetLength(ClassName, 256);
    SetLength(ClassName, GetClassName(wnd, PChar(ClassName), Length(ClassName)));
    if (ClassName='#32770') or (ClassName='TMessageForm') then
      Beep;
  end;
  Result := CallNextHookEx(0, nCode, wParam, lParam);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Hook: HHOOK;
begin
  Hook := SetWindowsHookEx(WH_CBT, CBTProc, HInstance, GetCurrentThreadId);
  if Hook=0 then
    RaiseLastOSError;
  try
    ShowMessage('hello');
  finally
    if not UnhookWindowsHookEx(Hook) then
      RaiseLastOSError;
  end;
end;

, . XP TMessageForm, Delphi TForm. Vista #32770.

, ShowMessage, , , .

+6

, OnCreate ( OnDestroy):

procedure TMainForm.FormCreate(Sender: TObject);
begin
  ...
  Application.HookMainWindow(ApplicationHook);
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
  ...
  Application.UnhookMainWindow(ApplicationHook);
end;

function TMainForm.ApplicationHook(var Message: TMessage): Boolean;
var
  I: Integer;
begin
  Result := False;
  if (Message.Msg = WM_ENABLE) and not TWMEnable(Message).Enabled then // disabling
    for I := 0 to Screen.FormCount - 1 do
      with Screen.Forms[I] do
        if Enabled and (ClassNameIs('TMessageForm') or // ShowMessage, MessageDlg
          ClassNameIs('TForm') or // InputQuery
          ClassNameIs('TMyLoginDialog')) then // your own dialogs, etc.
        begin
          Screen.Forms[I].Position := poScreenCenter; // for example
          Result := True;
          Break;
        end;
end;
+5

OnActiveFormChange?

procedure TForm3.FormCreate(Sender: TObject);
begin
  Screen.OnActiveFormChange := ScreenActiveFormChange;
end;

procedure TForm3.ScreenActiveFormChange(Sender: TObject);
begin
  if Screen.ActiveForm is TOKRightDlg then
    Screen.ActiveForm.Caption := 'Found';
end;

procedure TForm3.Button1Click(Sender: TObject);
begin
  with TOKRightDlg.Create(nil) do
  try
    ShowModal;
  finally
    Free;
  end;
end;
+2

There are TApplication.OnModalBegin and TApplicationEvents.OnModalBegin , I really have not tried them, so I can not comment on whether you can get the owner and / or modal form inside these events.

0
source

All Articles