程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 學習 Message(4): 通過 Application.OnMessage 或 TApplicationEvents 響應消息

學習 Message(4): 通過 Application.OnMessage 或 TApplicationEvents 響應消息

編輯:Delphi

 通過 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.


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved