unit Unit1;
interface
uses
ShellApi,
clipbrd,
shlobj,
Windows,
Messages,
SysUtils,
Variants,
Classes,
Graphics,
Controls,
Forms,
CommCtrl,
Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
FHookStarted : Boolean;
public
end;
var
Form1: TForm1;
JHook: THandle;
implementation
{$R *.dfm}
function SelectedFilename(LvHandle : Thandle) : String;
var Index : Integer;
Written : Cardinal;
cItemInfo : TLVITEM;
pItemInfo : PLVITEM;
pString : PChar;
Buffer : array[0..1023] of Char;
hProcess : THandle;
FPid : Cardinal;
begin
Result := '';
GetWindowThreadProcessID(LvHandle, FPid);
if FPid = 0 then Exit;
Index := SendMessage(LvHandle, LVM_GETITEMCOUNT, 0, 0);
ZeroMemory(@cItemInfo, SizeOf(cItemInfo));
hProcess := OpenProcess(PROCESS_ALL_ACCESS, False, FPid);
pItemInfo := VirtualAllocEx(hProcess, nil, SizeOf(cItemInfo), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
pString := VirtualAllocEx(hProcess, nil, 1024, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
try
while Index > 0 do
begin
Dec(Index);
if SendMessage(LvHandle, LVM_GETITEMSTATE, Index, LVIS_SELECTED) and LVIS_SELECTED <> 0 then
begin
if Assigned(pItemInfo) then
begin
cItemInfo.cchTextMax := 1024;
cItemInfo.pszText := pString;
cItemInfo.iSubItem := 0;
if WriteProcessMemory(hProcess, pItemInfo, @cItemInfo, SizeOf(cItemInfo), Written) then
begin
SendMessage(LvHandle, LVM_GETITEMTEXT, Index, Integer(pItemInfo));
ReadProcessMemory(hProcess, pString, @Buffer, 1024, Written);
Result := Buffer;
end;
end;
end;
end;
finally
if Assigned(pItemInfo) then
VirtualFreeEx(hProcess, pItemInfo, 0, MEM_RELEASE);
if Assigned(pString) then
VirtualFreeEx(hProcess, pString, 0, MEM_RELEASE);
end;
end;
function SameNames(Hwnd : THandle; ClassName, WindowName : string) : Boolean;
var Str: array[0..255] of char;
begin
Result := False;
GetClassName(Hwnd, Str, 255);
if AnsiSameText(ClassName, Str) then
begin
GetWindowText(Hwnd, Str, 255);
Result := AnsiSameText(WindowName, Str);
end;
end;
function ActiveCaption: string;
var
Handle: THandle;
Len: LongInt;
Title: string;
begin
Result := '';
Handle := GetForegroundWindow;
if Handle <> 0 then
begin
Len := GetWindowTextLength(Handle) + 1;
SetLength(Title, Len);
GetWindowText(Handle, PChar(Title), Len);
ActiveCaption := TrimRight(Title);
end;
end;
function FindCWindow(ParentHwnd : THandle; ClassName, WindowName : String) : THandle;
var Child : THandle;
begin
Result := 0;
Child := FindWindowEx(ParentHwnd, 0, nil, nil);
while (Child <> 0) and (Result = 0) do
begin
if SameNames(Child, ClassName, WindowName) then
begin
Result := Child;
Break;
end
else
Result := FindCWindow(Child, ClassName, WindowName);
if Result = 0 then
Child := FindWindowEx(ParentHwnd, Child, nil, nil);
end;
end;
function JournalProc(Code, wParam: Integer; var EventStrut: TEventMsg): Integer; stdcall;
var
P: TPoint;
Hwnd : THandle;
selectF: string;
begin
Result := CallNextHookEx(JHook, Code, wParam, Longint(@EventStrut));
if Code < 0 then Exit;
if Code = HC_SYSMODALON then Exit;
if Code = HC_ACTION then
begin
if EventStrut.message = WM_LBUTTONDOWN then
begin
///////////////////begin test line here
Hwnd := FindWindow(nil, pchar(activecaption));
Hwnd := FindCWindow(hwnd, 'SysListView32', 'FolderView');
selectF:=Format('%s', [SelectedFilename(Hwnd)]);
Form1.Caption := selectF;
/////////////////////////end test line here
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
JHook := SetWindowsHookEx(WH_JOURNALRECORD, @JournalProc, hInstance, 0);
if JHook > 0 then
begin
FHookStarted := True;
end
else
ShowMessage('No Journal Hook availible');
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FHookStarted := False;
UnhookWindowsHookEx(JHook);
JHook := 0;
end;
end.
interface
uses
ShellApi,
clipbrd,
shlobj,
Windows,
Messages,
SysUtils,
Variants,
Classes,
Graphics,
Controls,
Forms,
CommCtrl,
Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
FHookStarted : Boolean;
public
end;
var
Form1: TForm1;
JHook: THandle;
implementation
{$R *.dfm}
function SelectedFilename(LvHandle : Thandle) : String;
var Index : Integer;
Written : Cardinal;
cItemInfo : TLVITEM;
pItemInfo : PLVITEM;
pString : PChar;
Buffer : array[0..1023] of Char;
hProcess : THandle;
FPid : Cardinal;
begin
Result := '';
GetWindowThreadProcessID(LvHandle, FPid);
if FPid = 0 then Exit;
Index := SendMessage(LvHandle, LVM_GETITEMCOUNT, 0, 0);
ZeroMemory(@cItemInfo, SizeOf(cItemInfo));
hProcess := OpenProcess(PROCESS_ALL_ACCESS, False, FPid);
pItemInfo := VirtualAllocEx(hProcess, nil, SizeOf(cItemInfo), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
pString := VirtualAllocEx(hProcess, nil, 1024, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
try
while Index > 0 do
begin
Dec(Index);
if SendMessage(LvHandle, LVM_GETITEMSTATE, Index, LVIS_SELECTED) and LVIS_SELECTED <> 0 then
begin
if Assigned(pItemInfo) then
begin
cItemInfo.cchTextMax := 1024;
cItemInfo.pszText := pString;
cItemInfo.iSubItem := 0;
if WriteProcessMemory(hProcess, pItemInfo, @cItemInfo, SizeOf(cItemInfo), Written) then
begin
SendMessage(LvHandle, LVM_GETITEMTEXT, Index, Integer(pItemInfo));
ReadProcessMemory(hProcess, pString, @Buffer, 1024, Written);
Result := Buffer;
end;
end;
end;
end;
finally
if Assigned(pItemInfo) then
VirtualFreeEx(hProcess, pItemInfo, 0, MEM_RELEASE);
if Assigned(pString) then
VirtualFreeEx(hProcess, pString, 0, MEM_RELEASE);
end;
end;
function SameNames(Hwnd : THandle; ClassName, WindowName : string) : Boolean;
var Str: array[0..255] of char;
begin
Result := False;
GetClassName(Hwnd, Str, 255);
if AnsiSameText(ClassName, Str) then
begin
GetWindowText(Hwnd, Str, 255);
Result := AnsiSameText(WindowName, Str);
end;
end;
function ActiveCaption: string;
var
Handle: THandle;
Len: LongInt;
Title: string;
begin
Result := '';
Handle := GetForegroundWindow;
if Handle <> 0 then
begin
Len := GetWindowTextLength(Handle) + 1;
SetLength(Title, Len);
GetWindowText(Handle, PChar(Title), Len);
ActiveCaption := TrimRight(Title);
end;
end;
function FindCWindow(ParentHwnd : THandle; ClassName, WindowName : String) : THandle;
var Child : THandle;
begin
Result := 0;
Child := FindWindowEx(ParentHwnd, 0, nil, nil);
while (Child <> 0) and (Result = 0) do
begin
if SameNames(Child, ClassName, WindowName) then
begin
Result := Child;
Break;
end
else
Result := FindCWindow(Child, ClassName, WindowName);
if Result = 0 then
Child := FindWindowEx(ParentHwnd, Child, nil, nil);
end;
end;
function JournalProc(Code, wParam: Integer; var EventStrut: TEventMsg): Integer; stdcall;
var
P: TPoint;
Hwnd : THandle;
selectF: string;
begin
Result := CallNextHookEx(JHook, Code, wParam, Longint(@EventStrut));
if Code < 0 then Exit;
if Code = HC_SYSMODALON then Exit;
if Code = HC_ACTION then
begin
if EventStrut.message = WM_LBUTTONDOWN then
begin
///////////////////begin test line here
Hwnd := FindWindow(nil, pchar(activecaption));
Hwnd := FindCWindow(hwnd, 'SysListView32', 'FolderView');
selectF:=Format('%s', [SelectedFilename(Hwnd)]);
Form1.Caption := selectF;
/////////////////////////end test line here
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
JHook := SetWindowsHookEx(WH_JOURNALRECORD, @JournalProc, hInstance, 0);
if JHook > 0 then
begin
FHookStarted := True;
end
else
ShowMessage('No Journal Hook availible');
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FHookStarted := False;
UnhookWindowsHookEx(JHook);
JHook := 0;
end;
end.