From 8889e239f3b07ff2c275506d1a70bd4ae059bc98 Mon Sep 17 00:00:00 2001 From: Karu Date: Sat, 20 Nov 2021 05:56:40 -0300 Subject: [PATCH] fix for LDPlayer fullscreen width and height (it adds 1px for height and width) --- FullscreenLock/Checker.cs | 208 ++++++++++++------------ FullscreenLock/FullscreenLockContext.cs | 164 +++++++++---------- 2 files changed, 179 insertions(+), 193 deletions(-) diff --git a/FullscreenLock/Checker.cs b/FullscreenLock/Checker.cs index 10926ad..a4fea69 100644 --- a/FullscreenLock/Checker.cs +++ b/FullscreenLock/Checker.cs @@ -1,108 +1,102 @@ -using System; -using System.Runtime.InteropServices; -using System.Windows.Forms; -using System.Drawing; -using System.Diagnostics; - -namespace FullscreenLock -{ - class Checker - { - readonly Timer t = new Timer(); - bool ForegroundFullscreenState = false; - public event EventHandler ActiveStateChanged; - public event EventHandler ForegroundFullscreenStateChanged; - - // Import a bunch of win32 API calls. - [DllImport("user32.dll")] - private static extern IntPtr GetForegroundWindow(); - [DllImport("user32.dll", SetLastError = true)] - public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); - [DllImport("user32.dll", SetLastError = true)] - private static extern int GetWindowRect(IntPtr hwnd, out RECT rc); - [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] - public static extern bool ClipCursor(ref RECT rcClip); - [DllImport("user32.dll")] - private static extern IntPtr GetDesktopWindow(); - [DllImport("user32.dll")] - private static extern IntPtr GetShellWindow(); - - public Checker() - { - t.Tick += new EventHandler(CheckForFullscreenApps); - t.Interval = 100; - t.Start(); - } - - public void ActiveStateToggled(object sender, EventArgs e) - { - if (t.Enabled) - { - t.Stop(); - } - else - { - t.Start(); - } - - ActiveStateChanged?.Invoke(this, new BoolEventArgs(t.Enabled)); - } - - private void CheckForFullscreenApps(object sender, EventArgs e) - { - bool NewFullscreenState = IsForegroundFullScreen(); - - //If the fullscreen state changed, set the new state and emit the change event - if (ForegroundFullscreenState != NewFullscreenState) - { - ForegroundFullscreenState = NewFullscreenState; - ForegroundFullscreenStateChanged?.Invoke(this, new BoolEventArgs(NewFullscreenState)); - } - } - - public static bool IsForegroundFullScreen() - { - //Get the handles for the desktop and shell now. - IntPtr desktopHandle = GetDesktopWindow(); - IntPtr shellHandle = GetShellWindow(); - Rectangle screenBounds; - IntPtr hWnd; - - hWnd = GetForegroundWindow(); - if (hWnd != null && !hWnd.Equals(IntPtr.Zero)) - { - //Check we haven't picked up the desktop or the shell - if (!(hWnd.Equals(desktopHandle) || hWnd.Equals(shellHandle))) - { - GetWindowRect(hWnd, out RECT appBounds); - //determine if window is fullscreen - screenBounds = Screen.FromHandle(hWnd).Bounds; - GetWindowThreadProcessId(hWnd, out uint procid); - var proc = Process.GetProcessById((int)procid); - if ((appBounds.Bottom - appBounds.Top) == screenBounds.Height && (appBounds.Right - appBounds.Left) == screenBounds.Width) - { - Console.WriteLine(proc.ProcessName); - Cursor.Clip = screenBounds; - return true; - } - else - { - Cursor.Clip = Rectangle.Empty; - return false; - } - } - } - return false; - } - } - - public class BoolEventArgs : EventArgs - { - public bool Bool { get; set; } - - public BoolEventArgs(bool b) - { +using System; +using System.Diagnostics; +using System.Drawing; +using System.Runtime.InteropServices; +using System.Windows.Forms; + +namespace FullscreenLock { + + public class BoolEventArgs : EventArgs { + + public BoolEventArgs(bool b) { Bool = b; - } - } -} + } + + public bool Bool { get; set; } + } + + internal class Checker { + private readonly Timer t = new Timer(); + private bool ForegroundFullscreenState = false; + + public Checker() { + t.Tick += new EventHandler(CheckForFullscreenApps); + t.Interval = 100; + t.Start(); + } + + public event EventHandler ActiveStateChanged; + + public event EventHandler ForegroundFullscreenStateChanged; + + [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] + public static extern bool ClipCursor(ref RECT rcClip); + + [DllImport("user32.dll", SetLastError = true)] + public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); + + public static bool IsForegroundFullScreen() { + //Get the handles for the desktop and shell now. + IntPtr desktopHandle = GetDesktopWindow(); + IntPtr shellHandle = GetShellWindow(); + Rectangle screenBounds; + IntPtr hWnd; + + hWnd = GetForegroundWindow(); + if (hWnd != null && !hWnd.Equals(IntPtr.Zero)) { + //Check we haven't picked up the desktop or the shell + if (!(hWnd.Equals(desktopHandle) || hWnd.Equals(shellHandle))) { + GetWindowRect(hWnd, out RECT appBounds); + //determine if window is fullscreen + screenBounds = Screen.FromHandle(hWnd).Bounds; + GetWindowThreadProcessId(hWnd, out uint procid); + var proc = Process.GetProcessById((int)procid); + var screenHeight = (appBounds.Bottom - appBounds.Top); + var screenWidth = (appBounds.Right - appBounds.Left); + if (screenHeight >= screenBounds.Height && screenWidth >= screenBounds.Width) { + Console.WriteLine(proc.ProcessName); + Cursor.Clip = screenBounds; + return true; + } else { + Cursor.Clip = Rectangle.Empty; + return false; + } + } + } + return false; + } + + public void ActiveStateToggled(object sender, EventArgs e) { + if (t.Enabled) { + t.Stop(); + } else { + t.Start(); + } + + ActiveStateChanged?.Invoke(this, new BoolEventArgs(t.Enabled)); + } + + [DllImport("user32.dll")] + private static extern IntPtr GetDesktopWindow(); + + // Import a bunch of win32 API calls. + [DllImport("user32.dll")] + private static extern IntPtr GetForegroundWindow(); + + [DllImport("user32.dll")] + private static extern IntPtr GetShellWindow(); + + [DllImport("user32.dll", SetLastError = true)] + private static extern int GetWindowRect(IntPtr hwnd, out RECT rc); + + private void CheckForFullscreenApps(object sender, EventArgs e) { + bool NewFullscreenState = IsForegroundFullScreen(); + + //If the fullscreen state changed, set the new state and emit the change event + if (ForegroundFullscreenState != NewFullscreenState) { + ForegroundFullscreenState = NewFullscreenState; + ForegroundFullscreenStateChanged?.Invoke(this, new BoolEventArgs(NewFullscreenState)); + } + } + } +} \ No newline at end of file diff --git a/FullscreenLock/FullscreenLockContext.cs b/FullscreenLock/FullscreenLockContext.cs index d023d68..1a5a9b0 100644 --- a/FullscreenLock/FullscreenLockContext.cs +++ b/FullscreenLock/FullscreenLockContext.cs @@ -1,87 +1,79 @@ -using System; -using System.Drawing; -using System.Windows.Forms; - -namespace FullscreenLock -{ - class FullscreenLockContext : ApplicationContext - { - private NotifyIcon TrayIcon; - private ContextMenuStrip TrayIconContextMenu; - private ToolStripMenuItem QuitMenuItem; - private ToolStripMenuItem ShowMenuItem; - - private readonly FullscreenLock fsl; - private readonly Checker c; - - public FullscreenLockContext() - { - Application.ApplicationExit += new EventHandler(OnApplicationExit); - - InitializeComponent(); - - c = new Checker(); - fsl = new FullscreenLock(); - c.ActiveStateChanged += new EventHandler(fsl.ActiveStateChanged); - c.ForegroundFullscreenStateChanged += new EventHandler(fsl.ForegroundFullscreenStateChanged); - fsl.ActiveStateToggled += new EventHandler(c.ActiveStateToggled); - - TrayIcon.Visible = true; - } - - private void InitializeComponent() - { - TrayIcon = new NotifyIcon(); - TrayIconContextMenu = new ContextMenuStrip(); - ShowMenuItem = new ToolStripMenuItem(); - QuitMenuItem = new ToolStripMenuItem(); - TrayIconContextMenu.SuspendLayout(); - - TrayIcon.ContextMenuStrip = TrayIconContextMenu; - TrayIcon.Icon = Properties.Resources.Gaben; - TrayIcon.Text = "FullscreenLock"; - TrayIcon.Visible = true; - TrayIcon.MouseClick += new MouseEventHandler(TrayIcon_MouseClick); - - TrayIconContextMenu.Items.AddRange(new ToolStripItem[] { - ShowMenuItem, - QuitMenuItem - }); - TrayIconContextMenu.Name = "TrayIconContextMenu"; - TrayIconContextMenu.Size = new Size(104, 48); - - ShowMenuItem.Name = "ShowMenuItem"; - ShowMenuItem.Size = new Size(103, 22); - ShowMenuItem.Text = "Show"; - ShowMenuItem.Click += ShowMenuItem_Click; - - QuitMenuItem.Name = "QuitMenuItem"; - QuitMenuItem.Size = new Size(103, 22); - QuitMenuItem.Text = "Quit"; - QuitMenuItem.Click += QuitMenuItem_Click; - - TrayIconContextMenu.ResumeLayout(false); - } - - private void OnApplicationExit(object sender, EventArgs e) - { - TrayIcon.Visible = false; - } - - private void TrayIcon_MouseClick(object sender, MouseEventArgs e) - { - if (e.Button == MouseButtons.Left) - fsl.SetVisibility(true); - } - - private void ShowMenuItem_Click(object sender, EventArgs e) - { - fsl.SetVisibility(true); - } - - private void QuitMenuItem_Click(object sender, EventArgs e) - { - Application.Exit(); - } - } +using System; +using System.Drawing; +using System.Windows.Forms; + +namespace FullscreenLock { + + internal class FullscreenLockContext : ApplicationContext { + private readonly Checker c; + private readonly FullscreenLock fsl; + private ToolStripMenuItem QuitMenuItem; + private ToolStripMenuItem ShowMenuItem; + private NotifyIcon TrayIcon; + private ContextMenuStrip TrayIconContextMenu; + + public FullscreenLockContext() { + Application.ApplicationExit += new EventHandler(OnApplicationExit); + + InitializeComponent(); + + c = new Checker(); + fsl = new FullscreenLock(); + c.ActiveStateChanged += new EventHandler(fsl.ActiveStateChanged); + c.ForegroundFullscreenStateChanged += new EventHandler(fsl.ForegroundFullscreenStateChanged); + fsl.ActiveStateToggled += new EventHandler(c.ActiveStateToggled); + + TrayIcon.Visible = true; + } + + private void InitializeComponent() { + TrayIcon = new NotifyIcon(); + TrayIconContextMenu = new ContextMenuStrip(); + ShowMenuItem = new ToolStripMenuItem(); + QuitMenuItem = new ToolStripMenuItem(); + TrayIconContextMenu.SuspendLayout(); + + TrayIcon.ContextMenuStrip = TrayIconContextMenu; + TrayIcon.Icon = Properties.Resources.Gaben; + TrayIcon.Text = "FullscreenLock"; + TrayIcon.Visible = true; + TrayIcon.MouseClick += new MouseEventHandler(TrayIcon_MouseClick); + + TrayIconContextMenu.Items.AddRange(new ToolStripItem[] { + ShowMenuItem, + QuitMenuItem + }); + TrayIconContextMenu.Name = "TrayIconContextMenu"; + TrayIconContextMenu.Size = new Size(104, 48); + + ShowMenuItem.Name = "ShowMenuItem"; + ShowMenuItem.Size = new Size(103, 22); + ShowMenuItem.Text = "Show"; + ShowMenuItem.Click += ShowMenuItem_Click; + + QuitMenuItem.Name = "QuitMenuItem"; + QuitMenuItem.Size = new Size(103, 22); + QuitMenuItem.Text = "Quit"; + QuitMenuItem.Click += QuitMenuItem_Click; + + TrayIconContextMenu.ResumeLayout(false); + } + + private void OnApplicationExit(object sender, EventArgs e) { + TrayIcon.Visible = false; + } + + private void QuitMenuItem_Click(object sender, EventArgs e) { + Application.Exit(); + } + + private void ShowMenuItem_Click(object sender, EventArgs e) { + fsl.SetVisibility(true); + } + + private void TrayIcon_MouseClick(object sender, MouseEventArgs e) { + if (e.Button == MouseButtons.Left) + fsl.SetVisibility(true); + } + } } \ No newline at end of file