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.

时间: 2024-10-21 05:01:39

Application.HookMainWindow完全替代了原来的窗口过程(但是好像也会继续传递)的相关文章

Delphi 自定义窗口过程WinProc

unit ScWndProc; interface uses Forms, Messages; const DDGM_FOOMSG = WM_USER; //自定义消息 implementation uses windows,sysutils,Dialogs; var WProc : Pointer; function NewWndProc(handle: hWnd; msg,wParam,lParam: LongInt): LongInt ; stdcall; begin if msg = D

windows 窗口过程 线程消息队列

message loop window procedure message loop: a for loop in thread or winmain  GetMessage, TranslateMessage, and DispatchMessage GetMessage: get the message DispatchMessage: Dispatches a message to a window procedure. ----- winproc WindowProc callback

juce中真正的窗口过程

函数所在的具体位置. LRESULT peerWindowProc (HWND h, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { //============================================================================== case WM_NCHITTEST: if ((styleFlags & windowIgnoresMouseClicks

动态创建窗口,并重写窗口过程

在窗口过程中处理滚动条消息 WNDPROC pSubclassOldEditProc; INT iVScrollPos = 0, iVTextPos = 0; LRESULT winProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { //获取新设置的窗口属性,就是获取CNewEdit的指针,方便后面访问m_lOldProc这个成员 if (message == WM_VSCROLL) { //TRACE( _T("%d\n&

1.3 Writing the Window Procedure(编写窗口过程)

编写窗口过程(Writing the Window Procedure) DispatchMessage函数调用作为消息目标窗口的窗口过程.窗口过程具有以下签名(signature). C++ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); 4个参数: hwnd 是对应窗口的一个句柄. uMsg是消息代码:例如, WM_SIZE消息表明窗口被调整了大小. wParam和lParam包

Application HookMainWindow

//H Filebool __fastcall AppHookFunc(TMessage &Message); //cpp file void __fastcall TForm2::FormCreate(TObject *Sender) { Application->HookMainWindow(this->AppHookFunc); } // -----------------------------------------------------------------------

线程给主窗口发事件(消息),传递字符串

代码来自安晓辉: #ifndef CUSTOMEVENT_H #define CUSTOMEVENT_H #include <QEvent> #include <QString> class CustomEvent : public QEvent // 自定义事件 { public: CustomEvent(const QString & msg); static QEvent::Type m_eventType; // 此事件的类型 static QEvent::Type

打开窗口过程

procedure TForm1.Open_Tab_Form(ClassType:TComponentClass;OpenFrom:TForm);var ThePanel:Tpanel; Tabsheet1: TTabSheet;begin //动态创建tabsheet和panel TabSheet1 := TTabSheet.Create(Self); TabSheet1.PageControl := PageControl1; ThePanel:=Tpanel.Create(self); T

Delphi自定义窗口过程WinProc

unit ScWndProc; interface uses Forms, Messages; const DDGM_FOOMSG = WM_USER; //自定义消息 implementation uses windows,sysutils,Dialogs; var WProc : Pointer; function NewWndProc(handle: hWnd; msg,wParam,lParam: LongInt): LongInt ; stdcall; begin if msg = D