Skip to content

Commit

Permalink
Replace Form with NativeWindow.
Browse files Browse the repository at this point in the history
  • Loading branch information
MKKNinetyTwo authored Nov 14, 2024
1 parent 6d6547a commit 6d98ce5
Showing 1 changed file with 22 additions and 37 deletions.
59 changes: 22 additions & 37 deletions RetroBar/Utilities/ExplorerMonitor.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using ManagedShell.Interop;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace RetroBar.Utilities
{
public class ExplorerMonitor : IDisposable
{
private volatile bool _ExplorerMonitorIsMonitoring;
private _ExplorerMonitor _explorerMonitor;
private bool _ExplorerMonitorIsMonitoring;
private MonitorWindow _explorerMonitor;

public void ExplorerMonitorStart()
{
Expand All @@ -19,67 +19,52 @@ public void ExplorerMonitorStart()
else
{
_ExplorerMonitorIsMonitoring = true; // We will set flag to true to prevent multiple monitors.

// Start monitoring
_explorerMonitor = new _ExplorerMonitor();
_explorerMonitor.Show();
_explorerMonitor = new MonitorWindow(); // Start monitoring.
}
}

// ExplorerMonitor is a hidden form that captures taskbar events
private class _ExplorerMonitor : Form
public void Dispose()
{
private const int GWL_EXSTYLE = -20;
private const int WS_EX_TOOLWINDOW = 0x00000080;
private const int WS_EX_APPWINDOW = 0x00040000;
if (_explorerMonitor != null){_explorerMonitor?.Dispose();}
}

[DllImport("user32.dll", SetLastError = true)] private static extern uint RegisterWindowMessage(string lpString);
[DllImport("user32.dll")] private static extern IntPtr SetWindowLong(IntPtr hwnd, int index, int newStyle);
[DllImport("user32.dll")] private static extern int GetWindowLong(IntPtr hwnd, int index);
// NativeWindow implementation for monitoring
private class MonitorWindow : NativeWindow, IDisposable
{
//private readonly WindowManager _windowManager;
private static readonly int TaskbarCreatedMessage = NativeMethods.RegisterWindowMessage("TaskbarCreated");

public _ExplorerMonitor()
public MonitorWindow()
{
// These will make the ExplorerMonitor form completely invisible, so we can use it just as a monitor and not form
ClientSize = new System.Drawing.Size(1, 1); // Set the size to 1x1 pixel (tiny and invisible)
FormBorderStyle = FormBorderStyle.None; // Make the form borderless
BackColor = System.Drawing.Color.Lime; // Use a color thats fully transparent in the form
TransparencyKey = System.Drawing.Color.Lime; // Set transparency key to make the color transparent
ShowInTaskbar = false; // Ensure the form doesnt appear in the taskbar
ControlBox = false; // Ensure no controls (like buttons) are on the form
Visible = false; // Set the form as invisible
StartPosition = FormStartPosition.Manual; // Dont center this form
Location = new System.Drawing.Point(-1000, -1000); // Move it far off-screen

// Remove ExplorerMonitor from the Alt+Tab list by modifying its extended style
int extendedStyle = GetWindowLong(this.Handle, GWL_EXSTYLE);
SetWindowLong(this.Handle, GWL_EXSTYLE, extendedStyle | WS_EX_TOOLWINDOW);
CreateHandle(new CreateParams());
}

// We will override WndProc to listen for TaskbarCreated event
protected override void WndProc(ref Message m)
{
if (m.Msg == (int)RegisterWindowMessage("TaskbarCreated"))
if (m.Msg == TaskbarCreatedMessage)
{
try
{
//_windowManager.ReopenTaskbars(); // Reopen taskbars if explorer.exe is restarted.
string appPath = Process.GetCurrentProcess().MainModule?.FileName;
Process.Start(appPath); // Start a new instance of RetroBar
Environment.Exit(0); // Exit the current instance of RetroBar
}
catch (Exception ex)
{
Console.WriteLine($"Error restarting RetroBar: {ex.Message}");
//Debug.WriteLine($"Error handling TaskbarCreated message on ExplorerMonitor: {ex.Message}");
Debug.WriteLine($"Error restarting RetroBar: {ex.Message}");
}
}

// Call the base class to process other messages so we dont accidentally cause crashes or bugs.
base.WndProc(ref m);
}
}

public void Dispose()
{
if (_explorerMonitor != null){_explorerMonitor?.Dispose();}
public void Dispose()
{
DestroyHandle();
}
}
}
}

0 comments on commit 6d98ce5

Please sign in to comment.