zoukankan      html  css  js  c++  java
  • Delphi中Message消息的三种使用方法(覆盖WndProc,覆盖消息函数,改写WMCommand)

    实例1

    unit Unit1;

    interface
    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;

    const
    WM_ME=WM_USER+100; //自定义消息;
    type
    TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    //第一种消息处理方式; 只能处理常量消息;
    procedure wmme(var message:TMessage);message WM_ME; //自定义消息处理过程,专门处理WM_ME消息
    public
    //第二种消息处理方式;可能处理常量或变量消息;
    procedure WndProc(var message:TMessage);override; //重载窗口消息过程
    //第三种消息处理方式
    procedure WMCommand(var Message: TWMCommand); message WM_COMMAND; //命令消息处理过程
    procedure WMSysCommand(var Msg:TWMSysCommand);message WM_SYSCOMMAND; //处理系统性消息;
    end;
    var
    Form1: TForm1;
    implementation
    {$R *.dfm}

    procedure TForm1.Button1Click(Sender: TObject);
    begin
    SendMessage(Handle,WM_ME,0,0); // 发送消息WM_ME ; 消息先由WndProc处理,再交给wmme处理;
    end;

    procedure TForm1.WMSysCommand(var Msg: TWMSysCommand);
    begin
    //下面代码的作用是,用户如果点击了标题栏上的最小化和关闭按钮,则隐藏窗体。
    if (Msg.CmdType=SC_MINIMIZE) or (Msg.CmdType=SC_CLOSE) then
    begin
    Self.Hide; // 拦截了系统消息(我猜不能拦截普通消息),而且不再执行DefaultHandler。测试,可以把DefaultHandler放在这句之前和这句之后,先后顺序很明显。
    end else
    DefaultHandler(Msg); // 这句的作用是继续处理其它消息;
    end;

    procedure TForm1.WMCommand(var Message: TWMCommand);
    begin //第三种消息处理方式
    if Message.NotifyCode = BN_CLICKED then // 我怀疑可以拦截所有的消息?
    if FindControl(Message.Ctl) = Button1 then showmessage(‘点击了Button1’);
    inherited; // 屏蔽这句话的话,Button1Click的内容就不再执行了
    end;

    procedure TForm1.WndProc(var message: TMessage);
    begin
    if message.Msg=WM_ME then // 第二种消息处理方式,我怀疑可以拦截所有的消息?
    ShowMessage(IntToStr(Handle)+ 'WndProc');
    inherited WndProc(Message); // 这里inherited才会触发一次wmme消息;所以先执行WndProc,后执行wmme消息
    end;

    procedure TForm1.wmme(var message: TMessage);
    begin
    ShowMessage(IntToStr(Handle)+ 'wmme'); // 第一种消息处理方式
    end;

    end.

    ===================================================================

    unit Unit1;
    
    interface
    
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
    
    type
      TForm1 = class(TForm)
      private
        { Private declarations }
        procedure WndProc(var AMessage:TMessage);override;
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.WndProc(var AMessage: TMessage);
    begin
      if (AMessage.Msg=WM_SYSCOMMAND) and (AMessage.WParam=SC_CLOSE) then
        Exit
      else
        inherited;
    
    end;
    
    end.

     参考:http://www.cnblogs.com/key-ok/p/3417727.html

  • 相关阅读:
    Dump 文件生成与分析
    打造支持apk下载和html5缓存的 IIS(配合一个超简单的android APP使用)具体解释
    GridView编辑删除操作
    google域名邮箱申请 gmail域名邮箱申请(企业应用套件)指南
    nvl,空时的推断和取值
    如何将图片保存至自定义分组
    Java实现 蓝桥杯VIP 算法训练 集合运算
    Java实现 蓝桥杯VIP 算法训练 瓷砖铺放
    Java实现 蓝桥杯VIP 算法训练 瓷砖铺放
    Java实现 蓝桥杯VIP 算法训练 集合运算
  • 原文地址:https://www.cnblogs.com/findumars/p/3520126.html
Copyright © 2011-2022 走看看