forked from ReneLergner/WPinternals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFolderSelectDialog.cs
338 lines (294 loc) · 11.8 KB
/
FolderSelectDialog.cs
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// This class was found online.
// Original author is probably: Swizzy
// https://github.com/ttgxdinger/Random/blob/master/CPUKey%20Checker/CPUKey%20Checker/FolderSelectDialog.cs
using System;
using System.Windows.Forms;
using System.Reflection;
namespace WPinternals
{
/// <summary>
/// Wraps System.Windows.Forms.OpenFileDialog to make it present
/// a vista-style dialog.
/// </summary>
public class FolderSelectDialog
{
// Wrapped dialog
System.Windows.Forms.OpenFileDialog ofd = null;
/// <summary>
/// Default constructor
/// </summary>
public FolderSelectDialog()
{
ofd = new System.Windows.Forms.OpenFileDialog();
ofd.Filter = "Folders|\n";
ofd.AddExtension = false;
ofd.CheckFileExists = false;
ofd.DereferenceLinks = true;
ofd.Multiselect = false;
}
#region Properties
/// <summary>
/// Gets/Sets the initial folder to be selected. A null value selects the current directory.
/// </summary>
public string InitialDirectory
{
get { return ofd.InitialDirectory; }
set { ofd.InitialDirectory = value == null || value.Length == 0 ? Environment.CurrentDirectory : value; }
}
/// <summary>
/// Gets/Sets the title to show in the dialog
/// </summary>
public string Title
{
get { return ofd.Title; }
set { ofd.Title = value == null ? "Select a folder" : value; }
}
/// <summary>
/// Gets the selected folder
/// </summary>
public string FileName
{
get { return ofd.FileName; }
}
#endregion
#region Methods
/// <summary>
/// Shows the dialog
/// </summary>
/// <returns>True if the user presses OK else false</returns>
public bool ShowDialog()
{
return ShowDialog(IntPtr.Zero);
}
/// <summary>
/// Shows the dialog
/// </summary>
/// <param name="hWndOwner">Handle of the control to be parent</param>
/// <returns>True if the user presses OK else false</returns>
public bool ShowDialog(IntPtr hWndOwner)
{
bool flag = false;
if (Environment.OSVersion.Version.Major >= 6)
{
var r = new Reflector("System.Windows.Forms");
uint num = 0;
Type typeIFileDialog = r.GetType("FileDialogNative.IFileDialog");
object dialog = r.Call(ofd, "CreateVistaDialog");
r.Call(ofd, "OnBeforeVistaDialog", dialog);
uint options = (uint)r.CallAs(typeof(System.Windows.Forms.FileDialog), ofd, "GetOptions");
options |= (uint)r.GetEnum("FileDialogNative.FOS", "FOS_PICKFOLDERS");
r.CallAs(typeIFileDialog, dialog, "SetOptions", options);
object pfde = r.New("FileDialog.VistaDialogEvents", ofd);
object[] parameters = new object[] { pfde, num };
r.CallAs2(typeIFileDialog, dialog, "Advise", parameters);
num = (uint)parameters[1];
try
{
int num2 = (int)r.CallAs(typeIFileDialog, dialog, "Show", hWndOwner);
flag = 0 == num2;
}
finally
{
r.CallAs(typeIFileDialog, dialog, "Unadvise", num);
GC.KeepAlive(pfde);
}
}
else
{
var fbd = new FolderBrowserDialog();
fbd.Description = this.Title;
fbd.SelectedPath = this.InitialDirectory;
fbd.ShowNewFolderButton = false;
if (fbd.ShowDialog(new WindowWrapper(hWndOwner)) != DialogResult.OK) return false;
ofd.FileName = fbd.SelectedPath;
flag = true;
}
return flag;
}
#endregion
}
/// <summary>
/// Creates IWin32Window around an IntPtr
/// </summary>
public class WindowWrapper : System.Windows.Forms.IWin32Window
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="handle">Handle to wrap</param>
public WindowWrapper(IntPtr handle)
{
_hwnd = handle;
}
/// <summary>
/// Original ptr
/// </summary>
public IntPtr Handle
{
get { return _hwnd; }
}
private IntPtr _hwnd;
}
/// <summary>
/// This class is from the Front-End for Dosbox and is used to present a 'vista' dialog box to select folders.
/// Being able to use a vista style dialog box to select folders is much better then using the shell folder browser.
/// http://code.google.com/p/fed/
///
/// Example:
/// var r = new Reflector("System.Windows.Forms");
/// </summary>
public class Reflector
{
#region variables
string m_ns;
Assembly m_asmb;
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
/// <param name="ns">The namespace containing types to be used</param>
public Reflector(string ns)
: this(ns, ns)
{ }
/// <summary>
/// Constructor
/// </summary>
/// <param name="an">A specific assembly name (used if the assembly name does not tie exactly with the namespace)</param>
/// <param name="ns">The namespace containing types to be used</param>
public Reflector(string an, string ns)
{
m_ns = ns;
m_asmb = null;
foreach (AssemblyName aN in Assembly.GetExecutingAssembly().GetReferencedAssemblies())
{
if (aN.FullName.StartsWith(an))
{
m_asmb = Assembly.Load(aN);
break;
}
}
}
#endregion
#region Methods
/// <summary>
/// Return a Type instance for a type 'typeName'
/// </summary>
/// <param name="typeName">The name of the type</param>
/// <returns>A type instance</returns>
public Type GetType(string typeName)
{
Type type = null;
string[] names = typeName.Split('.');
if (names.Length > 0)
type = m_asmb.GetType(m_ns + "." + names[0]);
for (int i = 1; i < names.Length; ++i)
{
type = type.GetNestedType(names[i], BindingFlags.NonPublic);
}
return type;
}
/// <summary>
/// Create a new object of a named type passing along any params
/// </summary>
/// <param name="name">The name of the type to create</param>
/// <param name="parameters"></param>
/// <returns>An instantiated type</returns>
public object New(string name, params object[] parameters)
{
Type type = GetType(name);
ConstructorInfo[] ctorInfos = type.GetConstructors();
foreach (ConstructorInfo ci in ctorInfos)
{
try
{
return ci.Invoke(parameters);
}
catch { }
}
return null;
}
/// <summary>
/// Calls method 'func' on object 'obj' passing parameters 'parameters'
/// </summary>
/// <param name="obj">The object on which to excute function 'func'</param>
/// <param name="func">The function to execute</param>
/// <param name="parameters">The parameters to pass to function 'func'</param>
/// <returns>The result of the function invocation</returns>
public object Call(object obj, string func, params object[] parameters)
{
return Call2(obj, func, parameters);
}
/// <summary>
/// Calls method 'func' on object 'obj' passing parameters 'parameters'
/// </summary>
/// <param name="obj">The object on which to excute function 'func'</param>
/// <param name="func">The function to execute</param>
/// <param name="parameters">The parameters to pass to function 'func'</param>
/// <returns>The result of the function invocation</returns>
public object Call2(object obj, string func, object[] parameters)
{
return CallAs2(obj.GetType(), obj, func, parameters);
}
/// <summary>
/// Calls method 'func' on object 'obj' which is of type 'type' passing parameters 'parameters'
/// </summary>
/// <param name="type">The type of 'obj'</param>
/// <param name="obj">The object on which to excute function 'func'</param>
/// <param name="func">The function to execute</param>
/// <param name="parameters">The parameters to pass to function 'func'</param>
/// <returns>The result of the function invocation</returns>
public object CallAs(Type type, object obj, string func, params object[] parameters)
{
return CallAs2(type, obj, func, parameters);
}
/// <summary>
/// Calls method 'func' on object 'obj' which is of type 'type' passing parameters 'parameters'
/// </summary>
/// <param name="type">The type of 'obj'</param>
/// <param name="obj">The object on which to excute function 'func'</param>
/// <param name="func">The function to execute</param>
/// <param name="parameters">The parameters to pass to function 'func'</param>
/// <returns>The result of the function invocation</returns>
public object CallAs2(Type type, object obj, string func, object[] parameters)
{
MethodInfo methInfo = type.GetMethod(func, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
return methInfo.Invoke(obj, parameters);
}
/// <summary>
/// Returns the value of property 'prop' of object 'obj'
/// </summary>
/// <param name="obj">The object containing 'prop'</param>
/// <param name="prop">The property name</param>
/// <returns>The property value</returns>
public object Get(object obj, string prop)
{
return GetAs(obj.GetType(), obj, prop);
}
/// <summary>
/// Returns the value of property 'prop' of object 'obj' which has type 'type'
/// </summary>
/// <param name="type">The type of 'obj'</param>
/// <param name="obj">The object containing 'prop'</param>
/// <param name="prop">The property name</param>
/// <returns>The property value</returns>
public object GetAs(Type type, object obj, string prop)
{
PropertyInfo propInfo = type.GetProperty(prop, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
return propInfo.GetValue(obj, null);
}
/// <summary>
/// Returns an enum value
/// </summary>
/// <param name="typeName">The name of enum type</param>
/// <param name="name">The name of the value</param>
/// <returns>The enum value</returns>
public object GetEnum(string typeName, string name)
{
Type type = GetType(typeName);
FieldInfo fieldInfo = type.GetField(name);
return fieldInfo.GetValue(null);
}
#endregion
}
}