通过 Application.OnMessage 或 TApplicationEvents 响应消息 转载 https://www.cnblogs.com/del/archive/2008/10/24/1319086.html

 

 

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

posted @   QuincyYi  阅读(118)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示