通過 Application.OnMessage 響應消息:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
{這個自定義過程要復合 Application.OnMessage 的參數格式}
procedure MyMessage(var Msg: tagMSG; var Handled: Boolean);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.Clear;
Application.OnMessage := MyMessage; {讓 Application.OnMessage 執行自定義過程}
end;
{響應 WM_MOUSEMOVE 以外的所有消息}
procedure TForm1.MyMessage(var Msg: tagMSG; var Handled: Boolean);
begin
if Msg.message <> WM_MOUSEMOVE then
Memo1.Lines.Add('$' + IntToHex(Msg.message, 4));
end;
end.
通過 TApplicationEvents 響應消息, 需要在設計時添加 TApplicationEvents 組件, 並給它添加 OnMessage 事件:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, AppEvnts;
type
TForm1 = class(TForm)
Memo1: TMemo;
ApplicationEvents1: TApplicationEvents;
procedure FormCreate(Sender: TObject);
procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.Clear;
end;
{響應 WM_MOUSEMOVE 以外的所有消息}
procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
begin
if Msg.message <> WM_MOUSEMOVE then
Memo1.Lines.Add('$' + IntToHex(Msg.message, 4));
end;
end.