zoukankan      html  css  js  c++  java
  • 学习 Message(4): 通过 Application.OnMessage 或 TApplicationEvents 响应消息


    通过 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.
    
  • 相关阅读:
    XCode5中新建工程后强制使用了ARC,如何去掉?
    面向对象程序的设计原则--Head First 设计模式笔记
    ios控件自定义指引
    iOS UITableViewDelegate && UITableViewDataSource 执行顺序
    awakeFromNib方法和viewDidLoad方法区别
    ios 视图的旋转及应用
    线段树模板 (刘汝佳)
    poj 3468
    hdu 2829(四边形优化 && 枚举最后一个放炸弹的地方)
    poj 3517(约瑟夫环问题)
  • 原文地址:https://www.cnblogs.com/del/p/1319086.html
Copyright © 2011-2022 走看看