6

我有一个显示打印预览表单的第三方组件。我想将预览表单的标题更改为更合适的内容。不幸的是,我没有第三方组件的源代码,并且该组件不提供该功能。

是否有可能以某种方式捕获正在显示的模态表单并在显示之前设置它的属性?

4

4 回答 4

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;
于 2012-10-23T20:27:44.117 回答
6

尝试使用TScreen.OnActiveFormChange事件,使用TScreen.ActiveCustomFormorTScreen.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;
于 2012-10-24T01:41:07.017 回答
2

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

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.
于 2012-10-23T20:28:37.753 回答
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;
于 2012-10-23T20:34:31.427 回答