TApplicationEvents.OnMessage 的第二个参数 Handled 如果是 True, 表示消息已经处理过了, 到此为止.
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, AppEvnts, StdCtrls; type TForm1 = class(TForm) Memo1: TMemo; ApplicationEvents1: TApplicationEvents; procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean); procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean); begin if Msg.message = WM_LBUTTONDOWN then begin Memo1.Lines.Add('OnMessage'); Handled := False; {Handled 默认是 False, 这句可以省略} // Handled := True; {如果这样, 下面 OnMouseDown 事件将不会得到执行} end; end; procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin Memo1.Lines.Add('OnMouseDown'); end; end.
我们可以利用这个特性来屏蔽一些消息, 譬如给 TWebBrowser 屏蔽右键菜单:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, OleCtrls, SHDocVw, AppEvnts; type TForm1 = class(TForm) WebBrowser1: TWebBrowser; ApplicationEvents1: TApplicationEvents; procedure FormCreate(Sender: TObject); procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean); end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean); begin if IsChild(WebBrowser1.Handle, Msg.hwnd) and (Msg.message = WM_RBUTTONDOWN) then begin Handled := True; end; end; procedure TForm1.FormCreate(Sender: TObject); begin WindowState := wsMaximized; WebBrowser1.Align := alTop; WebBrowser1.Navigate('http://www.cnblogs.com/del/'); end; end.