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
12 changes: 6 additions & 6 deletions FullscreenLock.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
Debug|x64 = Debug|x64
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7A92E5B0-CFAF-4182-B4EE-48C5F0E66CB8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7A92E5B0-CFAF-4182-B4EE-48C5F0E66CB8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7A92E5B0-CFAF-4182-B4EE-48C5F0E66CB8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7A92E5B0-CFAF-4182-B4EE-48C5F0E66CB8}.Release|Any CPU.Build.0 = Release|Any CPU
{7A92E5B0-CFAF-4182-B4EE-48C5F0E66CB8}.Debug|x64.ActiveCfg = Debug|x64
{7A92E5B0-CFAF-4182-B4EE-48C5F0E66CB8}.Debug|x64.Build.0 = Debug|x64
{7A92E5B0-CFAF-4182-B4EE-48C5F0E66CB8}.Release|x64.ActiveCfg = Release|x64
{7A92E5B0-CFAF-4182-B4EE-48C5F0E66CB8}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 3 additions & 3 deletions FullscreenLock/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup>
</configuration>
</configuration>
228 changes: 123 additions & 105 deletions FullscreenLock/Checker.cs
Original file line number Diff line number Diff line change
@@ -1,108 +1,126 @@
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; }

using System;
using System.Linq;
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;

//determine process, to check the whitelist
Process proc = null;
try
{
GetWindowThreadProcessId(hWnd, out uint procid);
proc = Process.GetProcessById((int)procid);
}
catch (Exception) { }

if (proc != null)
{
if ((appBounds.Bottom - appBounds.Top) == screenBounds.Height && (appBounds.Right - appBounds.Left) == screenBounds.Width
&& !IsWhitelisted(proc.MainModule.FileName))
{
Console.WriteLine(proc.ProcessName);
Cursor.Clip = screenBounds;
return true;
}
else
{
Cursor.Clip = Rectangle.Empty;
return false;
}
}
}
}
return false;
}

public static bool IsWhitelisted(string sFullPathToProgram)
{
return Program.Settings_File.Instance.asWhitelist.ConvertAll(s => s.ToLower()).Contains(sFullPathToProgram.ToLower());
}
}

public class BoolEventArgs : EventArgs
{
public bool Bool { get; set; }

public BoolEventArgs(bool b)
{
Bool = b;
}
}
}
}
}
}
17 changes: 16 additions & 1 deletion FullscreenLock/FullscreenLock.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions FullscreenLock/FullscreenLock.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System.Windows.Forms;
using static FullscreenLock.Settings;

namespace FullscreenLock
{
public partial class FullscreenLock : Form
{
public event EventHandler ActiveStateToggled;


public FullscreenLock()
{
InitializeComponent();
Expand Down Expand Up @@ -48,5 +50,11 @@ public void ForegroundFullscreenStateChanged(object sender, BoolEventArgs e)
{
StatusLabel.Text = e.Bool ? "Fullscreen app in focus" : "Waiting for focus";
}

private void buttonSettings_Click(object sender, EventArgs e)
{
Settings settings = new Settings(this);
settings.Show();
}
}
}
Loading