sebus

BuildDialog.psm1

Mar 7th, 2019
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. Function BuildDialog {
  2. $sourcecode = @"
  3. using System;
  4. using System.Windows.Forms;
  5. using System.Reflection;
  6. namespace FolderSelect
  7. {
  8. public class FolderSelectDialog
  9. {
  10. System.Windows.Forms.OpenFileDialog ofd = null;
  11. public FolderSelectDialog()
  12. {
  13. ofd = new System.Windows.Forms.OpenFileDialog();
  14. ofd.Filter = "Folders|\n";
  15. ofd.AddExtension = false;
  16. ofd.CheckFileExists = false;
  17. ofd.DereferenceLinks = true;
  18. ofd.Multiselect = false;
  19. }
  20. public string InitialDirectory
  21. {
  22. get { return ofd.InitialDirectory; }
  23. set { ofd.InitialDirectory = value == null || value.Length == 0 ? Environment.CurrentDirectory : value; }
  24. }
  25. public string Title
  26. {
  27. get { return ofd.Title; }
  28. set { ofd.Title = value == null ? "Select a folder" : value; }
  29. }
  30. public string FileName
  31. {
  32. get { return ofd.FileName; }
  33. }
  34. public bool ShowDialog()
  35. {
  36. return ShowDialog(IntPtr.Zero);
  37. }
  38. public bool ShowDialog(IntPtr hWndOwner)
  39. {
  40. bool flag = false;
  41.  
  42. if (Environment.OSVersion.Version.Major >= 6)
  43. {
  44. var r = new Reflector("System.Windows.Forms");
  45. uint num = 0;
  46. Type typeIFileDialog = r.GetType("FileDialogNative.IFileDialog");
  47. object dialog = r.Call(ofd, "CreateVistaDialog");
  48. r.Call(ofd, "OnBeforeVistaDialog", dialog);
  49. uint options = (uint)r.CallAs(typeof(System.Windows.Forms.FileDialog), ofd, "GetOptions");
  50. options |= (uint)r.GetEnum("FileDialogNative.FOS", "FOS_PICKFOLDERS");
  51. r.CallAs(typeIFileDialog, dialog, "SetOptions", options);
  52. object pfde = r.New("FileDialog.VistaDialogEvents", ofd);
  53. object[] parameters = new object[] { pfde, num };
  54. r.CallAs2(typeIFileDialog, dialog, "Advise", parameters);
  55. num = (uint)parameters[1];
  56. try
  57. {
  58. int num2 = (int)r.CallAs(typeIFileDialog, dialog, "Show", hWndOwner);
  59. flag = 0 == num2;
  60. }
  61. finally
  62. {
  63. r.CallAs(typeIFileDialog, dialog, "Unadvise", num);
  64. GC.KeepAlive(pfde);
  65. }
  66. }
  67. else
  68. {
  69. var fbd = new FolderBrowserDialog();
  70. fbd.Description = this.Title;
  71. fbd.SelectedPath = this.InitialDirectory;
  72. fbd.ShowNewFolderButton = false;
  73. if (fbd.ShowDialog(new WindowWrapper(hWndOwner)) != DialogResult.OK) return false;
  74. ofd.FileName = fbd.SelectedPath;
  75. flag = true;
  76. }
  77. return flag;
  78. }
  79. }
  80. public class WindowWrapper : System.Windows.Forms.IWin32Window
  81. {
  82. public WindowWrapper(IntPtr handle)
  83. {
  84. _hwnd = handle;
  85. }
  86. public IntPtr Handle
  87. {
  88. get { return _hwnd; }
  89. }
  90.  
  91. private IntPtr _hwnd;
  92. }
  93. public class Reflector
  94. {
  95. string m_ns;
  96. Assembly m_asmb;
  97. public Reflector(string ns)
  98. : this(ns, ns)
  99. { }
  100. public Reflector(string an, string ns)
  101. {
  102. m_ns = ns;
  103. m_asmb = null;
  104. foreach (AssemblyName aN in Assembly.GetExecutingAssembly().GetReferencedAssemblies())
  105. {
  106. if (aN.FullName.StartsWith(an))
  107. {
  108. m_asmb = Assembly.Load(aN);
  109. break;
  110. }
  111. }
  112. }
  113. public Type GetType(string typeName)
  114. {
  115. Type type = null;
  116. string[] names = typeName.Split('.');
  117.  
  118. if (names.Length > 0)
  119. type = m_asmb.GetType(m_ns + "." + names[0]);
  120.  
  121. for (int i = 1; i < names.Length; ++i) {
  122. type = type.GetNestedType(names[i], BindingFlags.NonPublic);
  123. }
  124. return type;
  125. }
  126. public object New(string name, params object[] parameters)
  127. {
  128. Type type = GetType(name);
  129. ConstructorInfo[] ctorInfos = type.GetConstructors();
  130. foreach (ConstructorInfo ci in ctorInfos) {
  131. try {
  132. return ci.Invoke(parameters);
  133. } catch { }
  134. }
  135.  
  136. return null;
  137. }
  138. public object Call(object obj, string func, params object[] parameters)
  139. {
  140. return Call2(obj, func, parameters);
  141. }
  142. public object Call2(object obj, string func, object[] parameters)
  143. {
  144. return CallAs2(obj.GetType(), obj, func, parameters);
  145. }
  146. public object CallAs(Type type, object obj, string func, params object[] parameters)
  147. {
  148. return CallAs2(type, obj, func, parameters);
  149. }
  150. public object CallAs2(Type type, object obj, string func, object[] parameters) {
  151. MethodInfo methInfo = type.GetMethod(func, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  152. return methInfo.Invoke(obj, parameters);
  153. }
  154. public object Get(object obj, string prop)
  155. {
  156. return GetAs(obj.GetType(), obj, prop);
  157. }
  158. public object GetAs(Type type, object obj, string prop) {
  159. PropertyInfo propInfo = type.GetProperty(prop, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  160. return propInfo.GetValue(obj, null);
  161. }
  162. public object GetEnum(string typeName, string name) {
  163. Type type = GetType(typeName);
  164. FieldInfo fieldInfo = type.GetField(name);
  165. return fieldInfo.GetValue(null);
  166. }
  167. }
  168. }
  169. "@
  170. $assemblies = ('System.Windows.Forms', 'System.Reflection')
  171. Add-Type -TypeDefinition $sourceCode -ReferencedAssemblies $assemblies -ErrorAction STOP
  172. }
Advertisement
Add Comment
Please, Sign In to add comment