如何在不访问源的情况下更改模态表单的标题?

时间:2012-10-23 20:07:07

标签: delphi delphi-xe2

我有第三方组件,显示打印预览表单。我想将预览表单的标题更改为更合适的标题。不幸的是,我没有第三方组件的来源,组件也没有提供功能。

是否有可能以某种方式捕捉模态形式,因为它正在显示并在显示之前设置它的属性?

4 个答案:

答案 0 :(得分:10)

模态表单将导致调用表单停用,您可以在显示模式表单之前在活动表单上侦听WM_ACTIVATE消息。您将在消息处理程序中拥有激活窗口的句柄,如果它是模式窗体类型的形式,则可以测试它。下面是类名称的示例测试,您可以使用Spy ++之类的东西。请注意,在模态窗体变得可见之后会暂停,但我认为不会注意到不同的标题。

type
  TForm1 = class(TForm)
    ..
  protected
    procedure WMActivate(var Message: TWMActivate); message WM_ACTIVATE;
  end;

procedure TForm1.WMActivate(var Message: TWMActivate);
var
  Form: TWinControl;
begin
  if Message.Active = WA_INACTIVE then begin
    Form := FindControl(Message.ActiveWindow);
    if Form is TCustomForm then begin
      if TCustomForm(Form).ClassName = 'TThirdPartyModalForm' then
        TCustomForm(Form).Caption := 'My caption';
    end;
  end;
  inherited;
end;

答案 1 :(得分:6)

尝试使用TScreen.OnActiveFormChange事件,使用TScreen.ActiveCustomFormTScreen.ActiveForm属性了解哪个TForm具有焦点:

procedure TMainForm.DoSomething; 
begin 
  Screen.OnActiveFormChange := ActiveFormChanged;
  try
    // do something that triggers the modal form ...
  finally
    Screen.OnActiveFormChange := nil;
  end;
end;

procedure TMainForm.ActiveFormChanged(Sender: TObject);
var
  Form: TCustomForm;
begin
  Form := Screen.ActiveCustomForm; 
  if (Form <> nil) and (Form.ClassName = 'TModalFormClassName') then 
    Form.Caption := 'My caption'; 
end;

答案 2 :(得分:2)

您可以使用SetWindowsHookEx功能安装WH_CBT挂钩。

var
 hhk: HHOOK;


function CBT_FUNC(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
const
  ClassNameBufferSize = 1024;
var
 hTemp  : HWND;
 i      : Integer;
 RetVal : Integer;
 ClassNameBuffer: Array[0..ClassNameBufferSize-1] of Char;
begin
   case nCode of
     HCBT_ACTIVATE:
     begin
       hTemp := HWND(wParam);
       if (Screen<>nil) and (hTemp>0) then
       begin
          RetVal := GetClassName(wParam, ClassNameBuffer, SizeOf(ClassNameBuffer));
          //check for the class 
          if (RetVal>0) and SameText(ClassNameBuffer,'TForm2') then
          begin
             Assert(RetVal < ClassNameBufferSize, 'Class name larger than fixed buffer size');
            for i := 0 to Screen.FormCount-1 do
             if Screen.Forms[i].Handle=hTemp then
               begin 
                  //set the caption
                  Screen.Forms[i].Caption:='Hello';
                  Break;
               end;
          end;
       end;
     end;
   end;
  Result := CallNextHookEx(hhk, nCode, wParam, lParam);
end;

Procedure InitHook();
var
  dwThreadID : DWORD;
begin
  dwThreadID := GetCurrentThreadId;
  hhk := SetWindowsHookEx(WH_CBT, @CBT_FUNC, hInstance, dwThreadID);
  if hhk=0 then RaiseLastOSError;
end;


Procedure KillHook();
begin
  if (hhk <> 0) then
    UnhookWindowsHookEx(hhk);
end;


initialization
  InitHook();

finalization
  KillHook();
end.

答案 3 :(得分:2)

这是您可以尝试(未测试)的“伪”代码:

const
  MY_PRINT_PREVIEW_MSG = WM_USER + 200;

type
  TForm1 = class(TForm)
    procedure MyPrintPreviewMsg(var Msg: TMessage); message MY_PRINT_PREVIEW_MSG;
    procedure MyPrintPreview;
  end;
...
procedure TForm1.MyPrintPreviewMsg(var Msg: TMessage);
var
  h: HWND;
begin
  h := Screen.Forms[0].Handle; // if the modal dialog is VCL dialog (verify it with spy++)
  // h := FindWindow(<class name>, <caption>); // non VCL window
  if (h <> 0) then
  begin
    SetWindowText(h, 'new caption');
  end;
end;

procedure TForm1.MyPrintPreview;
begin
  PostMessage(Handle, MY_PRINT_PREVIEW_MSG, 0, 0);
  ThirdPartyPrintPreview;
end;