diff --git a/RetroBar/Utilities/ExplorerMonitor.cs b/RetroBar/Utilities/ExplorerMonitor.cs new file mode 100644 index 00000000..bb9ed46c --- /dev/null +++ b/RetroBar/Utilities/ExplorerMonitor.cs @@ -0,0 +1,58 @@ +using ManagedShell.Common.Logging; +using ManagedShell.Interop; +using System; +using System.Windows.Forms; + +namespace RetroBar.Utilities +{ + public class ExplorerMonitor : IDisposable + { + private ExplorerMonitorWindow _explorerMonitorWindow; + + public void ExplorerMonitorStart(WindowManager windowManagerRef) + { + if (_explorerMonitorWindow != null) { return; } // Prevent multiple monitors. + + _explorerMonitorWindow = new ExplorerMonitorWindow(windowManagerRef); // Start monitoring. + } + + public void Dispose() + { + _explorerMonitorWindow?.Dispose(); + } + + private class ExplorerMonitorWindow : NativeWindow, IDisposable + { + private readonly WindowManager _windowManagerRef; + private static readonly int WM_TASKBARCREATEDMESSAGE = NativeMethods.RegisterWindowMessage("TaskbarCreated"); + + public ExplorerMonitorWindow(WindowManager windowManagerRef) + { + _windowManagerRef = windowManagerRef; + CreateHandle(new CreateParams()); + } + + protected override void WndProc(ref Message m) + { + if (m.Msg == WM_TASKBARCREATEDMESSAGE) + { + try + { + _windowManagerRef.ReopenTaskbars(); // Reopen taskbars if explorer.exe is restarted. + } + catch (Exception ex) + { + ShellLogger.Warning($"Error handling TaskbarCreated message on ExplorerMonitor: {ex.Message}"); + } + } + + base.WndProc(ref m); // Call the base class to process other messages so we dont accidentally cause crashes or bugs. + } + + public void Dispose() + { + DestroyHandle(); + } + } + } +} diff --git a/RetroBar/Utilities/WindowManager.cs b/RetroBar/Utilities/WindowManager.cs index 11c16fb0..90215133 100644 --- a/RetroBar/Utilities/WindowManager.cs +++ b/RetroBar/Utilities/WindowManager.cs @@ -1,4 +1,4 @@ -using ManagedShell; +using ManagedShell; using ManagedShell.AppBar; using ManagedShell.Common.Logging; using System; @@ -18,12 +18,16 @@ public class WindowManager : IDisposable private readonly ShellManager _shellManager; private readonly Updater _updater; + private readonly ExplorerMonitor _explorerMonitor = new(); + public WindowManager(ShellManager shellManager, StartMenuMonitor startMenuMonitor, Updater updater) { _shellManager = shellManager; _startMenuMonitor = startMenuMonitor; _updater = updater; + _explorerMonitor.ExplorerMonitorStart(this); + _shellManager.ExplorerHelper.HideExplorerTaskbar = true; openTaskbars(); @@ -183,6 +187,7 @@ private void resetScreenCache() public void Dispose() { + _explorerMonitor?.Dispose(); _shellManager.ExplorerHelper.HideExplorerTaskbar = false; Settings.Instance.PropertyChanged -= Settings_PropertyChanged; }