Delphi不用DLL方式拦截全局外壳程序(Shell application)消息

拦截全局外壳程序窗体消息,一般都要用DLL创建WH_SHELL钩子,但有个非公开的API函数RegisterShellHookWindow,绑定一个handle句柄,然后注册SHELLHOOK消息后,就可以在handle句柄消息处理的过程中处理以下wParam值

  HSHELL_WINDOWCREATED = $0001;         // 系统级的窗体被创建
  HSHELL_WINDOWDESTROYED = $0002;       // 系统级的窗体即将被关闭
  HSHELL_ACTIVATESHELLWINDOW = $0003;   // SHELL 的主窗体将被激活
  //HSHELL_WINDOWACTIVATED = $0004;       // 系统级的窗体被激活
  HSHELL_GETMINRECT = $0005;            // 窗体被最大化或最小化
  HSHELL_REDRAW     = $0006;            // Windows 任务栏被刷新
  HSHELL_TASKMAN    = $0007;            // 任务列表的内容被选中
  HSHELL_LANGUAGE   = $0008;            // 中英文切换或输入法切换

 

实例如下:

unit Unit1;

interface

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

 
type
  TForm1 = class(TForm)
    procedure FormClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure AppMessage(var Msg: TMsg; var Handled: Boolean);
  end;

Function RegisterShellHookWindow(HWND: LongWord): Integer; stdcall;
external 'User32.dll' Name 'RegisterShellHookWindow';

var
  Form1: TForm1;
  Msg_ID: Cardinal;
implementation

{$R *.dfm}

procedure TForm1.AppMessage(var Msg: TMsg; var Handled: Boolean);
begin
  if msg.message=msg_id then
  if msg.wParam=HSHELL_windowactivated then
  begin
    //msg.LParam为激活窗口句柄
  end;

end;


procedure TForm1.FormCreate(Sender: TObject);
begin

Msg_ID := RegisterWindowMessage('SHELLHOOK');
RegisterShellHookWindow(application.Handle);
application.OnMessage:=self.AppMessage;
end;

发布了5 篇原创文章 · 获赞 3 · 访问量 9636
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览