Skip to content
This repository has been archived by the owner on Jan 4, 2024. It is now read-only.

Commit

Permalink
Global hotkeys to toggle visibility + completely hide the GUI (#5)
Browse files Browse the repository at this point in the history
Register global hotkeys to toggle visibility and stealth mode (hiding the app window), by @timmolderez
  • Loading branch information
timmolderez committed Jul 3, 2022
1 parent 4f7325a commit 1716fba
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
5 changes: 2 additions & 3 deletions Win11ClockTogglerGUI/Win11ClockTogglerGUI.Designer.cs

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

70 changes: 69 additions & 1 deletion Win11ClockTogglerGUI/Win11ClockTogglerGUI.cs
Expand Up @@ -3,11 +3,14 @@
using System.Windows.Forms;
using System.Collections.Generic;
using Win11ClockToggler;
using System.Runtime.InteropServices;

namespace Win11ClockTogglerGUI
{
public partial class Win11ClockTogglerGUI : Form
{
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);

private bool IsDirty = false;
private List<IntPtr> CurrentMonitoredControls = new List<IntPtr>();
Expand All @@ -16,9 +19,17 @@ public partial class Win11ClockTogglerGUI : Form
private readonly string REG_CHKNOTIFAREA_STATUS = "chkNotifArea_Status";
private readonly string REG_CHKALLLDISPLAYS_STATUS = "chkAllDisplays_Status";

private static int TOGGLE_KEY_ID = 1;
private static int STEALTH_KEY_ID = 2;

public Win11ClockTogglerGUI()
{
InitializeComponent();

// Register hotkeys
int keyModifiers = 0x008 + 0x004; // Win + Shift
RegisterHotKey(this.Handle, TOGGLE_KEY_ID, keyModifiers, (int)Keys.F6);
RegisterHotKey(this.Handle, STEALTH_KEY_ID, keyModifiers, (int)Keys.F7);
}

private void CheckBoxes_Paint(object sender, PaintEventArgs e)
Expand All @@ -40,6 +51,47 @@ private void btnExit_Click(object sender, EventArgs e)
this.Close();
}

// Process any messages sent to this window
protected override void WndProc(ref Message m)
{
bool passThroughMsg = true;

// Catch the WM_HOTKEY message to handle any hotkeys being pressed
// https://docs.microsoft.com/en-us/windows/win32/inputdev/wm-hotkey
const int WM_HOTKEY = 0x0312;
if (m.Msg == WM_HOTKEY)
{
int id = m.WParam.ToInt32();
if (id == TOGGLE_KEY_ID)
{
btnHideShow_Click(null, null);
}
else if (id == STEALTH_KEY_ID)
{
toggleStealthMode();
}
}

// Catch the WM_SYSCOMMAND message
// https://docs.microsoft.com/en-us/windows/win32/menurc/wm-syscommand
const int WM_SYSCOMMAND = 0x0112;
if (m.Msg == WM_SYSCOMMAND)
{
// When the window is being minimized (SC_MINIMIZE)
const int SC_MINIMIZE = 0xf020;
if (m.WParam.ToInt32() == SC_MINIMIZE)
{
passThroughMsg = false; // To prevent the regular minimize behaviour
toggleStealthMode();
}
}

if (passThroughMsg)
{
base.WndProc(ref m);
}
}

private void btnHideShow_Click(object sender, EventArgs e)
{
//By default, hide at least the clock
Expand Down Expand Up @@ -110,7 +162,7 @@ private void Win11ClockTogglerGUI_Load(object sender, EventArgs e)

//Check if there are secondary taskbars in secondary windows
if (!Helper.AreThereSecondaryTaskbars())
{
{
//Disable checkbox if there are not secondary taskbars
chkSecondary.Checked = false;
DisableCheckBox(chkSecondary);
Expand Down Expand Up @@ -141,6 +193,22 @@ private void Win11ClockTogglerGUI_FormClosing(object sender, FormClosingEventArg
}
}

private void toggleStealthMode()
{
if (Visible)
{
Hide();
MessageBox.Show("The Win11ClockToggler window is now completely hidden.\nWhenever you want to bring it back, press Win+Shift+F7.");

}
else
{
Show();
this.WindowState = FormWindowState.Normal;
BringToFront();
}
}

//Timer to monitor if the current notification area pops up again because of a new icon or notification
private void tmrShowMonitor_Tick(object sender, EventArgs e)
{
Expand Down

0 comments on commit 1716fba

Please sign in to comment.