100 lines
2.6 KiB
C#
100 lines
2.6 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Drawing;
|
||
using System.IO;
|
||
using System.Reflection;
|
||
using System.Runtime.InteropServices;
|
||
using System.Windows.Forms;
|
||
|
||
namespace PrivacyGlass
|
||
{
|
||
public class TrayApp : ApplicationContext
|
||
{
|
||
const uint MOD_ALT = 0x0001;
|
||
const uint MOD_CONTROL = 0x0002;
|
||
const uint HOTKEY_ID = 1;
|
||
|
||
private NotifyIcon tray;
|
||
private List<BlurOverlay> overlays = new List<BlurOverlay>();
|
||
//private const int HOTKEY_ID = 100;
|
||
|
||
[DllImport("user32.dll")]
|
||
static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
|
||
|
||
[DllImport("user32.dll")]
|
||
static extern bool UnregisterHotKey(IntPtr hWnd, int id);
|
||
|
||
private void RegisterGlobalHotKey()
|
||
{
|
||
// Ctrl + Alt + Space
|
||
RegisterHotKey(
|
||
IntPtr.Zero,
|
||
(int)HOTKEY_ID,
|
||
MOD_CONTROL | MOD_ALT,
|
||
(uint)Keys.Space
|
||
);
|
||
}
|
||
|
||
public TrayApp()
|
||
{
|
||
CreateTrayIcon();
|
||
CreateOverlays();
|
||
RegisterGlobalHotKey();
|
||
Application.AddMessageFilter(new HotkeyFilter(Toggle));
|
||
}
|
||
|
||
|
||
private void CreateTrayIcon()
|
||
{
|
||
var assembly = Assembly.GetExecutingAssembly();
|
||
using (Stream s = assembly.GetManifestResourceStream("PrivacyGlass.Resources.privacyglass.ico"))
|
||
{
|
||
|
||
Icon trayicon = new Icon(s);
|
||
|
||
tray = new NotifyIcon()
|
||
{
|
||
Text = "PrivacyCurtain",
|
||
Icon = trayicon,
|
||
Visible = true,
|
||
ContextMenuStrip = BuildMenu()
|
||
};
|
||
}
|
||
|
||
}
|
||
|
||
private ContextMenuStrip BuildMenu()
|
||
{
|
||
var menu = new ContextMenuStrip();
|
||
|
||
menu.Items.Add("Toggle Now", null, (_, __) => Toggle());
|
||
menu.Items.Add("Exit", null, Exit);
|
||
|
||
return menu;
|
||
}
|
||
|
||
private void CreateOverlays()
|
||
{
|
||
foreach (var screen in Screen.AllScreens)
|
||
{
|
||
BlurOverlay ov = new BlurOverlay(screen.Bounds);
|
||
ov.TintColor = Color.Black;
|
||
ov.TintOpacity = 0; // 0–255, higher = darker
|
||
|
||
overlays.Add(ov);
|
||
}
|
||
}
|
||
|
||
private void Toggle() =>
|
||
overlays.ForEach(o => o.Toggle());
|
||
|
||
private void Exit(object sender, EventArgs e)
|
||
{
|
||
UnregisterHotKey(IntPtr.Zero, (int)HOTKEY_ID);
|
||
tray.Visible = false;
|
||
Application.Exit();
|
||
}
|
||
|
||
}
|
||
|
||
}
|