//hook例子源码
unit Main_Form_Unit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls;
type
TMainForm = class(TForm)
ListBox1: TListBox;
Panel1: TPanel;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
StaticText1: TStaticText;
procedure Button1Click(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure StaticText1Click(Sender: TObject);
private
function ExtractHwnd (const Text: string): THandle;
function ExtractTitle (const Text: string): string;
procedure UpdateEdit;
procedure CallEnumWindows;
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
uses ShellAPI;
function GetTitle(Hwnd: THandle
Param: Pointer): Boolean
stdcall;
var
TitleStr: string;
begin
SetLength (TitleStr, 300);
GetWindowText (Hwnd, PChar (TitleStr), 300);
MainForm.ListBox1.Items.Add (
IntToStr (Hwnd) + ': ' + TitleStr);
Result := True;
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
CallEnumWindows;
ListBox1.ItemIndex := 0;
UpdateEdit;
Button2.Enabled := True;
end;
function TMainForm.ExtractHwnd(const Text: string): THandle;
begin
Result := StrToInt (
Copy (Text, 1, Pos (':', Text) - 1));
end;
function TMainForm.ExtractTitle(const Text: string): string;
begin
Result := Copy (Text, Pos (':', Text) + 2, Length (Text));
end;
procedure TMainForm.ListBox1Click(Sender: TObject);
begin
UpdateEdit;
end;
procedure TMainForm.Button2Click(Sender: TObject);
var
OldItemIndex : Integer;
begin
SetWindowText (
ExtractHwnd (ListBox1.Items [ListBox1.ItemIndex]),
PChar (Edit1.Text));
OldItemIndex := ListBox1.ItemIndex;
CallEnumWindows;
ListBox1.ItemIndex := OldItemIndex;
UpdateEdit;
end;
procedure TMainForm.UpdateEdit;
begin
Edit1.Text := ExtractTitle (ListBox1.Items [ListBox1.ItemIndex]);
end;
procedure TMainForm.CallEnumWindows;
begin
ListBox1.Clear;
EnumWindows (@GetTitle, 0);
end;
end.