diff --git a/QEMUWF/FolderPicker.cs b/QEMUWF/FolderPicker.cs new file mode 100644 index 0000000..f0c8e97 --- /dev/null +++ b/QEMUWF/FolderPicker.cs @@ -0,0 +1,219 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.ComTypes; + +namespace QEMUWF +{ + public class FolderPicker + { + private readonly List _resultPaths = new List(); + private readonly List _resultNames = new List(); + + public List ResultPaths => _resultPaths; + public List ResultNames => _resultNames; + public string ResultPath => ResultPaths.FirstOrDefault(); + public string ResultName => ResultNames.FirstOrDefault(); + public virtual string InputPath { get; set; } + public virtual bool ForceFileSystem { get; set; } + public virtual bool Multiselect { get; set; } + public virtual string Title { get; set; } + public virtual string OkButtonLabel { get; set; } + public virtual string FileNameLabel { get; set; } + + protected virtual int SetOptions(int options) + { + if (ForceFileSystem) + { + options |= (int)FOS.FOS_FORCEFILESYSTEM; + } + + if (Multiselect) + { + options |= (int)FOS.FOS_ALLOWMULTISELECT; + } + return options; + } + // for all .NET + public virtual bool? ShowDialog(IntPtr owner, bool throwOnError = false) + { + var dialog = (IFileOpenDialog)new FileOpenDialog(); + if (!string.IsNullOrEmpty(InputPath)) + { + if (CheckHr(SHCreateItemFromParsingName(InputPath, null, typeof(IShellItem).GUID, out var item), throwOnError) != 0) + return null; + + dialog.SetFolder(item); + } + + var options = FOS.FOS_PICKFOLDERS; + options = (FOS)SetOptions((int)options); + dialog.SetOptions(options); + + if (Title != null) + { + dialog.SetTitle(Title); + } + + if (OkButtonLabel != null) + { + dialog.SetOkButtonLabel(OkButtonLabel); + } + + if (FileNameLabel != null) + { + dialog.SetFileName(FileNameLabel); + } + + if (owner == IntPtr.Zero) + { + owner = Process.GetCurrentProcess().MainWindowHandle; + if (owner == IntPtr.Zero) + { + owner = GetDesktopWindow(); + } + } + + var hr = dialog.Show(owner); + if (hr == ERROR_CANCELLED) + return null; + + if (CheckHr(hr, throwOnError) != 0) + return null; + + if (CheckHr(dialog.GetResults(out var items), throwOnError) != 0) + return null; + + items.GetCount(out var count); + for (var i = 0; i < count; i++) + { + items.GetItemAt(i, out var item); + CheckHr(item.GetDisplayName(SIGDN.SIGDN_DESKTOPABSOLUTEPARSING, out var path), throwOnError); + CheckHr(item.GetDisplayName(SIGDN.SIGDN_DESKTOPABSOLUTEEDITING, out var name), throwOnError); + if (path != null || name != null) + { + _resultPaths.Add(path); + _resultNames.Add(name); + } + } + return true; + } + + private static int CheckHr(int hr, bool throwOnError) + { + if (hr != 0 && throwOnError) Marshal.ThrowExceptionForHR(hr); + return hr; + } + + [DllImport("shell32")] + private static extern int SHCreateItemFromParsingName([MarshalAs(UnmanagedType.LPWStr)] string pszPath, IBindCtx pbc, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, out IShellItem ppv); + + [DllImport("user32")] + private static extern IntPtr GetDesktopWindow(); + +#pragma warning disable IDE1006 // Naming Styles + private const int ERROR_CANCELLED = unchecked((int)0x800704C7); +#pragma warning restore IDE1006 // Naming Styles + + [ComImport, Guid("DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7")] // CLSID_FileOpenDialog + private class FileOpenDialog { } + + [ComImport, Guid("d57c7288-d4ad-4768-be02-9d969532d960"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + private interface IFileOpenDialog + { + [PreserveSig] int Show(IntPtr parent); // IModalWindow + [PreserveSig] int SetFileTypes(); // not fully defined + [PreserveSig] int SetFileTypeIndex(int iFileType); + [PreserveSig] int GetFileTypeIndex(out int piFileType); + [PreserveSig] int Advise(); // not fully defined + [PreserveSig] int Unadvise(); + [PreserveSig] int SetOptions(FOS fos); + [PreserveSig] int GetOptions(out FOS pfos); + [PreserveSig] int SetDefaultFolder(IShellItem psi); + [PreserveSig] int SetFolder(IShellItem psi); + [PreserveSig] int GetFolder(out IShellItem ppsi); + [PreserveSig] int GetCurrentSelection(out IShellItem ppsi); + [PreserveSig] int SetFileName([MarshalAs(UnmanagedType.LPWStr)] string pszName); + [PreserveSig] int GetFileName([MarshalAs(UnmanagedType.LPWStr)] out string pszName); + [PreserveSig] int SetTitle([MarshalAs(UnmanagedType.LPWStr)] string pszTitle); + [PreserveSig] int SetOkButtonLabel([MarshalAs(UnmanagedType.LPWStr)] string pszText); + [PreserveSig] int SetFileNameLabel([MarshalAs(UnmanagedType.LPWStr)] string pszLabel); + [PreserveSig] int GetResult(out IShellItem ppsi); + [PreserveSig] int AddPlace(IShellItem psi, int alignment); + [PreserveSig] int SetDefaultExtension([MarshalAs(UnmanagedType.LPWStr)] string pszDefaultExtension); + [PreserveSig] int Close(int hr); + [PreserveSig] int SetClientGuid(); // not fully defined + [PreserveSig] int ClearClientData(); + [PreserveSig] int SetFilter([MarshalAs(UnmanagedType.IUnknown)] object pFilter); + [PreserveSig] int GetResults(out IShellItemArray ppenum); + [PreserveSig] int GetSelectedItems([MarshalAs(UnmanagedType.IUnknown)] out object ppsai); + } + + [ComImport, Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + private interface IShellItem + { + [PreserveSig] int BindToHandler(); // not fully defined + [PreserveSig] int GetParent(); // not fully defined + [PreserveSig] int GetDisplayName(SIGDN sigdnName, [MarshalAs(UnmanagedType.LPWStr)] out string ppszName); + [PreserveSig] int GetAttributes(); // not fully defined + [PreserveSig] int Compare(); // not fully defined + } + + [ComImport, Guid("b63ea76d-1f85-456f-a19c-48159efa858b"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + private interface IShellItemArray + { + [PreserveSig] int BindToHandler(); // not fully defined + [PreserveSig] int GetPropertyStore(); // not fully defined + [PreserveSig] int GetPropertyDescriptionList(); // not fully defined + [PreserveSig] int GetAttributes(); // not fully defined + [PreserveSig] int GetCount(out int pdwNumItems); + [PreserveSig] int GetItemAt(int dwIndex, out IShellItem ppsi); + [PreserveSig] int EnumItems(); // not fully defined + } + +#pragma warning disable CA1712 // Do not prefix enum values with type name + private enum SIGDN : uint + { + SIGDN_DESKTOPABSOLUTEEDITING = 0x8004c000, + SIGDN_DESKTOPABSOLUTEPARSING = 0x80028000, + SIGDN_FILESYSPATH = 0x80058000, + SIGDN_NORMALDISPLAY = 0, + SIGDN_PARENTRELATIVE = 0x80080001, + SIGDN_PARENTRELATIVEEDITING = 0x80031001, + SIGDN_PARENTRELATIVEFORADDRESSBAR = 0x8007c001, + SIGDN_PARENTRELATIVEPARSING = 0x80018001, + SIGDN_URL = 0x80068000 + } + + [Flags] + private enum FOS + { + FOS_OVERWRITEPROMPT = 0x2, + FOS_STRICTFILETYPES = 0x4, + FOS_NOCHANGEDIR = 0x8, + FOS_PICKFOLDERS = 0x20, + FOS_FORCEFILESYSTEM = 0x40, + FOS_ALLNONSTORAGEITEMS = 0x80, + FOS_NOVALIDATE = 0x100, + FOS_ALLOWMULTISELECT = 0x200, + FOS_PATHMUSTEXIST = 0x800, + FOS_FILEMUSTEXIST = 0x1000, + FOS_CREATEPROMPT = 0x2000, + FOS_SHAREAWARE = 0x4000, + FOS_NOREADONLYRETURN = 0x8000, + FOS_NOTESTFILECREATE = 0x10000, + FOS_HIDEMRUPLACES = 0x20000, + FOS_HIDEPINNEDPLACES = 0x40000, + FOS_NODEREFERENCELINKS = 0x100000, + FOS_OKBUTTONNEEDSINTERACTION = 0x200000, + FOS_DONTADDTORECENT = 0x2000000, + FOS_FORCESHOWHIDDEN = 0x10000000, + FOS_DEFAULTNOMINIMODE = 0x20000000, + FOS_FORCEPREVIEWPANEON = 0x40000000, + FOS_SUPPORTSTREAMABLEITEMS = unchecked((int)0x80000000) + } +#pragma warning restore CA1712 // Do not prefix enum values with type name + } +} diff --git a/QEMUWF/Form3.Designer.cs b/QEMUWF/Form3.Designer.cs index 472a564..67b0c0f 100644 --- a/QEMUWF/Form3.Designer.cs +++ b/QEMUWF/Form3.Designer.cs @@ -35,7 +35,7 @@ private void InitializeComponent() this.next = new System.Windows.Forms.Button(); this.bacc = new System.Windows.Forms.Button(); this.help = new System.Windows.Forms.Button(); - this.tabControl1 = new TabControlWithoutHeader(); + this.tabControl1 = new QEMUWF.TabControlWithoutHeader(); this.tabPage1 = new System.Windows.Forms.TabPage(); this.label4 = new System.Windows.Forms.Label(); this.comboBox1 = new System.Windows.Forms.ComboBox(); @@ -157,6 +157,7 @@ private void InitializeComponent() this.tabControl1.Controls.Add(this.tabPage4); this.tabControl1.Controls.Add(this.tabPage5); this.tabControl1.Location = new System.Drawing.Point(0, 3); + this.tabControl1.Multiline = true; this.tabControl1.Name = "tabControl1"; this.tabControl1.SelectedIndex = 0; this.tabControl1.Size = new System.Drawing.Size(438, 449); @@ -419,7 +420,7 @@ private void InitializeComponent() 0, 0}); this.numericUpDown1.Name = "numericUpDown1"; - this.numericUpDown1.Size = new System.Drawing.Size(30, 22); + this.numericUpDown1.Size = new System.Drawing.Size(50, 22); this.numericUpDown1.TabIndex = 5; this.numericUpDown1.Value = new decimal(new int[] { 1, @@ -497,7 +498,7 @@ private void InitializeComponent() // // textBox3 // - this.textBox3.Location = new System.Drawing.Point(186, 124); + this.textBox3.Location = new System.Drawing.Point(186, 117); this.textBox3.Name = "textBox3"; this.textBox3.Size = new System.Drawing.Size(96, 22); this.textBox3.TabIndex = 11; @@ -506,7 +507,7 @@ private void InitializeComponent() // label14 // this.label14.AutoSize = true; - this.label14.Location = new System.Drawing.Point(51, 128); + this.label14.Location = new System.Drawing.Point(51, 120); this.label14.Name = "label14"; this.label14.Size = new System.Drawing.Size(46, 13); this.label14.TabIndex = 10; @@ -515,7 +516,7 @@ private void InitializeComponent() // label13 // this.label13.AutoSize = true; - this.label13.Location = new System.Drawing.Point(51, 219); + this.label13.Location = new System.Drawing.Point(51, 208); this.label13.Name = "label13"; this.label13.Size = new System.Drawing.Size(153, 13); this.label13.TabIndex = 9; @@ -524,7 +525,7 @@ private void InitializeComponent() // button1 // this.button1.Enabled = false; - this.button1.Location = new System.Drawing.Point(271, 237); + this.button1.Location = new System.Drawing.Point(271, 226); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(50, 22); this.button1.TabIndex = 8; @@ -535,7 +536,7 @@ private void InitializeComponent() // textBox2 // this.textBox2.Enabled = false; - this.textBox2.Location = new System.Drawing.Point(54, 237); + this.textBox2.Location = new System.Drawing.Point(54, 226); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(212, 22); this.textBox2.TabIndex = 7; @@ -543,7 +544,7 @@ private void InitializeComponent() // checkBox1 // this.checkBox1.AutoSize = true; - this.checkBox1.Location = new System.Drawing.Point(54, 155); + this.checkBox1.Location = new System.Drawing.Point(54, 145); this.checkBox1.Name = "checkBox1"; this.checkBox1.Size = new System.Drawing.Size(184, 17); this.checkBox1.TabIndex = 6; @@ -553,7 +554,7 @@ private void InitializeComponent() // label12 // this.label12.AutoSize = true; - this.label12.Location = new System.Drawing.Point(51, 100); + this.label12.Location = new System.Drawing.Point(51, 93); this.label12.Name = "label12"; this.label12.Size = new System.Drawing.Size(132, 13); this.label12.TabIndex = 5; @@ -561,7 +562,7 @@ private void InitializeComponent() // // numericUpDown2 // - this.numericUpDown2.Location = new System.Drawing.Point(186, 96); + this.numericUpDown2.Location = new System.Drawing.Point(186, 91); this.numericUpDown2.Minimum = new decimal(new int[] { 2, 0, diff --git a/QEMUWF/Program.cs b/QEMUWF/Program.cs index a84cd71..f3152c7 100644 --- a/QEMUWF/Program.cs +++ b/QEMUWF/Program.cs @@ -18,11 +18,13 @@ static void Main() string s = Properties.Settings.Default.qemuPath; if (string.IsNullOrEmpty(s) || !Directory.Exists(s) || (Directory.Exists(s) && !File.Exists(Path.Combine(s, "qemu-system-i386.exe")))) { - FolderBrowserDialog browserDialog = new FolderBrowserDialog(); - browserDialog.Description = "Select the folder where Qemu is installed."; - browserDialog.ShowDialog(); - Properties.Settings.Default.qemuPath = browserDialog.SelectedPath; - Properties.Settings.Default.Save(); + FolderPicker dialog = new FolderPicker(); + dialog.Title = "Select the folder where Qemu is installed."; + if (dialog.ShowDialog(IntPtr.Zero) == true) + { + Properties.Settings.Default.qemuPath = dialog.ResultPath; + Properties.Settings.Default.Save(); + } } Application.Run(new Form1()); } diff --git a/QEMUWF/QEMUWF.csproj b/QEMUWF/QEMUWF.csproj index 04afe28..4d456c0 100644 --- a/QEMUWF/QEMUWF.csproj +++ b/QEMUWF/QEMUWF.csproj @@ -50,6 +50,7 @@ + Form