学習Message(4):Application.OnMessageまたはTApplicationEventsレスポンスメッセージ

2012 ワード

アプリケーションで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.