如何从表单外面捕捉表单的某些事件? [英] How do I catch certain events of a form from outside the form?
问题描述
我的项目有一个对象,它包装了正在监视的每个表单,我认为这个处理将会在这个对象。基本上,当我创建一个我想要监视的表单时,我创建一个相应的对象,这个对象又被添加到所有创建的表单的列表中。最重要的是,当表单关闭时,我必须知道,所以我可以从列表中删除此表单的包装对象。
这些事件包括:
- 最小化
- 最大化
- 还原
- 关闭
- 重点进/出
我不想要什么: p>
- 用于此处理的任何表单或单位内的任何代码
- 从任何自定义基础表单继承表单
- 使用表单的事件,例如
OnClose
,因为它们将用于其他目的
我想要的:
- 处理这些事件的Windows消息
- 有关如何从课外获取Windows消息的任何提示
- 需要收听哪些Windows消息
重写相同信息但不同方法的问题
以下是David提供的解决方案的更完整的例子:
private
{私有声明}
SaveProc:TWndMethod;
procedure CommonWindowProc(var Message:TMessage);
...
程序TForm1.Button1Click(Sender:TObject);
var
f:tForm2;
begin
f:= tForm2.Create(nil);
SaveProc:= f.WindowProc;
f.WindowProc:= CommonWindowProc;
f.Show;
结束
程序TForm1.CommonWindowProc(var Message:TMessage);
begin
case Message.Msg
WM_SIZE:Memo1.Lines.Add('Resizing');
WM_CLOSE:Memo1.Lines.Add('Closing');
CM_MOUSEENTER:Memo1.Lines.Add('Mouse enter form');
CM_MOUSELEAVE:Memo1.Lines.Add('Mouse leave form');
//所有其他消息将根据需要可用
end;
SaveProc(Message); //调用另一个形式的原始处理程序
end;
I'm working on something which will require monitoring of many forms. From outside the form, and without putting any code inside the form, I need to somehow capture events from these forms, most likely in the form of windows messages. But how would you capture windows messages from outside the class it's related to?
My project has an object which wraps each form it is monitoring, and I presume this handling will go in this object. Essentially, when I create a form I want to monitor, I create a corresponding object which in turn gets added to a list of all created forms. Most importantly, when that form is closed, I have to know so I can remove this form's wrapper object from the list.
These events include:
- Minimize
- Maximize
- Restore
- Close
- Focus in/out
What I DON'T want:
- Any code inside any forms or form units for this handling
- Inheriting the forms from any custom base form
- Using the form's events such as
OnClose
because they will be used for other purposes
What I DO want:
- Handling of windows messages for these events
- Any tips on how to get windows messages from outside the class
- Which windows messages I need to listen for
Question re-written with same information but different approach
Here's a more complete example of the solution that David Provided:
private
{ Private declarations }
SaveProc : TWndMethod;
procedure CommonWindowProc(var Message: TMessage);
...
procedure TForm1.Button1Click(Sender: TObject);
var
f : tForm2;
begin
f := tForm2.Create(nil);
SaveProc := f.WindowProc;
f.WindowProc := CommonWindowProc;
f.Show;
end;
procedure TForm1.CommonWindowProc(var Message: TMessage);
begin
case Message.Msg of
WM_SIZE : Memo1.Lines.Add('Resizing');
WM_CLOSE : Memo1.Lines.Add('Closing');
CM_MOUSEENTER : Memo1.Lines.Add('Mouse enter form');
CM_MOUSELEAVE : Memo1.Lines.Add('Mouse leaving form');
// all other messages will be available as needed
end;
SaveProc(Message); // Call the original handler for the other form
end;
这篇关于如何从表单外面捕捉表单的某些事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!