标签:style blog class code java ext
好奇一下。看来Object Pascal确实与Windows深入结合了。
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; const UM_Test = WM_USER + 100; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } procedure MyMessage(var Msg: TMessage); message UM_Test; end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin Perform(UM_Test, 0, 0); end; procedure TForm1.MyMessage(var Msg: TMessage); begin inherited; ShowMessage(‘Hello‘); end; end.
在message处理中和其他不一样的是inherited不会因为没有在祖先中找到一样的函数或者方法而将inherited失效,他会传入缺省的消息处理.
这里调用TFORM1的祖先的消息处理,由于tform和tcustomform没有这个实现,所以直接调用的是tcustomform的defaulthandle.(注意这个方法是对twincontrol的override)。
但是,如果本类重载了DefaultHandler函数,就会直接调用本类的DefaultHandler函数:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; const UM_Test = WM_USER + 100; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } procedure MyMessage(var Msg: TMessage); message UM_Test; procedure DefaultHandler(var Message); override; end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin Perform(UM_Test, 0, 0); end; procedure TForm1.DefaultHandler(var Message); begin with TMessage(Message) do begin if Msg = UM_Test then ShowMessage(‘DefaultHandler‘); end; inherited; end; procedure TForm1.MyMessage(var Msg: TMessage); begin inherited; ShowMessage(‘Hello‘); end; end.
顺便再看看这样改写的效果:
procedure TForm1.DefaultHandler(var Message); begin with TMessage(Message) do begin if Msg = UM_Test then ShowMessage(‘DefaultHandler‘); if Msg = WM_SETTEXT then ShowMessage(‘WM_SETTEXT‘); end; inherited; end;
inherited在消息中的作用,码迷,mamicode.com
标签:style blog class code java ext
原文地址:http://www.cnblogs.com/findumars/p/3703078.html