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.