2013-09-30 17 views
8

Sto cercando di ottenere un elenco di file selezionati dal desktop di Windows e da Esplora risorse di Windows. Il requisito è che dovrei essere in grado di recuperare la selezione corrente dalla finestra di Explorer attiva o dal Desktop.Ottieni l'elenco dei file selezionati da Windows Desktop

Sono riuscito a mettere insieme il seguente codice, dopo aver attraversato le risorse online, ma non fornisce un elenco di elementi selezionati dal desktop.

ArrayList selected = new ArrayList(); 
var shell = new Shell32.Shell(); 
IntPtr handle = IntPtr.Zero; 
handle = GetForegroundWindow(); 
int intHandle = handle.ToInt32(); 

//For each explorer 
foreach (InternetExplorer window in new ShellWindowsClass()) 
{ 

    if (window.HWND == (int)handle) 
    { 
     Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems(); 
     foreach (Shell32.FolderItem item in items) 
     { 
      selected.Add(item.Path); 
     } 
    } 
} 

Oltre a questo, ho provato quanto segue ma dà solo un elenco di tutti gli elementi selezionati in tutte le finestre aperte explorer ignorando il desktop.

string filename; = Path.GetFileNameWithoutExtension(window.FullName).ToLower(); 
if (filename.ToLowerInvariant() == "explorer") 
{ 
    Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems(); 
    foreach (Shell32.FolderItem item in items) 
    { 
     //MessageBox.Show(item.Path.ToString()); 
     selected.Add(item.Path); 
    } 
} 

Così ho appena finisco sempre con una lista da Windows Explorer e ottenere alcun risultato, anche quando non ci sono le finestre di Explorer sono aperte. Le tecniche attuali sembrano ignorare del tutto il desktop.

Apprezzerei molto se qualcuno potesse aiutarmi a ottenere un elenco di file selezionati dalla finestra/desktop attualmente attiva.

Grazie.

+0

potrebbe ottenere file (s) selezionato sul desktop? Se sì, potresti dirmi come farlo? Per favore! – GSP

risposta

1

È facile per desktop poiché è ancora un listview, basta trovare la maniglia corretta. la visualizzazione elenco è figlio dell'handle del desktop.

Desktop 
+- Progman (for backward compatibility) 
    +- Shell Def View 
     +- SysListView32 (even under 64 bit) 

quindi è possibile eseguire tutte le operazioni di listview nella visualizzazione elenco. ma altre finestre di explorer non contengono una vista elenco. Invece usano la finestra con la classe DirectUIHWND che è un mistero per molti. Ho appena trovato un post che descrive un modo per svelare quel mistero.

http://smartbear.com/forums?forumid=81&threadid=68427#68428

spero che aiuta.

0

Penso che dovresti comunicare tra i processi. I seguenti argomenti aiuteranno.

Questo è un esempio di recupero di icone dal desktop. Viene recuperata la lista degli oggetti del desktop e le loro posizioni attuali. http://social.msdn.microsoft.com/Forums/windows/en-US/d7df8a4d-fc0f-4b62-80c9-7768756456e6/how-can-i-get-desktops-icons-information-?forum=winforms

Qui il parametro LVM_GETITEMSTATE può essere utilizzato all'interno del codice di esempio dal collegamento sopra riportato. http://msdn.microsoft.com/en-us/library/windows/desktop/bb761053(v=vs.85).aspx

Buona fortuna ..

1
using System.Runtime.InteropServices; 

    public class ShellItems 
    { 
     [StructLayoutAttribute(LayoutKind.Sequential)] 
     private struct LVITEM 
     { 
      public uint mask; 
      public int iItem; 
      public int iSubItem; 
      public uint state; 
      public uint stateMask; 
      public IntPtr pszText; 
      public int cchTextMax; 
      public int iImage; 
      public IntPtr lParam; 
     } 

     const int LVM_FIRST = 0x1000; 
     const int LVM_GETSELECTEDCOUNT = 4146; 
     const int LVM_GETNEXTITEM = LVM_FIRST + 12; 
     const int LVNI_SELECTED = 2; 
     const int LVM_GETITEMCOUNT = LVM_FIRST + 4; 
     const int LVM_GETITEM = LVM_FIRST + 75; 
     const int LVIF_TEXT = 0x0001; 

     [DllImport("user32.dll", EntryPoint = "GetShellWindow")] 
     public static extern System.IntPtr GetShellWindow(); 

     [DllImport("user32.dll", SetLastError = true)] 
     public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle); 

     [DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)] 
     public static extern int SendMessagePtr(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam); 

     [DllImport("User32.DLL")] 
     public static extern int SendMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam); 


     public int SelectedItemCount 
     { 
      get 
      { 
       return SendMessage(ShellListViewHandle, LVM_GETSELECTEDCOUNT, IntPtr.Zero.ToInt32(), IntPtr.Zero.ToInt32()); 
      } 
     } 
     public int Count 
     { 
      get 
      { 
       return SendMessage(ShellListViewHandle, LVM_GETITEMCOUNT, IntPtr.Zero.ToInt32(), IntPtr.Zero.ToInt32()); 
      } 
     } 
     public string GetItemText(int idx) 
     { 
      // Declare and populate the LVITEM structure 
      LVITEM lvi = new LVITEM(); 
      lvi.mask = LVIF_TEXT; 
      lvi.cchTextMax = 512; 
      lvi.iItem = idx;   // the zero-based index of the ListView item 
      lvi.iSubItem = 0;   // the one-based index of the subitem, or 0 if this 
      // structure refers to an item rather than a subitem 
      lvi.pszText = Marshal.AllocHGlobal(512); 

      // Send the LVM_GETITEM message to fill the LVITEM structure 
      IntPtr ptrLvi = Marshal.AllocHGlobal(Marshal.SizeOf(lvi)); 
      Marshal.StructureToPtr(lvi, ptrLvi, false); 
      try 
      { 
       SendMessagePtr(ShellListViewHandle, LVM_GETITEM, IntPtr.Zero, ptrLvi); 
      } 
      catch (Exception ex) 
      { 
       System.Diagnostics.Debug.WriteLine(ex.Message); 
      } 

      // Extract the text of the specified item 
      string itemText = Marshal.PtrToStringAuto(lvi.pszText); 
      return itemText; 
     } 

     IntPtr ShellListViewHandle 
     { 
      get 
      { 
       IntPtr _ProgMan = GetShellWindow(); 
       IntPtr _SHELLDLL_DefViewParent = _ProgMan; 
       IntPtr _SHELLDLL_DefView = FindWindowEx(_ProgMan, IntPtr.Zero, "SHELLDLL_DefView", null); 
       IntPtr _SysListView32 = FindWindowEx(_SHELLDLL_DefView, IntPtr.Zero, "SysListView32", "FolderView"); 
       return _SysListView32; 
      } 
     } 

     public int GetSelectedItemIndex(int iPos = -1) 
     { 
      return SendMessage(ShellListViewHandle, LVM_GETNEXTITEM, iPos, LVNI_SELECTED); 
     } 
    } 
+1

Restituisce il conteggio degli articoli e il conteggio delle voci selezionate, ma si blocca su GetItemText, qualche idea? –

Problemi correlati