Delphi:检测何时创建了新表单 [英] Delphi: Detect when a new form has been created
问题描述
我想检测何时创建了新表单。
I'd like to detect when a new form has been created.
现在,我使用 Screen.ActiveFormChange
事件并检查 Screen.CustomForms
中是否有新表单,但之后会触发
事件。 ActiveFormChange
表单的OnShow
Now I use the Screen.ActiveFormChange
event and check for new forms in Screen.CustomForms
but ActiveFormChange
is fired after the OnShow
event of the form.
我想在 OnShow $ c $之前检测表单c>被解雇了。是否可以在不修改
Vcl.Forms
单元的情况下执行此操作?
I'd like to detect the form even before OnShow
was fired. Is there any way to do this without modifying the Vcl.Forms
unit?
我想检测所有表单(也包括Delphi模态消息等),因此不可能从自定义类继承所有表单(如果我错了,请纠正我)。
I'd like to detect all forms (also Delphi modal messages etc.) therefore inheriting all forms from a custom class is not possible (correct me if I am wrong).
或者,是否有可能是否检测到某个新组件已添加到某些 TComponent.FComponents
列表中?
Alternatively, is it possible to detect that a new component was added to some TComponent.FComponents
list?
推荐答案
您可以使用 SetWindowsHookEx
函数安装 WH_CBT
挂钩,则必须实现 CBTProc回调函数,并最终拦截其中一个可能的代码钩子的值。在这种情况下,您可以尝试使用 HCBT_ACTIVATE
或 HCBT_CREATEWND
。
You can use the SetWindowsHookEx
function to install a WH_CBT
Hook, then you must implement a CBTProc callback function and finally intercept one of the possible code values for this hook. in this case you can try with HCBT_ACTIVATE
or HCBT_CREATEWND
.
检查此示例的 HCBT_ACTIVATE
代码。
var
hhk: HHOOK;
function CBT_FUNC(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
const
ClassNameBufferSize = 1024;
var
hWindow: HWND;
RetVal : Integer;
ClassNameBuffer: Array[0..ClassNameBufferSize-1] of Char;
begin
Result := CallNextHookEx(hhk, nCode, wParam, lParam);
if nCode<0 then exit;
case nCode of
HCBT_ACTIVATE:
begin
hWindow := HWND(wParam);
if (hWindow>0) then
begin
RetVal := GetClassName(wParam, ClassNameBuffer, SizeOf(ClassNameBuffer));
if RetVal>0 then
begin
//do something
OutputDebugString(ClassNameBuffer);
end;
end;
end;
end;
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.
注意:如果使用
HCBT_CREATEWND
代码,您将
拦截系统创建的任何窗口,而不仅仅是表单。
Note : if you uses the
HCBT_CREATEWND
code instead you will intercept any window created by the system not just "forms".
这篇关于Delphi:检测何时创建了新表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!