Главная » Delphi » Win API » как можно получить список выделенных фалов в windows explorer

как можно получить список выделенных фалов в windows explorer

16:32
function PrintEnumProc(Wnd: HWND; LParam: LPARAM): BOOL; stdcall;
const
  MaxTextSize = 1024;
var
  PrText: array[0..MaxTextSize] of char;
  i, count,state: integer;
  vBuffer: array[0..255] of Char;
  vProcessId: DWORD;
  vProcess: THandle;
  vPointer: Pointer;
  vNumberOfBytesRead: Cardinal;
  vItem: TLVItem;
begin
  Result := True;
  windows.GetClassName(Wnd, PrText, MaxTextSize);
  if PrText = 'SysListView32' then begin
    count := SendMessage(Wnd, LVM_GETITEMCOUNT, 0, 0);
    GetWindowThreadProcessId(Wnd, @vProcessId);
    vProcess := OpenProcess(PROCESS_VM_OPERATION or PROCESS_VM_READ or PROCESS_VM_WRITE, False, vProcessId);
    vPointer := VirtualAllocEx(vProcess, nil, 4096, MEM_RESERVE or MEM_COMMIT, PAGE_READWRITE);
    try
      for I := 0 to count - 1 do begin
        state:=SendMessage(Wnd, LVM_GETITEMSTATE, I, LVIS_SELECTED);
        if state=LVIS_SELECTED then begin
        with vItem do begin
          mask := LVIF_TEXT;
          iItem := I;
          iSubItem := 0;
          cchTextMax := SizeOf(vBuffer);
          pszText := Pointer(Cardinal(vPointer) + SizeOf(TLVItem));
        end;
        WriteProcessMemory(vProcess, vPointer, @vItem, SizeOf(TLVItem), vNumberOfBytesRead);
        SendMessage(Wnd, LVM_GETITEM, I, integer(vPointer));
        ReadProcessMemory(vProcess, Pointer(Cardinal(vPointer) + SizeOf(TLVItem)),
            @vBuffer[0], SizeOf(vBuffer), vNumberOfBytesRead);
        TMemo(Pointer(LParam)^).Lines.Add(vBuffer);
        end;
      end;
    finally
      VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);
      CloseHandle(vProcess)
    end;
    Result := false;
  end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  W: HWND;
begin
  Memo1.Clear;
  W := FindWindow('ExploreWClass', nil);
  if W = 0 then
    raise Exception.Create('Explorer window not found');
  EnumChildWindows(W, @PrintEnumProc, LPARAM(@memo1));
end;