Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 101 additions & 107 deletions FullscreenLock/Checker.cs
Original file line number Diff line number Diff line change
@@ -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<BoolEventArgs> ActiveStateChanged;
public event EventHandler<BoolEventArgs> 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<BoolEventArgs> ActiveStateChanged;

public event EventHandler<BoolEventArgs> 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));
}
}
}
}
164 changes: 78 additions & 86 deletions FullscreenLock/FullscreenLockContext.cs
Original file line number Diff line number Diff line change
@@ -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<BoolEventArgs>(fsl.ActiveStateChanged);
c.ForegroundFullscreenStateChanged += new EventHandler<BoolEventArgs>(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<BoolEventArgs>(fsl.ActiveStateChanged);
c.ForegroundFullscreenStateChanged += new EventHandler<BoolEventArgs>(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);
}
}
}