zoukankan      html  css  js  c++  java
  • Application.HookMainWindow完全替代了原来的窗口过程(但是好像也会继续传递)

    unit HookMain;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, ExtCtrls;
    
    type
      THookForm = class(TForm)
        SendBtn: TButton;
        GroupBox1: TGroupBox;
        LogList: TListBox;
        DoLog: TCheckBox;
        ExitBtn: TButton;
        Button1: TButton;
        procedure SendBtnClick(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure ExitBtnClick(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        function AppWindowHook(var Message: TMessage): Boolean;
      end;
    
    var
      HookForm: THookForm;
    
    implementation
    
    {$R *.DFM}
    
    procedure THookForm.FormCreate(Sender: TObject);
    begin
      Application.HookMainWindow(AppWindowHook);
    end;
    
    procedure THookForm.FormDestroy(Sender: TObject);
    begin
      Application.UnhookMainWindow(AppWindowHook);
    end;
    
    function THookForm.AppWindowHook(var Message: TMessage): Boolean;
    const
      LogStr = 'Message ID: $%x, WParam: $%x, LParam: $%x';
    begin
      Result := True;
      if DoLog.Checked then
        with Message do
          LogList.Items.Add(Format(LogStr, [Msg, WParam, LParam]));
    end;
    
    procedure THookForm.SendBtnClick(Sender: TObject);
    begin
      SendMessage(Application.Handle, WM_NULL, 0, 0);
    end;
    
    procedure THookForm.ExitBtnClick(Sender: TObject);
    begin
      Close;
    end;
    
    procedure THookForm.Button1Click(Sender: TObject);
    begin
        ShowMessage('ddd');
    end;
    
    end.

    理论解释:

    Enables a native Windows dialog box to receive messages sent to the application's main window.

    Use HookMainWindow to ensure that a native Windows dialog box behaves correctly as a child of the application, not as a stand-alone window. For example, switching among applications with Alt+Tab treats the application as a single task after calling HookMainWindow, rather than treating the native Windows dialog box as a separate task.

    When the window identified by the Handle property receives relevant dialog messages, it passes them to the dialog procedure passed as the Hook parameter.

    There is no problem with leaving a dialog box hooked into the main window, even for extended periods. However, should the dialog box close, call the UnhookMainWindow method to release the hook.

  • 相关阅读:
    全文搜索引擎 Elasticsearch 入门教程
    什么是网络爬虫?
    如何更高效的使用谷歌解决问题
    python内置函数(2)-递归与迭代
    python内置函数(1)
    Life is short, you need Python
    统计单词个数及词频(C++实现)
    计算城市间的球面距离(C++实现)
    C++实现树的基本操作,界面友好,操作方便,运行流畅,运用模板
    C++,利用链式栈实现括号匹配,界面友好,操作方便,运行流畅
  • 原文地址:https://www.cnblogs.com/findumars/p/4966103.html
Copyright © 2011-2022 走看看