Skip to content

Commit 6350903

Browse files
Add screen blur option
1 parent a13babb commit 6350903

File tree

6 files changed

+178
-3
lines changed

6 files changed

+178
-3
lines changed

src/InvLock/BlurWindow.xaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Window x:Class="InvLock.BlurWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:InvLock"
7+
mc:Ignorable="d"
8+
Loaded="Window_Loaded"
9+
WindowStyle="None"
10+
MouseDown="Window_MouseDown"
11+
AllowsTransparency="True"
12+
Background="Black"
13+
Opacity="0"
14+
Title="BlurWindow" Height="450" Width="800">
15+
</Window>

src/InvLock/BlurWindow.xaml.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using InvLock.Utilities;
2+
3+
using System.Windows;
4+
5+
namespace InvLock;
6+
7+
/// <summary>
8+
/// Interaction logic for BlurWindow.xaml
9+
/// </summary>
10+
public partial class BlurWindow : Window
11+
{
12+
public BlurWindow()
13+
{
14+
InitializeComponent();
15+
}
16+
17+
private void Window_Loaded(object sender, RoutedEventArgs e)
18+
{
19+
WindowsApi.EnableBlur(this);
20+
}
21+
22+
private void Window_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
23+
{
24+
DragMove();
25+
}
26+
}

src/InvLock/LockWindow.xaml.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public partial class LockWindow : Window
1919
private readonly SimpleGlobalHook hook = new SimpleGlobalHook();
2020
private readonly Dictionary<KeyCode, DateTime> pressedKeys = [];
2121
private readonly Settings settings;
22+
private readonly List<BlurWindow> blurWindows = [];
2223
private List<IntPtr> windows = [];
24+
private Point lastMousePosition;
2325
private bool isOpen = true;
2426
private bool suppressInput = false;
2527

@@ -120,11 +122,32 @@ private void ActivateLockScreen()
120122
MinimizeWindows();
121123
}
122124

123-
isOpen = true;
124-
suppressInput = true;
125-
126125
Dispatcher.Invoke(() =>
127126
{
127+
lastMousePosition = WpfScreenHelper.MouseHelper.MousePosition;
128+
129+
if (settings.BlurScreen)
130+
{
131+
foreach (Rect bounds in WpfScreenHelper.Screen.AllScreens.Select(s => s.WpfBounds))
132+
{
133+
BlurWindow blurWindow = new()
134+
{
135+
Width = bounds.Width,
136+
Height = bounds.Height,
137+
Left = bounds.Left,
138+
Top = bounds.Top,
139+
};
140+
141+
blurWindows.Add(blurWindow);
142+
blurWindow.Show();
143+
}
144+
145+
_ = WindowsApi.SetCursorPosition(new Point(100000, 100000));
146+
}
147+
148+
isOpen = true;
149+
suppressInput = true;
150+
128151
_ = Activate();
129152

130153
textBlock.Text = settings.LockText;
@@ -155,6 +178,9 @@ private void DeactivateLockScreen()
155178

156179
isOpen = true;
157180

181+
_ = WindowsApi.SetCursorPosition(lastMousePosition);
182+
blurWindows.ForEach(w => w.Close());
183+
158184
textBlock.Text = settings.UnlockText;
159185
textBlock.Stroke = (Brush)FindResource("PaletteGreenBrush");
160186

@@ -203,6 +229,9 @@ private async void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.Ses
203229

204230
isOpen = true;
205231

232+
_ = WindowsApi.SetCursorPosition(lastMousePosition);
233+
blurWindows.ForEach(w => w.Close());
234+
206235
_ = Activate();
207236

208237
await Task.Delay(500);

src/InvLock/MainWindow.xaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,34 @@
7979
</ComboBox>
8080
</ui:CardControl>
8181

82+
<ui:CardControl Margin="0,0,0,12" Icon="{ui:SymbolIcon Symbol=Clock24}" Height="75">
83+
<ui:CardControl.Header>
84+
<Grid>
85+
<Grid.RowDefinitions>
86+
<RowDefinition Height="Auto" />
87+
<RowDefinition Height="Auto" />
88+
</Grid.RowDefinitions>
89+
<ui:TextBlock Grid.Row="0" FontTypography="Body" Text="Run at startup" />
90+
<ui:TextBlock Grid.Row="1" Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}" FontSize="12" Text="Start InvLock when you sign in to Windows" />
91+
</Grid>
92+
</ui:CardControl.Header>
93+
<ui:ToggleSwitch Grid.Column="1" IsChecked="{Binding IsAutoStartEnabled}" OffContent="Off" OnContent="On" />
94+
</ui:CardControl>
95+
8296
<ui:CardControl Margin="0,0,0,12" Icon="{ui:SymbolIcon Symbol=Window24}" Height="75">
8397
<ui:CardControl.Header>
8498
<ui:TextBlock Grid.Row="0" FontTypography="Body" Text="Show desktop while locked" />
8599
</ui:CardControl.Header>
86100
<ui:ToggleSwitch Grid.Column="1" IsChecked="{Binding Settings.HideWindows}" OffContent="Off" OnContent="On" />
87101
</ui:CardControl>
88102

103+
<ui:CardControl Margin="0,0,0,12" Icon="{ui:SymbolIcon Symbol=Blur24}" Height="75">
104+
<ui:CardControl.Header>
105+
<ui:TextBlock Grid.Row="0" FontTypography="Body" Text="Blur screen while locked" />
106+
</ui:CardControl.Header>
107+
<ui:ToggleSwitch Grid.Column="1" IsChecked="{Binding Settings.BlurScreen}" OffContent="Off" OnContent="On" />
108+
</ui:CardControl>
109+
89110
<ui:CardControl Margin="0,0,0,12" Icon="{ui:SymbolIcon Symbol=WindowShield24}" Height="75">
90111
<ui:CardControl.Header>
91112
<ui:TextBlock Grid.Row="0" FontTypography="Body" Text="Use windows lock screen to unlock" />

src/InvLock/Settings.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class Settings : INotifyPropertyChanged
1717
private static readonly string settingsPath = Path.Combine(App.ApplicationDataPath, "settings.json");
1818
private bool useWindowsLockScreen = true;
1919
private bool hideWindows = false;
20+
private bool blurScreen = false;
2021
private string lockText = "Lock Screen Active";
2122
private string theme = "Windows default";
2223
private HashSet<KeyCode> lockShortcut = [KeyCode.VcLeftControl, KeyCode.VcLeftShift, KeyCode.VcL];
@@ -34,6 +35,16 @@ public bool HideWindows
3435
}
3536
}
3637

38+
public bool BlurScreen
39+
{
40+
get => blurScreen;
41+
set
42+
{
43+
blurScreen = value;
44+
OnPropertyChanged();
45+
}
46+
}
47+
3748
public bool UseWindowsLockScreen
3849
{
3950
get => useWindowsLockScreen;

src/InvLock/Utilities/WindowsApi.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Diagnostics;
22
using System.Runtime.InteropServices;
33
using System.Text;
4+
using System.Windows;
5+
using System.Windows.Interop;
46

57
namespace InvLock.Utilities;
68

@@ -70,12 +72,46 @@ public static void ShowTaskbar(bool show)
7072
_ = ShowWindow(hWnd, show ? SW_SHOW : SW_HIDE);
7173
}
7274

75+
public static bool SetCursorPosition(Point point)
76+
{
77+
return SetCursorPos((int)point.X, (int)point.Y);
78+
}
79+
80+
public static void EnableBlur(Window window)
81+
{
82+
WindowInteropHelper windowHelper = new WindowInteropHelper(window);
83+
84+
AccentPolicy accent = new AccentPolicy
85+
{
86+
AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND
87+
};
88+
89+
int accentStructSize = Marshal.SizeOf(accent);
90+
91+
nint accentPtr = Marshal.AllocHGlobal(accentStructSize);
92+
Marshal.StructureToPtr(accent, accentPtr, false);
93+
94+
WindowCompositionAttributeData data = new WindowCompositionAttributeData
95+
{
96+
Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY,
97+
SizeOfData = accentStructSize,
98+
Data = accentPtr
99+
};
100+
101+
_ = SetWindowCompositionAttribute(windowHelper.Handle, ref data);
102+
103+
Marshal.FreeHGlobal(accentPtr);
104+
}
105+
73106
[DllImport("user32.dll")]
74107
internal static extern bool LockWorkStation();
75108

76109
[DllImport("user32.dll")]
77110
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
78111

112+
[DllImport("user32.dll")]
113+
internal static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);
114+
79115
[DllImport("user32.dll")]
80116
private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);
81117

@@ -102,4 +138,41 @@ public static void ShowTaskbar(bool show)
102138

103139
[DllImport("user32.dll")]
104140
private static extern bool IsIconic(IntPtr hWnd);
141+
142+
[DllImport("user32.dll")]
143+
private static extern bool SetCursorPos(int x, int y);
144+
145+
internal enum AccentState
146+
{
147+
ACCENT_DISABLED = 1,
148+
ACCENT_ENABLE_GRADIENT = 0,
149+
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
150+
ACCENT_ENABLE_BLURBEHIND = 3,
151+
ACCENT_INVALID_STATE = 4
152+
}
153+
154+
internal enum WindowCompositionAttribute
155+
{
156+
// ...
157+
WCA_ACCENT_POLICY = 19
158+
159+
// ...
160+
}
161+
162+
[StructLayout(LayoutKind.Sequential)]
163+
internal struct AccentPolicy
164+
{
165+
public AccentState AccentState;
166+
public int AccentFlags;
167+
public int GradientColor;
168+
public int AnimationId;
169+
}
170+
171+
[StructLayout(LayoutKind.Sequential)]
172+
internal struct WindowCompositionAttributeData
173+
{
174+
public WindowCompositionAttribute Attribute;
175+
public IntPtr Data;
176+
public int SizeOfData;
177+
}
105178
}

0 commit comments

Comments
 (0)