-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathShellView.cs
More file actions
147 lines (117 loc) · 4.97 KB
/
Copy pathShellView.cs
File metadata and controls
147 lines (117 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
namespace ShellN.Extensions;
public sealed class ShellView : InterlockedComObject<IShellView>
{
private static readonly Lazy<ShellView?> _desktop = new(GetDesktop);
public static ShellView? Desktop => _desktop.Value;
public static ShellView? FromObject(object? obj, bool owned = true)
{
if (obj == null)
return null;
if (obj is nint unk)
{
var si = DirectN.Extensions.Com.ComObject.FromPointer<IShellView>(unk);
if (si != null)
return FromObject(si.Object, owned);
return null;
}
if (obj is ShellView view)
return view;
if (obj is IShellView shellView)
return new ShellView(null, shellView, owned);
return null;
}
public static IEnumerable<ShellView> Windows
{
get
{
CheckSTAThread();
using var windows = DirectN.Extensions.Com.ComObject.CoCreate<IShellWindows>(Constants.ShellWindows);
if (windows == null)
yield break;
windows.Object.get_Count(out var count);
for (var i = 0; i < count; i++)
{
using var idx = new Variant(i);
windows.Object.Item(idx.Detached, out var folderObj);
var folder = new ComObject<DirectN.IDispatch>(folderObj);
if (folder == null)
continue;
var sp = folder.As<DirectN.IServiceProvider>();
if (sp == null)
continue;
sp.Object.QueryService(Constants.SID_STopLevelBrowser, typeof(IShellBrowser).GUID, out var ppv);
var browser = DirectN.Extensions.Com.ComObject.FromPointer<IShellBrowser>(ppv);
if (browser == null)
continue;
browser.Object.QueryActiveShellView(out var shellView);
if (shellView == null)
continue;
var view = new ShellView(folder, shellView, true);
yield return view;
}
}
}
private static void CheckSTAThread()
{
if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
throw new InvalidOperationException("The current thread is not in STA mode.");
}
private static ShellView? GetDesktop()
{
CheckSTAThread();
using var windows = DirectN.Extensions.Com.ComObject.CoCreate<IShellWindows>(Constants.ShellWindows);
if (windows == null)
return null;
using var loc = new Variant(Constants.CSIDL_DESKTOP);
var hr = windows.Object.FindWindowSW(loc.Detached, new VARIANT(), ShellWindowTypeConstants.SWC_DESKTOP, out var hwnd, ShellWindowFindWindowOptions.SWFO_NEEDDISPATCH, out var obj);
if (obj == null)
return null;
using var disp = new ComObject<DirectN.IDispatch>(obj, releaseOnDispose: false);
var sp = disp.As<DirectN.IServiceProvider>();
if (sp == null)
return null;
hr = sp.Object.QueryService(Constants.SID_STopLevelBrowser, typeof(IShellBrowser).GUID, out var ppv);
var browser = DirectN.Extensions.Com.ComObject.FromPointer<IShellBrowser>(ppv);
if (browser == null)
return null;
browser.Object.QueryActiveShellView(out var view);
if (view == null)
return null;
return new ShellView(disp, view, false);
}
private ShellView(IComObject<DirectN.IDispatch>? dispatch, IShellView comObject, bool owned)
: this(dispatch, new ComObject<IShellView>(comObject), owned)
{
}
private ShellView(IComObject<DirectN.IDispatch>? dispatch, IComObject<IShellView> comObject, bool owned)
: base(comObject)
{
Dispatch = dispatch;
Owned = owned;
}
public IComObject<DirectN.IDispatch>? Dispatch { get; }
public bool Owned { get; }
public HWND Hwnd { get { NativeObject.GetWindow(out var hwnd); return hwnd; } }
public Window? Window => Window.FromHandle(Hwnd, false);
public ShellFolder? GetFolder(bool owned = true)
{
var folderView = ComObject.As<IFolderView>();
if (folderView == null)
return null;
folderView.Object.GetFolder(typeof(IShellFolder).GUID, out var ppv);
if (ppv == 0)
return null;
var shellFolder = DirectN.Extensions.Com.ComObject.ComWrappers.GetOrCreateObjectForComInstance(ppv, CreateObjectFlags.UniqueInstance);
if (shellFolder == null)
return null;
return ShellItem.FromObject(shellFolder, false, owned) as ShellFolder;
}
public HRESULT UIActivate(SVUIA_STATUS status, bool throwOnError = true) => NativeObject.UIActivate((uint)status).ThrowOnError(throwOnError);
public HRESULT Refresh(bool throwOnError = true) => NativeObject.Refresh().ThrowOnError(throwOnError);
protected override void Dispose(bool disposing)
{
if (!Owned)
return;
base.Dispose(disposing);
}
}