Read selection from the active Explorer tab and use email feedback
This commit is contained in:
1 parent
3f9541c1b9
commit
0e84a1c452
15 files changed
+185
-53
No files matched your search
@@ -0,0 +1,114 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
internal static class ExplorerSelection
|
||||
{
|
||||
[DllImport("user32.dll")] static extern IntPtr GetForegroundWindow();
|
||||
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
|
||||
static extern IntPtr FindWindowEx(IntPtr parent, IntPtr after, string className, string? title);
|
||||
[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)]
|
||||
static extern bool IsWindowVisible(IntPtr window);
|
||||
|
||||
static readonly Guid TopLevelBrowser = new("4C96BE40-915C-11CF-99D3-00AA004AE837");
|
||||
static readonly Guid ShellBrowser = new("000214E2-0000-0000-C000-000000000046");
|
||||
|
||||
[ComImport, Guid("6D5140C1-7436-11CE-8034-00AA006009FA")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
interface IServiceProvider
|
||||
{
|
||||
[PreserveSig]
|
||||
int QueryService(in Guid service, in Guid iid, [MarshalAs(UnmanagedType.Interface)] out object? result);
|
||||
}
|
||||
|
||||
// IShellBrowser inherits IOleWindow. Only its first vtable method is needed.
|
||||
[ComImport, Guid("000214E2-0000-0000-C000-000000000046")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
interface IShellBrowser
|
||||
{
|
||||
[PreserveSig] int GetWindow(out IntPtr window);
|
||||
}
|
||||
|
||||
static IntPtr ActiveTab(IntPtr frame)
|
||||
{
|
||||
// Explorer places the active ShellTabWindowClass first in child z-order.
|
||||
// Skip hidden children during a tab switch; do not use the tab caption or path.
|
||||
IntPtr tab = IntPtr.Zero;
|
||||
while ((tab = FindWindowEx(frame, tab, "ShellTabWindowClass", null)) != IntPtr.Zero)
|
||||
if (IsWindowVisible(tab)) return tab;
|
||||
return IntPtr.Zero;
|
||||
}
|
||||
|
||||
static IntPtr TabOf(object window)
|
||||
{
|
||||
object? browser = null;
|
||||
try {
|
||||
if (window is not IServiceProvider provider) return IntPtr.Zero;
|
||||
if (provider.QueryService(TopLevelBrowser, ShellBrowser, out browser) < 0 || browser is not IShellBrowser shellBrowser)
|
||||
return IntPtr.Zero;
|
||||
return shellBrowser.GetWindow(out var tab) >= 0 ? tab : IntPtr.Zero;
|
||||
} finally { Release(browser); }
|
||||
}
|
||||
|
||||
static void Release(object? value)
|
||||
{
|
||||
if (value != null && Marshal.IsComObject(value)) Marshal.ReleaseComObject(value);
|
||||
}
|
||||
|
||||
internal static string[] Read()
|
||||
{
|
||||
IntPtr frame = GetForegroundWindow();
|
||||
if (frame == IntPtr.Zero) return [];
|
||||
IntPtr activeTab = ActiveTab(frame);
|
||||
object? shell = null, windows = null, selectedWindow = null;
|
||||
try {
|
||||
shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application")!);
|
||||
windows = ((dynamic)shell!).Windows();
|
||||
int count = ((dynamic)windows).Count;
|
||||
for (int index = 0; index < count; index++) {
|
||||
object? candidate = ((dynamic)windows).Item(index);
|
||||
try {
|
||||
if (candidate == null || new IntPtr((long)((dynamic)candidate).HWND) != frame) continue;
|
||||
// All tabs share IWebBrowser2.HWND. Match the tab's own shell
|
||||
// browser HWND before reading its selection, including empty selections.
|
||||
if (activeTab != IntPtr.Zero && TabOf(candidate) != activeTab) continue;
|
||||
// On systems without tab windows, only accept an unambiguous frame.
|
||||
if (selectedWindow != null) return [];
|
||||
selectedWindow = candidate;
|
||||
candidate = null;
|
||||
} catch (COMException) { }
|
||||
finally { Release(candidate); }
|
||||
}
|
||||
if (selectedWindow == null) return [];
|
||||
var files = ReadItems(selectedWindow);
|
||||
// A switch or close during COM calls must never use a stale tab's files.
|
||||
if (GetForegroundWindow() != frame || ActiveTab(frame) != activeTab) return [];
|
||||
return files;
|
||||
} catch { return []; }
|
||||
finally {
|
||||
Release(selectedWindow);
|
||||
Release(windows);
|
||||
Release(shell);
|
||||
}
|
||||
}
|
||||
|
||||
static string[] ReadItems(object window)
|
||||
{
|
||||
var files = new List<string>();
|
||||
object? document = null, items = null;
|
||||
try {
|
||||
document = ((dynamic)window).Document;
|
||||
items = ((dynamic)document).SelectedItems();
|
||||
int count = ((dynamic)items).Count;
|
||||
for (int index = 0; index < count; index++) {
|
||||
object? item = ((dynamic)items).Item(index);
|
||||
try {
|
||||
string file = ((dynamic)item!).Path;
|
||||
if (File.Exists(file)) files.Add(file);
|
||||
} finally { Release(item); }
|
||||
}
|
||||
return files.Distinct(StringComparer.OrdinalIgnoreCase).ToArray();
|
||||
} finally {
|
||||
Release(items);
|
||||
Release(document);
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-36
@@ -5,7 +5,6 @@ using Windows.ApplicationModel;
|
||||
|
||||
internal static class Program
|
||||
{
|
||||
[DllImport("user32.dll")] static extern IntPtr GetForegroundWindow();
|
||||
[DllImport("user32.dll")] static extern short GetAsyncKeyState(int key);
|
||||
[DllImport("user32.dll")] static extern bool GetCursorPos(out Point point);
|
||||
[DllImport("user32.dll")] static extern bool SetProcessDpiAwarenessContext(IntPtr context);
|
||||
@@ -80,39 +79,5 @@ internal static class Program
|
||||
Application.Run();
|
||||
}
|
||||
|
||||
static string[] Selection()
|
||||
{
|
||||
var files = new List<string>();
|
||||
object? shell = null, windows = null;
|
||||
try {
|
||||
shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application")!);
|
||||
windows = ((dynamic)shell!).Windows();
|
||||
IntPtr foreground = GetForegroundWindow();
|
||||
foreach (object window in (dynamic)windows) {
|
||||
object? document = null, items = null;
|
||||
try {
|
||||
if (new IntPtr((long)((dynamic)window).HWND) != foreground) continue;
|
||||
document = ((dynamic)window).Document;
|
||||
items = ((dynamic)document).SelectedItems();
|
||||
foreach (object item in (dynamic)items) {
|
||||
try {
|
||||
string file = ((dynamic)item).Path;
|
||||
if (File.Exists(file)) files.Add(file);
|
||||
} finally { Marshal.ReleaseComObject(item); }
|
||||
}
|
||||
break;
|
||||
} catch (COMException) { }
|
||||
finally {
|
||||
if (items != null) Marshal.ReleaseComObject(items);
|
||||
if (document != null) Marshal.ReleaseComObject(document);
|
||||
Marshal.ReleaseComObject(window);
|
||||
}
|
||||
}
|
||||
} catch { }
|
||||
finally {
|
||||
if (windows != null) Marshal.ReleaseComObject(windows);
|
||||
if (shell != null) Marshal.ReleaseComObject(shell);
|
||||
}
|
||||
return files.Distinct(StringComparer.OrdinalIgnoreCase).ToArray();
|
||||
}
|
||||
static string[] Selection() => ExplorerSelection.Read();
|
||||
}
|
||||
Reference in new issue
Block a user