AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Win32/Win64 API (native code) Delphi Window hook zum abfangen von größen Änderung
Thema durchsuchen
Ansicht
Themen-Optionen

Window hook zum abfangen von größen Änderung

Ein Thema von Black-Horse · begonnen am 29. Aug 2007
Antwort Antwort
Black-Horse

Registriert seit: 29. Aug 2007
9 Beiträge
 
#1

Window hook zum abfangen von größen Änderung

  Alt 29. Aug 2007, 15:52
Hallo,

ich habe folgendes Problem :
ich ahbe ein Fenster und möchte eine nachricht an mein Programm bekommen, sobald sich das Fenster in der größe oder position verändert hat (z.b. unten rechts ziehen). ich habe als testapplikation zum hooken TextPad verwendet. jedoch bekomme ich das event einfach nciht zurück. egal ob ich es mit WM_SIZE oder WM_PAINT oder SC_MINIMIZE oder WM_SIZING versucht habe. (eine abfrage die alle paar sekunden die Größe und die Position des Fensters checkt mit GetWindowPos möchte ich cniht)

hier mal mein source :

Unit1.pas aus Project1.dpr
Delphi-Quellcode:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    btnHook: TButton;
    edWndToHook: TEdit;
    lvMain: TListView;
    procedure btnHookClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }

  protected
    procedure _WMCopyData(var Msg: TWMCopyData); message WM_COPYDATA;

  public
    { Public declarations } 
  end;

var
  Form1: TForm1;
  WindowHandle: THandle;

  function InstallHook(DWThreadID: DWORD): boolean; stdcall external 'HookDLL.dll';
  procedure UnInstallHook; stdcall external 'HookDLL.dll

implementation

{$R *.dfm} 

function MyEnumWindowProc(Wnd: THandle; LParam: LongWord): boolean;
stdcall;
var
  WinCaption : string;
  Len: integer;
begin
  Result := True;
  Len := GetWindowTextLength(Wnd);
  SetLength(WinCaption, Len);
  GetWindowText(Wnd, PChar(WinCaption), Len+1);

  if (Copy(WinCaption, 0, 7) = 'TextPad') then
  begin
    WindowHandle := Wnd;
  end;
end;

function GetWindowHandleByExeName(const AExeName: string): THandle;
var
  SnapShot: THandle;
  p: TProcessEntry32;
  ProcessHandle: THandle;
begin
  Result := 0;
  WindowHandle := 0;
  ProcessHandle := 0;
  p.dwSize := SizeOf(p);
  SnapShot := CreateToolhelp32Snapshot(TH32CS_SnapProcess, 0);
  try
    if Process32First(SnapShot, p) then
      repeat
      if AnsiLowerCase(AExeName) = AnsiLowerCase(p.szExeFile) then
        ProcessHandle := p.th32ProcessID;
      until (ProcessHandle <> 0) or not Process32Next(SnapShot, p);
    EnumWindows(@MyEnumWindowProc, ProcessHandle);
    Result := WindowHandle;
  finally
    CloseHandle(SnapShot);
  end;
end;


procedure TForm1.btnHookClick(Sender: TObject);


begin


// GetWindowThreadProcessId(GetWindowHandleByExeName('Project1.exe'), nil)
  if not InstallHook(0) then
     raise Exception.Create('Error installing hook');

end;


procedure TForm1._WMCopyData(var Msg: TWMCopyData);
var
  aMsg: ^tagMsg;
  WinCaption : string;
  Wnd : THandle;
begin
  aMsg := Msg.CopyDataStruct.lpData;
  Wnd := aMsg^.HWND;

  SetLength(WinCaption, GetWindowTextLength(Wnd));
  GetWindowText(Wnd, PChar(WinCaption), GetWindowTextLength(Wnd)+1);
  if (Copy(WinCaption, 1, 7) = 'TextPad') then
  begin


  with lvMain.Items.Add do
  begin
    Caption := IntToStr(aMsg^.time);




    SubItems.Add(WinCaption);

    SubItems.Add(IntToStr(aMsg^.message));
    SubItems.Add(IntToStr(aMsg^.wParam));
    SubItems.Add(IntToStr(aMsg^.lParam));
  end;

  end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  UninstallHook;
end;

end.
und hier meine hookdll.dll

Delphi-Quellcode:
library HookDll;

uses
  Windows,
  Messages,
  SysUtils,
  Classes;

{$R *.res}

var
  G_hHook : THandle;

//------------------------------------------------------------------------------
 function MessageHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
  aMsg: ^tagMSG;
  aCDS: TCopyDataStruct;
  hWnd: THandle;
begin
  if (nCode < HC_ACTION) then
    Result := CallNextHookEx(G_hHook, nCode, wParam, lParam)
  else
  begin
    aMsg := pointer(lParam);


    aCDS.dwData := 0;
    aCDS.cbData := SizeOf(tagMsg);
    aCDS.lpData := aMsg;

    if aMsg^.message = WM_PAINT then // !!!
    begin
      hWnd := FindWindow(nil, 'Form1');
      SendMessage(hWnd, WM_COPYDATA, 0, integer(@aCDS));
    end;
// if aMsg^.message = WM_PRINTCLIENT then // !!!
// begin
// hWnd := FindWindow(nil, 'Form1');
// SendMessage(hWnd, WM_COPYDATA, 0, integer(@aCDS));
// end;
    {if aMsg^.message = SC_MINIMIZE then // !!!
    begin
      hWnd := FindWindow(nil, 'Form1');
      SendMessage(hWnd, WM_COPYDATA, 0, integer(@aCDS));
    end;
    if aMsg^.message = WM_PAINT then // !!!
    begin
      hWnd := FindWindow(nil, 'Form1');
      SendMessage(hWnd, WM_COPYDATA, 0, integer(@aCDS));
    end;}

    Result := CallNextHookEx(G_hHook, nCode, wParam, lParam);
  end;
end;


//------------------------------------------------------------------------------
function InstallHook(DWThreadID: DWORD): boolean; stdcall;
begin
  Result := False;

  if (G_hHook <> 0) then
  begin
    MessageBox(0, 'Hook already installed!', '', 0);
    Exit;
  end;

  G_hHook := SetWindowsHookEx(WH_GETMESSAGE, @MessageHookProc, GetModuleHandle('HookDLL.dll'), DWThreadID);
// G_hHook := SetWindowsHookEx(WH_GETMESSAGE, @MessageHookProc2, GetModuleHandle('HookDLL.dll'), DWThreadID);
// G_hHook := SetWindowsHookEx(WH_MOUSE, @MessageHookProc, GetModuleHandle('HookDLL.dll'), DWThreadID);
// G_hHook := SetWindowsHookEx(WH_KEYBOARD, @MessageHookProc, GetModuleHandle('HookDLL.dll'), DWThreadID);

  //ThreadID := DWThreadID;

  Result := (G_hHook <> 0);

  if Result then
     MessageBox(0, 'Hook successfully installed.', 'InstallHook', MB_ICONINFORMATION);
end;
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
procedure UnInstallHook; stdcall;
begin
  if (G_hHook <> 0) then
     UnhookWindowsHookEx(G_hHook);
  if (G_hHook2 <> 0) then
     UnhookWindowsHookEx(G_hHook2);
end;
//------------------------------------------------------------------------------

exports
  InstallHook,
  UnInstallHook;

begin
  G_hHook := 0;
end.
und zur ergänzung noch meine dfm
Delphi-Quellcode:
object Form1: TForm1
  Left = 342
  Top = 107
  Width = 687
  Height = 522
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnClose = FormClose
  PixelsPerInch = 96
  TextHeight = 13
  object btnHook: TButton
    Left = 8
    Top = 8
    Width = 97
    Height = 25
    Caption = 'Hook this window'
    TabOrder = 0
    OnClick = btnHookClick
  end
  object edWndToHook: TEdit
    Left = 112
    Top = 10
    Width = 561
    Height = 21
    Anchors = [akLeft, akTop, akRight]
    TabOrder = 1
    Text = 'notepad'
  end
  object lvMain: TListView
    Left = 8
    Top = 40
    Width = 665
    Height = 441
    Anchors = [akLeft, akTop, akRight, akBottom]
    Columns = <
      item
        Caption = 'Time'
      end
      item
        Caption = 'FensterID'
        Width = 150
      end
      item
        Caption = 'Message'
        Width = 100
      end
      item
        Caption = 'wParam'
        Width = 100
      end
      item
        AutoSize = True
        Caption = 'lParam'
      end>
    TabOrder = 2
    ViewStyle = vsReport
  end
end
weiß irgendwer wie ich an die events ran komme und welche das sind? ich habe schon einige zeit bei msdn gesucht, jedoch keine wirkliche lösung gefunden.


Viel Dank im Voraus
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 14:46 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz