diff --git a/src/Misc/Commands.cs b/src/Misc/Commands.cs index a8c1b7f..fd5bc13 100644 --- a/src/Misc/Commands.cs +++ b/src/Misc/Commands.cs @@ -1,15 +1,15 @@  using System; +using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; +using System.Text; +using System.Windows; using System.Windows.Forms; using System.Windows.Interop; -using System.Windows; using static mpvnet.Global; -using System.Collections.Generic; -using System.Text; namespace mpvnet { @@ -88,7 +88,7 @@ namespace mpvnet public static void Open_DVD_Or_BD_Folder() { App.InvokeOnMainThread(new Action(() => { - using (var dialog = new BetterFolderBrowser()) + using (var dialog = new FolderBrowser()) if (dialog.ShowDialog() == DialogResult.OK) Core.LoadDiskFolder(dialog.SelectedPath); })); diff --git a/src/Misc/BetterFolderBrowser.cs b/src/Misc/FolderBrowser.cs similarity index 66% rename from src/Misc/BetterFolderBrowser.cs rename to src/Misc/FolderBrowser.cs index 490ff22..6dd85c1 100644 --- a/src/Misc/BetterFolderBrowser.cs +++ b/src/Misc/FolderBrowser.cs @@ -1,94 +1,48 @@  -// https://github.com/Willy-Kimura/BetterFolderBrowser - using System; using System.Windows.Forms; -using System.ComponentModel; using System.Reflection; +using System.IO; +using System.Runtime.InteropServices; +using System.Diagnostics; namespace mpvnet { - public partial class BetterFolderBrowser : CommonDialog + public partial class FolderBrowser : CommonDialog { - IContainer components = null; BetterFolderBrowserDialog _dialog = new BetterFolderBrowserDialog(); - public BetterFolderBrowser() - { - InitializeComponent(); - SetDefaults(); - } - - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - components.Dispose(); - - base.Dispose(disposing); - } - - void InitializeComponent() => components = new Container(); - - public string Title - { - get { return _dialog.Title; } - set { _dialog.Title = value; } - } - - public string RootFolder - { - get { return _dialog.InitialDirectory; } - set { _dialog.InitialDirectory = value; } - } - - public bool Multiselect - { - get { return _dialog.AllowMultiselect; } - set { _dialog.AllowMultiselect = value; } - } - - public string SelectedPath => _dialog.FileName; - - public string[] SelectedPaths => _dialog.FileNames; - - public string SelectedFolder => _dialog.FileName; - - public string[] SelectedFolders => _dialog.FileNames; - - void SetDefaults() - { - _dialog.AllowMultiselect = false; - _dialog.Title = "Please select a folder..."; - _dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); + public string SelectedPath { + get => _dialog.FileName; + set => _dialog.FileName = value; } public new DialogResult ShowDialog() { - DialogResult result; - - if (_dialog.ShowDialog(IntPtr.Zero)) - result = DialogResult.OK; - else - result = DialogResult.Cancel; - - return result; + return _dialog.ShowDialog(GetOwnerHandle()) ? DialogResult.OK : DialogResult.Cancel; } - public new DialogResult ShowDialog(IWin32Window owner) + public static IntPtr GetOwnerHandle() { - DialogResult result; + IntPtr foregroundWindow = GetForegroundWindow(); + GetWindowThreadProcessId(foregroundWindow, out var procID); - if (_dialog.ShowDialog(owner.Handle)) - result = DialogResult.OK; - else - result = DialogResult.Cancel; + using (var proc = Process.GetCurrentProcess()) + if (proc.Id == procID) + return foregroundWindow; - return result; + return IntPtr.Zero; } + [DllImport("user32.dll")] + public static extern IntPtr GetForegroundWindow(); + + [DllImport("user32.dll")] + public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); + protected override bool RunDialog(IntPtr hwndOwner) => _dialog.ShowDialog(hwndOwner); - public override void Reset() => SetDefaults(); + public override void Reset() { } class BetterFolderBrowserDialog { @@ -105,27 +59,14 @@ namespace mpvnet ofd.Multiselect = false; } - public bool AllowMultiselect { - get { return ofd.Multiselect; } - set { ofd.Multiselect = value; } - } - - public string[] FileNames => ofd.FileNames; - - public string InitialDirectory { - get { return ofd.InitialDirectory; } + public string FileName { + get => ofd.FileName; set { - ofd.InitialDirectory = (value == null || value.Length == 0) ? Environment.CurrentDirectory : value; + if (Directory.Exists(value)) + ofd.InitialDirectory = value; } } - public string Title { - get { return ofd.Title; } - set { ofd.Title = (value == null) ? "Select a folder" : value; } - } - - public string FileName => ofd.FileName; - public bool ShowDialog() => ShowDialog(IntPtr.Zero); public bool ShowDialog(IntPtr hWndOwner) @@ -201,7 +142,7 @@ namespace mpvnet string[] names = typeName.Split('.'); if (names.Length > 0) - type = m_asmb.GetType((m_ns + Convert.ToString(".")) + names[0]); + type = m_asmb.GetType(m_ns + Convert.ToString(".") + names[0]); for (int i = 1; i < names.Length; i++) type = type.GetNestedType(names[i], BindingFlags.NonPublic); diff --git a/src/Misc/MainForm.cs b/src/Misc/MainForm.cs index f0ad314..d3efc04 100644 --- a/src/Misc/MainForm.cs +++ b/src/Misc/MainForm.cs @@ -9,12 +9,13 @@ using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; using System.Windows.Forms.Integration; +using System.Windows.Threading; +using MsgBoxEx; using WpfControls = System.Windows.Controls; using static mpvnet.Native; using static mpvnet.Global; -using MsgBoxEx; namespace mpvnet { @@ -639,6 +640,8 @@ namespace mpvnet try { App.RunTask(() => { MenuAutoResetEvent.WaitOne(); + System.Windows.Application.Current.Dispatcher.Invoke( + DispatcherPriority.Background, new Action(delegate { })); Core.Command(item.Command); }); } diff --git a/src/WPF/MsgBox/MessageBoxExStatic.cs b/src/WPF/MsgBox/MessageBoxExStatic.cs index dacf22f..14db2cd 100644 --- a/src/WPF/MsgBox/MessageBoxExStatic.cs +++ b/src/WPF/MsgBox/MessageBoxExStatic.cs @@ -7,6 +7,7 @@ using System.ComponentModel; using System.Diagnostics; using System.Drawing.Text; using System.Linq; +using System.Runtime.InteropServices; using System.Windows; using System.Windows.Controls; using System.Windows.Interop; @@ -101,8 +102,8 @@ namespace MsgBoxEx public static IntPtr GetOwnerHandle() { - IntPtr foregroundWindow = Native.GetForegroundWindow(); - Native.GetWindowThreadProcessId(foregroundWindow, out var procID); + IntPtr foregroundWindow = GetForegroundWindow(); + GetWindowThreadProcessId(foregroundWindow, out var procID); using (var proc = Process.GetCurrentProcess()) if (proc.Id == procID) @@ -111,6 +112,12 @@ namespace MsgBoxEx return IntPtr.Zero; } + [DllImport("user32.dll")] + public static extern IntPtr GetForegroundWindow(); + + [DllImport("user32.dll")] + public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); + public static Color ColorFromString(string colorString) { Color wpfColor = Colors.Black; diff --git a/src/WPF/MsgBox/Native.cs b/src/WPF/MsgBox/Native.cs deleted file mode 100644 index 2d45a23..0000000 --- a/src/WPF/MsgBox/Native.cs +++ /dev/null @@ -1,15 +0,0 @@ - -using System; -using System.Runtime.InteropServices; - -namespace MsgBoxEx -{ - class Native - { - [DllImport("user32.dll")] - public static extern IntPtr GetForegroundWindow(); - - [DllImport("user32.dll")] - public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); - } -} diff --git a/src/WPF/StringSettingControl.xaml.cs b/src/WPF/StringSettingControl.xaml.cs index 82bdbae..8dda154 100644 --- a/src/WPF/StringSettingControl.xaml.cs +++ b/src/WPF/StringSettingControl.xaml.cs @@ -62,13 +62,12 @@ namespace DynamicGUI switch (StringSetting.Type) { case "folder": - using (var d = new WinForms.FolderBrowserDialog()) + using (FolderBrowser fb = new FolderBrowser()) { - d.Description = "Choose a folder."; - d.SelectedPath = ValueTextBox.Text; + fb.SelectedPath = ValueTextBox.Text; - if (d.ShowDialog() == WinForms.DialogResult.OK) - ValueTextBox.Text = d.SelectedPath; + if (fb.ShowDialog() == WinForms.DialogResult.OK) + ValueTextBox.Text = fb.SelectedPath; } break; case "color": diff --git a/src/mpv.net.csproj b/src/mpv.net.csproj index 7e6074f..47809eb 100644 --- a/src/mpv.net.csproj +++ b/src/mpv.net.csproj @@ -78,7 +78,7 @@ - + Component @@ -112,7 +112,6 @@ -