使用(1)设置TApplication.OnMessage事件、 (2)重载WndProc虚拟方法、 (3)使用TApplicationEvents对象;这3种方法好像都能改变程序对消息的处理,我很想知道这些具体怎么使用?它们有什么区别吗?求教大大,希望能给出代码作为具体用法的示例,谢谢了。
使用(1)设置TApplication.OnMessage事件、 (2)重载WndProc虚拟方法、 (3)使用TApplicationEvents对象;这3种方法好像都能改变程序对消息的处理,我很想知道这些具体怎么使用?它们有什么区别吗?求教大大,希望能给出代码作为具体用法的示例,谢谢了。
TApplication.OnMessage是在TApplication.Processmessage中调用的
在Processmessage中调用API dispatchmessage将消息发到某个窗口过程,
窗口过程将消息发给某个对象,对象的wndproc被调用. 你如果指定了TApplication.OnMessage:=myproc;(myproc:tmessageevent;
其中tmessageevent=procedure(var msg:Tmsg;var handled:boolean) of object)
那么TApplication.Processmessage就调用myproc,如果myproc中handled:=true消息处理完毕,以后代码不再响应该消息,否则按上面说的向下传
3没用过
to yuqianyi1974:关于1和2,你这样说我还是似懂非懂,能分别给出这两种用法的完整代码我看看吗?先谢过了。
const
WM_DATA = WM_APP + 200;procedure MessageProc(var Msg: TMsg; var Handled: Boolean);
procedure WMData(var Msg: TMessage); message WM_DATA;
procedure TForm1.WMData(var Msg: TMessage);
begin
Memo.Lines.Add('TForm1.WMData has processed WM_DATA.');
DefaultHandler(Msg);
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnMessage := Self.MessageProc;
end;procedure TForm1.MessageProc(var Msg: TMsg; var Handled: Boolean);
begin
if Msg.message = WM_DATA then
begin
Memo.Lines.Add('Application.OnMessage has processed WM_DATA.');
Handled := False;//你改一下true试试 end;
end;
procedure TForm1.ButtonPostClick(Sender: TObject);
begin
PostMessage(Self.Handle, WM_DATA, 0, 0);//只有PostMessage才能让MessageProc响应
Memo.Lines.Add('WM_DATA has been posted.');
end;
procedure WndProc(var Msg: TMessage); override;
procedure WMData(var Msg: TMessage); message WM_DATA;
procedure TForm1.ButtonSendClick(Sender: TObject);
begin
SendMessage(Self.Handle, WM_DATA, 0, 0);
Memo.Lines.Add('WM_DATA has been sent.');
end;procedure TForm1.ButtonPostClick(Sender: TObject);
begin
PostMessage(Self.Handle, WM_DATA, 0, 0);
Memo.Lines.Add('WM_DATA has been posted.');
end;procedure TForm1.MessageProc(var Msg: TMsg; var Handled: Boolean);
begin
if Msg.message = WM_DATA then
begin
Memo.Lines.Add('Application.OnMessage has processed WM_DATA.');
Handled := TRUE;
end;
end;procedure TForm1.WMData(var Msg: TMessage);
begin
Memo.Lines.Add('TForm1.WMData has processed WM_DATA.');
DefaultHandler(Msg);
end;procedure TForm1.WndProc(var Msg: TMessage);
begin
if Msg.Msg = WM_DATA then
Memo.Lines.Add('TForm1.WndProc has processed WM_DATA.');
inherited WndProc(Msg);
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnMessage := Self.MessageProc;
end;procedure TForm1.ButtonPerformClick(Sender: TObject);
begin
Perform(WM_DATA, 0, 0);
Memo.Lines.Add('WM_DATA has been performed.');
end;
这是一段完整包括设置TApplication.OnMessage事件、重载WndProc虚拟方法的例程,你好好琢磨琢磨
在delphi的"... \Demos\AppEvents"目录下有例程
to yuqianyi1974:先给分吧,呵呵。