Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#12

AW: Fremdes Fenster unverschiebbar (Wie erhalte ich die Events?)

  Alt 12. Dez 2011, 15:02
Das mit dem globalen Hook (WH_CALLWNDPROC) würde mich ja mal brennend interessieren, allerdings werden nur die Nachrichten vom aufrufenden Programm abgefangen, alle Nachrichten an fremde Fenster kommen nicht an.

Weiß jemand warum?

Hook-DLL
Delphi-Quellcode:
library HookCallWndProc;

uses
  Windows,
  Messages;

{$R *.res}

type
  TMyCallBack = procedure( nCode : Integer; WPARAM : WPARAM; LPARAM : LPARAM ); stdcall;

var
  HookHandle : Cardinal = 0;
  FCallBack : TMyCallBack = nil;

function CallWndProcHookProc( nCode : Integer; WPARAM : WPARAM; LPARAM : LPARAM ) : LRESULT; stdcall;
begin
  Result := CallNextHookEx( HookHandle, nCode, WPARAM, LPARAM );

  if nCode = HC_ACTION
  then
    if Assigned( FCallBack )
    then
      FCallBack( nCode, WPARAM, LPARAM );

end;

function InstallHook( aCallBack : TMyCallBack ) : Boolean; stdcall;
begin
  Result := False;
  if HookHandle = 0
  then
    begin

      HookHandle := SetWindowsHookEx( WH_CALLWNDPROC, @CallWndProcHookProc, HInstance, 0 );

      FCallBack := aCallBack;

      Result := True;

    end;
end;

function UninstallHook : Boolean; stdcall;
begin
  Result := UnhookWindowsHookEx( HookHandle );
  FCallBack := nil;
  HookHandle := 0;
end;

exports

  InstallHook,
  UninstallHook;

end.
GUI:
Delphi-Quellcode:
unit Main.View;

interface

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

type
  TForm1 = class( TForm )
    ListBox1 : TListBox;
    procedure FormDestroy( Sender : TObject );
    procedure FormCreate( Sender : TObject );
  private
  public
  end;

var
  Form1 : TForm1;

implementation

type
  TMyCallBack = procedure( nCode : Integer; WPARAM : WPARAM; LPARAM : LPARAM ); stdcall;

function InstallHook( CallBack : TMyCallBack ) : Boolean; stdcall; external 'HookCallWndProc.dll';
function UninstallHook( ) : Boolean; stdcall; external 'HookCallWndProc.dll';

{$R *.dfm}

procedure HookCallWndProc( nCode : Integer; WPARAM : WPARAM; LPARAM : LPARAM ); stdcall;
var
  cwps : TCWPStruct;
  szClass : array [0 .. 80] of Char;
begin
  if nCode = HC_ACTION
  then
    begin
      CopyMemory( @cwps, Pointer( LPARAM ), SizeOf( CWPSTRUCT ) );
      GetClassName( cwps.hwnd, szClass, Length( szClass ) - 1 );
      case cwps.message of
        WM_WINDOWPOSCHANGING :
          begin
            Form1.ListBox1.ItemIndex := Form1.ListBox1.Items.Add( 'WINDOWSPOSCHANGING ' + szClass );
          end;
      end;

    end;
end;

procedure TForm1.FormCreate( Sender : TObject );
begin
  InstallHook( HookCallWndProc );
end;

procedure TForm1.FormDestroy( Sender : TObject );
begin
  UninstallHook;
end;

end.
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat