[英]Get list of selected files from Windows Desktop
我正在嘗試從Windows桌面和資源管理器Windows獲取選定文件的列表。 要求是我應該能夠從活動資源管理器窗口或桌面檢索當前選擇。
在瀏覽了在線資源之后,我設法將以下代碼組合在一起,但是它沒有提供從桌面中選擇的項目的列表。
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);
}
}
}
除此之外,我嘗試了以下操作,但是它只提供了所有打開的資源管理器窗口中所有選定元素的列表,而忽略了桌面。
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);
}
}
因此,即使在沒有打開資源管理器窗口的情況下,我總是總是從資源管理器窗口獲得一個列表,並且沒有任何結果。 當前的技術似乎完全忽略了桌面。
如果有人可以幫助我從當前活動的窗口/桌面中獲取選定文件的列表,我將不勝感激。
謝謝。
桌面仍然很容易,因為它仍然是一個列表視圖,只需找到正確的句柄即可。 列表視圖是桌面句柄的子級。
Desktop
+- Progman (for backward compatibility)
+- Shell Def View
+- SysListView32 (even under 64 bit)
那么您可以在列表視圖上執行所有列表視圖操作。 但其他資源管理器窗口不包含列表視圖。 相反,他們使用帶有DirectUIHWND
類的window,這對許多人來說都是個謎。 我剛剛找到了一篇帖子,描述了揭開這個神秘面紗的方法。
http://smartbear.com/forums?forumid=81&threadid=68427#68428
希望對您有所幫助。
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);
}
}
我認為您應該在流程之間進行溝通。 以下主題將有所幫助。
這是從桌面檢索圖標的示例。 獲取桌面項目列表及其當前位置。 http://social.msdn.microsoft.com/Forums/windows/zh-CN/d7df8a4d-fc0f-4b62-80c9-7768756456e6/how-can-i-get-desktops-icons-information-?forum=winforms
這里的參數LVM_GETITEMSTATE可以在上面鏈接的示例代碼中使用。 http://msdn.microsoft.com/zh-cn/library/windows/desktop/bb761053(v=vs.85).aspx
祝好運..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.