mirror of
https://github.com/Stone-Red-Code/InvLock.git
synced 2026-09-04 23:41:16 +02:00
Move code to src directory
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.12.35527.113 d17.12
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InvLock", "InvLock\InvLock.csproj", "{CE36EA45-CC69-468A-8550-EF8912236277}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{CE36EA45-CC69-468A-8550-EF8912236277}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{CE36EA45-CC69-468A-8550-EF8912236277}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{CE36EA45-CC69-468A-8550-EF8912236277}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{CE36EA45-CC69-468A-8550-EF8912236277}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -0,0 +1,15 @@
|
||||
<Application x:Class="InvLock.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
|
||||
xmlns:local="clr-namespace:InvLock"
|
||||
StartupUri="MainWindow.xaml">
|
||||
<Application.Resources>
|
||||
<ResourceDictionary>
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<ui:ThemesDictionary Theme="Dark" />
|
||||
<ui:ControlsDictionary />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
</ResourceDictionary>
|
||||
</Application.Resources>
|
||||
</Application>
|
||||
@@ -0,0 +1,140 @@
|
||||
using CuteUtils.Logging;
|
||||
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
|
||||
namespace InvLock;
|
||||
|
||||
/// <summary>
|
||||
/// Interaction logic for App.xaml
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
public const string AppGuid = "4B2411E5-1AE9-450C-AF2F-9A515ECBB9DF";
|
||||
|
||||
public const string AppName = "InvLock";
|
||||
|
||||
// This is the previous name of the application, used to migrate the application data folder
|
||||
private const string PreviousAppName = "InvLock";
|
||||
|
||||
private static readonly string logFilePath = Path.Combine(ApplicationDataPath, $"{AppName}.log");
|
||||
private readonly Thread? eventThread;
|
||||
private readonly EventWaitHandle eventWaitHandle;
|
||||
public static string ApplicationDataPath => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "StoneRed", AppName);
|
||||
|
||||
public static Logger Logger { get; } = new Logger()
|
||||
{
|
||||
Config = new()
|
||||
{
|
||||
FatalConfig = new OutputConfig()
|
||||
{
|
||||
ConsoleColor = ConsoleColor.DarkRed,
|
||||
LogTarget = LogTarget.DebugConsole | LogTarget.File,
|
||||
FilePath = logFilePath
|
||||
},
|
||||
ErrorConfig = new OutputConfig()
|
||||
{
|
||||
ConsoleColor = ConsoleColor.Red,
|
||||
LogTarget = LogTarget.DebugConsole | LogTarget.File,
|
||||
FilePath = logFilePath
|
||||
},
|
||||
WarnConfig = new OutputConfig()
|
||||
{
|
||||
ConsoleColor = ConsoleColor.Yellow,
|
||||
LogTarget = LogTarget.DebugConsole | LogTarget.File,
|
||||
FilePath = logFilePath
|
||||
},
|
||||
InfoConfig = new OutputConfig()
|
||||
{
|
||||
ConsoleColor = ConsoleColor.White,
|
||||
LogTarget = LogTarget.DebugConsole | LogTarget.File,
|
||||
FilePath = logFilePath
|
||||
},
|
||||
DebugConfig = new OutputConfig()
|
||||
{
|
||||
ConsoleColor = ConsoleColor.Gray,
|
||||
LogTarget = LogTarget.DebugConsole,
|
||||
},
|
||||
FormatConfig = new FormatConfig()
|
||||
{
|
||||
DebugConsoleFormat = $"> {{{LogFormatType.DateTime}:HH:mm:ss}} | {{{LogFormatType.LogSeverity},-5}} | {{{LogFormatType.Message}}}\nat {{{LogFormatType.LineNumber}}} | {{{LogFormatType.FilePath}}}"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private static string PreviousApplicationDataPath => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "StoneRed", PreviousAppName);
|
||||
|
||||
public App()
|
||||
{
|
||||
// Setup global event handler
|
||||
eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, AppGuid, out bool createdNew);
|
||||
|
||||
// Check if creating new was successful
|
||||
if (!createdNew)
|
||||
{
|
||||
Setup(false);
|
||||
Logger.LogWarn("Shutting down because other instance already running.", source: "Setup");
|
||||
// Shutdown Application
|
||||
_ = eventWaitHandle.Set();
|
||||
Current.Shutdown();
|
||||
}
|
||||
else
|
||||
{
|
||||
eventThread = new Thread(() =>
|
||||
{
|
||||
while (eventWaitHandle.WaitOne())
|
||||
{
|
||||
_ = Current.Dispatcher.BeginInvoke(() => ((MainWindow)Current.MainWindow).RestoreWindow());
|
||||
}
|
||||
});
|
||||
eventThread.Start();
|
||||
|
||||
Setup(true);
|
||||
Exit += CloseHandler;
|
||||
}
|
||||
}
|
||||
|
||||
protected void CloseHandler(object sender, EventArgs e)
|
||||
{
|
||||
eventWaitHandle.Close();
|
||||
eventThread?.Interrupt();
|
||||
}
|
||||
|
||||
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
||||
{
|
||||
Exception exception = (Exception)e.ExceptionObject;
|
||||
Logger.LogFatal(exception + (e.IsTerminating ? "\t Process terminating!" : ""), source: exception.Source ?? "Unknown");
|
||||
}
|
||||
|
||||
private static void Setup(bool clearLogFile)
|
||||
{
|
||||
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
||||
|
||||
try
|
||||
{
|
||||
if (Directory.Exists(PreviousApplicationDataPath) && !Directory.Exists(ApplicationDataPath))
|
||||
{
|
||||
Directory.Move(PreviousApplicationDataPath, ApplicationDataPath);
|
||||
Logger.LogInfo("Migrated ApplicationData folder", source: "Setup");
|
||||
}
|
||||
|
||||
if (!Directory.Exists(ApplicationDataPath))
|
||||
{
|
||||
_ = Directory.CreateDirectory(ApplicationDataPath);
|
||||
Logger.LogInfo("Created ApplicationData folder", source: "Setup");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_ = MessageBox.Show(ex.ToString(), AppName, MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
Logger.Log(ex.Message, "Setup", LogSeverity.Error);
|
||||
}
|
||||
|
||||
if (clearLogFile)
|
||||
{
|
||||
Logger.ClearLogFile(LogSeverity.Info);
|
||||
}
|
||||
|
||||
Logger.LogInfo("Setup complete", source: "Setup");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using System.Windows;
|
||||
|
||||
[assembly: ThemeInfo(
|
||||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
|
||||
//(used if a resource is not found in the page,
|
||||
// or application resource dictionaries)
|
||||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||
//(used if a resource is not found in the page,
|
||||
// app, or any theme specific resource dictionaries)
|
||||
)]
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 259 KiB |
@@ -0,0 +1,294 @@
|
||||
using System.Globalization;
|
||||
using System.Windows;
|
||||
using System.Windows.Markup;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace InvLock.Controls;
|
||||
|
||||
[ContentProperty("Text")]
|
||||
public class OutlinedTextBlock : FrameworkElement, IAddChild
|
||||
{
|
||||
#region Private Fields
|
||||
|
||||
private Geometry _textGeometry;
|
||||
|
||||
#endregion Private Fields
|
||||
|
||||
#region Private Methods
|
||||
|
||||
/// <summary>
|
||||
/// Invoked when a dependency property has changed. Generate a new FormattedText object to display.
|
||||
/// </summary>
|
||||
/// <param name="d">OutlineText object whose property was updated.</param>
|
||||
/// <param name="e">Event arguments for the dependency property.</param>
|
||||
private static void OnOutlineTextInvalidated(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
((OutlinedTextBlock)d).CreateText();
|
||||
}
|
||||
|
||||
#endregion Private Methods
|
||||
|
||||
#region FrameworkElement Overrides
|
||||
|
||||
/// <summary>
|
||||
/// Create the outline geometry based on the formatted text.
|
||||
/// </summary>
|
||||
public void CreateText()
|
||||
{
|
||||
FontStyle fontStyle = FontStyles.Normal;
|
||||
FontWeight fontWeight = FontWeights.Medium;
|
||||
|
||||
if (Bold == true)
|
||||
{
|
||||
fontWeight = FontWeights.Bold;
|
||||
}
|
||||
|
||||
if (Italic == true)
|
||||
{
|
||||
fontStyle = FontStyles.Italic;
|
||||
}
|
||||
|
||||
// Create the formatted text based on the properties set.
|
||||
FormattedText formattedText = new FormattedText(
|
||||
Text,
|
||||
CultureInfo.GetCultureInfo("en-us"),
|
||||
FlowDirection.LeftToRight,
|
||||
new Typeface(Font, fontStyle, fontWeight, FontStretches.Normal),
|
||||
FontSize,
|
||||
Brushes.Black // This brush does not matter since we use the geometry of the text.
|
||||
);
|
||||
|
||||
// Build the geometry object that represents the text.
|
||||
_textGeometry = formattedText.BuildGeometry(new Point(0, 0));
|
||||
|
||||
//set the size of the custome control based on the size of the text
|
||||
MinWidth = formattedText.Width;
|
||||
MinHeight = formattedText.Height;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// OnRender override draws the geometry of the text and optional highlight.
|
||||
/// </summary>
|
||||
/// <param name="drawingContext">Drawing context of the OutlineText control.</param>
|
||||
protected override void OnRender(DrawingContext drawingContext)
|
||||
{
|
||||
CreateText();
|
||||
// Draw the outline based on the properties that are set.
|
||||
drawingContext.DrawGeometry(Fill, new Pen(Stroke, StrokeThickness), _textGeometry);
|
||||
}
|
||||
|
||||
#endregion FrameworkElement Overrides
|
||||
|
||||
#region DependencyProperties
|
||||
|
||||
/// <summary>
|
||||
/// Identifies the Bold dependency property.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty BoldProperty = DependencyProperty.Register(
|
||||
"Bold",
|
||||
typeof(bool),
|
||||
typeof(OutlinedTextBlock),
|
||||
new FrameworkPropertyMetadata(
|
||||
false,
|
||||
FrameworkPropertyMetadataOptions.AffectsRender,
|
||||
new PropertyChangedCallback(OnOutlineTextInvalidated),
|
||||
null
|
||||
)
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Identifies the Fill dependency property.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty FillProperty = DependencyProperty.Register(
|
||||
"Fill",
|
||||
typeof(Brush),
|
||||
typeof(OutlinedTextBlock),
|
||||
new FrameworkPropertyMetadata(
|
||||
new SolidColorBrush(Colors.LightSteelBlue),
|
||||
FrameworkPropertyMetadataOptions.AffectsRender,
|
||||
new PropertyChangedCallback(OnOutlineTextInvalidated),
|
||||
null
|
||||
)
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Identifies the Font dependency property.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty FontProperty = DependencyProperty.Register(
|
||||
"Font",
|
||||
typeof(FontFamily),
|
||||
typeof(OutlinedTextBlock),
|
||||
new FrameworkPropertyMetadata(
|
||||
new FontFamily("Arial"),
|
||||
FrameworkPropertyMetadataOptions.AffectsRender,
|
||||
new PropertyChangedCallback(OnOutlineTextInvalidated),
|
||||
null
|
||||
)
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Identifies the FontSize dependency property.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty FontSizeProperty = DependencyProperty.Register(
|
||||
"FontSize",
|
||||
typeof(double),
|
||||
typeof(OutlinedTextBlock),
|
||||
new FrameworkPropertyMetadata(
|
||||
48.0,
|
||||
FrameworkPropertyMetadataOptions.AffectsRender,
|
||||
new PropertyChangedCallback(OnOutlineTextInvalidated),
|
||||
null
|
||||
)
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Identifies the Italic dependency property.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ItalicProperty = DependencyProperty.Register(
|
||||
"Italic",
|
||||
typeof(bool),
|
||||
typeof(OutlinedTextBlock),
|
||||
new FrameworkPropertyMetadata(
|
||||
false,
|
||||
FrameworkPropertyMetadataOptions.AffectsRender,
|
||||
new PropertyChangedCallback(OnOutlineTextInvalidated),
|
||||
null
|
||||
)
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Identifies the Stroke dependency property.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty StrokeProperty = DependencyProperty.Register(
|
||||
"Stroke",
|
||||
typeof(Brush),
|
||||
typeof(OutlinedTextBlock),
|
||||
new FrameworkPropertyMetadata(
|
||||
new SolidColorBrush(Colors.Teal),
|
||||
FrameworkPropertyMetadataOptions.AffectsRender,
|
||||
new PropertyChangedCallback(OnOutlineTextInvalidated),
|
||||
null
|
||||
)
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Identifies the StrokeThickness dependency property.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty StrokeThicknessProperty = DependencyProperty.Register(
|
||||
"StrokeThickness",
|
||||
typeof(ushort),
|
||||
typeof(OutlinedTextBlock),
|
||||
new FrameworkPropertyMetadata(
|
||||
(ushort)0,
|
||||
FrameworkPropertyMetadataOptions.AffectsRender,
|
||||
new PropertyChangedCallback(OnOutlineTextInvalidated),
|
||||
null
|
||||
)
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Identifies the Text dependency property.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
|
||||
"Text",
|
||||
typeof(string),
|
||||
typeof(OutlinedTextBlock),
|
||||
new FrameworkPropertyMetadata(
|
||||
"",
|
||||
FrameworkPropertyMetadataOptions.AffectsRender,
|
||||
new PropertyChangedCallback(OnOutlineTextInvalidated),
|
||||
null
|
||||
)
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Specifies whether the font should display Bold font weight.
|
||||
/// </summary>
|
||||
public bool Bold
|
||||
{
|
||||
get => (bool)GetValue(BoldProperty);
|
||||
|
||||
set => SetValue(BoldProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies the brush to use for the fill of the formatted text.
|
||||
/// </summary>
|
||||
public Brush Fill
|
||||
{
|
||||
get => (Brush)GetValue(FillProperty);
|
||||
|
||||
set => SetValue(FillProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The font to use for the displayed formatted text.
|
||||
/// </summary>
|
||||
public FontFamily Font
|
||||
{
|
||||
get => (FontFamily)GetValue(FontProperty);
|
||||
|
||||
set => SetValue(FontProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The current font size.
|
||||
/// </summary>
|
||||
public double FontSize
|
||||
{
|
||||
get => (double)GetValue(FontSizeProperty);
|
||||
|
||||
set => SetValue(FontSizeProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies whether the font should display Italic font style.
|
||||
/// </summary>
|
||||
public bool Italic
|
||||
{
|
||||
get => (bool)GetValue(ItalicProperty);
|
||||
|
||||
set => SetValue(ItalicProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies the brush to use for the stroke and optional hightlight of the formatted text.
|
||||
/// </summary>
|
||||
public Brush Stroke
|
||||
{
|
||||
get => (Brush)GetValue(StrokeProperty);
|
||||
|
||||
set => SetValue(StrokeProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The stroke thickness of the font.
|
||||
/// </summary>
|
||||
public ushort StrokeThickness
|
||||
{
|
||||
get => (ushort)GetValue(StrokeThicknessProperty);
|
||||
|
||||
set => SetValue(StrokeThicknessProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies the text string to display.
|
||||
/// </summary>
|
||||
public string Text
|
||||
{
|
||||
get => (string)GetValue(TextProperty);
|
||||
|
||||
set => SetValue(TextProperty, value);
|
||||
}
|
||||
|
||||
public void AddChild(object value)
|
||||
{
|
||||
}
|
||||
|
||||
public void AddText(string value)
|
||||
{
|
||||
Text = value;
|
||||
}
|
||||
|
||||
#endregion DependencyProperties
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
|
||||
namespace InvLock.DataContexts;
|
||||
|
||||
internal class MainWindowDataContext : INotifyPropertyChanged
|
||||
{
|
||||
#if DEBUG
|
||||
public string Title => $"{App.AppName} - Dev {AppVersion}";
|
||||
#else
|
||||
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
|
||||
public string Title => $"{App.AppName} - {AppVersion}";
|
||||
#endif
|
||||
|
||||
public string AppName => App.AppName;
|
||||
|
||||
public string AppVersion => Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "Unknown";
|
||||
|
||||
public Settings Settings { get; } = Settings.Load();
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// This file is used by Code Analysis to maintain SuppressMessage
|
||||
// attributes that are applied to this project.
|
||||
// Project-level suppressions either have no target or are given
|
||||
// a specific target and scoped to a namespace, type, member, etc.
|
||||
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
[assembly: SuppressMessage("Minor Code Smell", "S2325:Methods and properties that don't access instance data should be static", Justification = "Required for bindings", Scope = "namespaceanddescendants", Target = "~N:InvLock.DataContexts")]
|
||||
[assembly: SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Required for bindings", Scope = "namespaceanddescendants", Target = "~N:InvLock.DataContexts")]
|
||||
@@ -0,0 +1,28 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net9.0-windows7.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UseWPF>true</UseWPF>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<ApplicationIcon>Assets\logo.ico</ApplicationIcon>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="icon.ico" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CuteUtils" Version="1.0.0" />
|
||||
<PackageReference Include="SharpHook" Version="5.3.8" />
|
||||
<PackageReference Include="WPF-UI" Version="3.0.5" />
|
||||
<PackageReference Include="WPF-UI.Tray" Version="3.0.5" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Resource Include="Assets\logo.ico" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,21 @@
|
||||
<Window x:Class="InvLock.LockWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:InvLock"
|
||||
xmlns:controls="clr-namespace:InvLock.Controls"
|
||||
mc:Ignorable="d"
|
||||
Deactivated="Window_Deactivated"
|
||||
AllowsTransparency="True"
|
||||
Background="Transparent"
|
||||
WindowStyle="None"
|
||||
Topmost="True"
|
||||
WindowStartupLocation="CenterScreen"
|
||||
Loaded="Window_Loaded"
|
||||
Closing="Window_Closing"
|
||||
ShowInTaskbar="False"
|
||||
Title="MainWindow" Height="Auto" Width="Auto">
|
||||
|
||||
<controls:OutlinedTextBlock x:Name="textBlock" Text="Lock Screen Active" Bold="True" Stroke="Red" Opacity="0" Fill="Black" StrokeThickness="1" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="50" Font="Segoe UI" />
|
||||
</Window>
|
||||
@@ -0,0 +1,238 @@
|
||||
using InvLock.Utilities;
|
||||
|
||||
using SharpHook;
|
||||
using SharpHook.Native;
|
||||
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Animation;
|
||||
|
||||
namespace InvLock;
|
||||
|
||||
/// <summary>
|
||||
/// Interaction logic for LockWindow.xaml
|
||||
/// </summary>
|
||||
public partial class LockWindow : Window
|
||||
{
|
||||
private readonly SimpleGlobalHook hook = new SimpleGlobalHook();
|
||||
private readonly Dictionary<KeyCode, DateTime> pressedKeys = [];
|
||||
private readonly Settings settings;
|
||||
private List<IntPtr> windows = [];
|
||||
private bool isOpen = true;
|
||||
private bool suppressInput = false;
|
||||
|
||||
public LockWindow(Settings settings)
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
Window w = new()
|
||||
{
|
||||
Left = -100,
|
||||
Top = -100,
|
||||
Width = 0,
|
||||
Height = 0,
|
||||
|
||||
WindowStyle = WindowStyle.ToolWindow,
|
||||
ShowInTaskbar = false
|
||||
};
|
||||
|
||||
WindowInteropHelper helper = new WindowInteropHelper(w);
|
||||
_ = helper.EnsureHandle();
|
||||
|
||||
Owner = w;
|
||||
|
||||
this.settings = settings;
|
||||
|
||||
hook.MouseMoved += Hook_Mouse;
|
||||
hook.MouseDragged += Hook_Mouse;
|
||||
hook.MousePressed += Hook_Mouse;
|
||||
hook.MouseReleased += Hook_Mouse;
|
||||
hook.MouseClicked += Hook_Mouse;
|
||||
hook.MouseWheel += Hook_MouseWheel;
|
||||
|
||||
hook.KeyReleased += Hook_KeyReleased;
|
||||
hook.KeyTyped += Hook_Key;
|
||||
hook.KeyPressed += Hook_KeyPressed;
|
||||
Microsoft.Win32.SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
|
||||
|
||||
_ = Task.Run(hook.Run);
|
||||
}
|
||||
|
||||
private void ActivateLockScreen()
|
||||
{
|
||||
if (settings.HideWindows)
|
||||
{
|
||||
MinimizeWindows();
|
||||
}
|
||||
|
||||
isOpen = true;
|
||||
suppressInput = true;
|
||||
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
_ = Activate();
|
||||
|
||||
textBlock.Text = settings.LockText;
|
||||
textBlock.Stroke = (Brush)FindResource("PaletteRedBrush");
|
||||
|
||||
Animation();
|
||||
});
|
||||
}
|
||||
|
||||
private void DeactivateLockScreen()
|
||||
{
|
||||
isOpen = false;
|
||||
Dispatcher.Invoke(Close);
|
||||
}
|
||||
|
||||
private void Hook_KeyPressed(object? sender, KeyboardHookEventArgs e)
|
||||
{
|
||||
foreach (KeyCode key in pressedKeys.Keys)
|
||||
{
|
||||
if (DateTime.UtcNow - pressedKeys[key] > TimeSpan.FromSeconds(1))
|
||||
{
|
||||
_ = pressedKeys.Remove(key);
|
||||
}
|
||||
}
|
||||
_ = pressedKeys[e.Data.KeyCode] = DateTime.UtcNow;
|
||||
|
||||
if (pressedKeys.ContainsKey(KeyCode.VcL) && pressedKeys.ContainsKey(KeyCode.VcLeftShift) && pressedKeys.ContainsKey(KeyCode.VcLeftControl) && pressedKeys.Count == 3)
|
||||
{
|
||||
if (suppressInput)
|
||||
{
|
||||
DeactivateLockScreen();
|
||||
}
|
||||
else
|
||||
{
|
||||
ActivateLockScreen();
|
||||
}
|
||||
}
|
||||
|
||||
e.SuppressEvent = suppressInput;
|
||||
}
|
||||
|
||||
private void Hook_KeyReleased(object? sender, KeyboardHookEventArgs e)
|
||||
{
|
||||
_ = pressedKeys.Remove(e.Data.KeyCode);
|
||||
e.SuppressEvent = suppressInput;
|
||||
}
|
||||
|
||||
private async void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e)
|
||||
{
|
||||
if (e.Reason == Microsoft.Win32.SessionSwitchReason.SessionUnlock)
|
||||
{
|
||||
if (settings.HideWindows)
|
||||
{
|
||||
RestoreWindows();
|
||||
}
|
||||
|
||||
isOpen = true;
|
||||
|
||||
_ = Activate();
|
||||
|
||||
await Task.Delay(500);
|
||||
|
||||
textBlock.Text = settings.UnlockText;
|
||||
textBlock.Stroke = (Brush)FindResource("PaletteGreenBrush");
|
||||
|
||||
Animation();
|
||||
}
|
||||
}
|
||||
|
||||
private void MinimizeWindows()
|
||||
{
|
||||
windows = WindowsApi.GetOpenWindows();
|
||||
|
||||
WindowsApi.ShowTaskbar(false);
|
||||
|
||||
IntPtr thisWindowHandle = Dispatcher.Invoke(() => new WindowInteropHelper(this).Handle);
|
||||
|
||||
foreach (IntPtr handle in windows)
|
||||
{
|
||||
if (handle == thisWindowHandle)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
_ = WindowsApi.ShowWindow(handle, WindowsApi.SW_MINIMIZE);
|
||||
}
|
||||
}
|
||||
|
||||
private void RestoreWindows()
|
||||
{
|
||||
WindowsApi.ShowTaskbar(true);
|
||||
|
||||
foreach (IntPtr handle in windows)
|
||||
{
|
||||
_ = WindowsApi.ShowWindow(handle, WindowsApi.SW_RESTORE);
|
||||
}
|
||||
}
|
||||
|
||||
private void Animation()
|
||||
{
|
||||
Storyboard storyboard = new Storyboard();
|
||||
TimeSpan duration = TimeSpan.FromMilliseconds(500);
|
||||
|
||||
DoubleAnimation fadeInAnimation = new DoubleAnimation()
|
||||
{ From = 0.0, To = 1.0, Duration = new Duration(duration) };
|
||||
|
||||
DoubleAnimation fadeOutAnimation = new DoubleAnimation
|
||||
{
|
||||
From = 1.0,
|
||||
To = 0.0,
|
||||
Duration = new Duration(duration),
|
||||
BeginTime = TimeSpan.FromSeconds(3)
|
||||
};
|
||||
|
||||
Storyboard.SetTargetName(fadeInAnimation, name: textBlock.Name);
|
||||
Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath("Opacity", 1));
|
||||
storyboard.Children.Add(fadeInAnimation);
|
||||
storyboard.Begin(textBlock);
|
||||
|
||||
Storyboard.SetTargetName(fadeOutAnimation, textBlock.Name);
|
||||
Storyboard.SetTargetProperty(fadeOutAnimation, new PropertyPath("Opacity", 0));
|
||||
storyboard.Children.Add(fadeOutAnimation);
|
||||
storyboard.Begin(textBlock);
|
||||
}
|
||||
|
||||
private void Hook_MouseWheel(object? sender, MouseWheelHookEventArgs e)
|
||||
{
|
||||
e.SuppressEvent = suppressInput;
|
||||
}
|
||||
|
||||
private void Hook_Mouse(object? sender, MouseHookEventArgs e)
|
||||
{
|
||||
e.SuppressEvent = suppressInput;
|
||||
}
|
||||
|
||||
private void Hook_Key(object? sender, KeyboardHookEventArgs e)
|
||||
{
|
||||
e.SuppressEvent = suppressInput;
|
||||
}
|
||||
|
||||
private void Window_Deactivated(object sender, EventArgs e)
|
||||
{
|
||||
if (isOpen)
|
||||
{
|
||||
isOpen = false;
|
||||
Close();
|
||||
}
|
||||
}
|
||||
|
||||
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
|
||||
{
|
||||
if (suppressInput)
|
||||
{
|
||||
_ = WindowsApi.LockWorkStation();
|
||||
suppressInput = false;
|
||||
}
|
||||
|
||||
e.Cancel = true;
|
||||
}
|
||||
|
||||
private void Window_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
_ = Activate();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
<ui:FluentWindow x:Class="InvLock.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:InvLock"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:tray="http://schemas.lepo.co/wpfui/2022/xaml/tray"
|
||||
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
|
||||
xmlns:dataContext="clr-namespace:InvLock.DataContexts"
|
||||
mc:Ignorable="d"
|
||||
d:DataContext="{d:DesignInstance Type=dataContext:MainWindowDataContext}"
|
||||
d:DesignHeight="450"
|
||||
d:DesignWidth="800"
|
||||
ui:Design.Background="{DynamicResource ApplicationBackgroundBrush}"
|
||||
ui:Design.Foreground="{DynamicResource TextFillColorPrimaryBrush}"
|
||||
ExtendsContentIntoTitleBar="True"
|
||||
Foreground="{DynamicResource TextFillColorPrimaryBrush}"
|
||||
WindowBackdropType="Mica"
|
||||
WindowCornerPreference="Round"
|
||||
WindowStartupLocation="CenterScreen"
|
||||
WindowState="Minimized"
|
||||
Loaded="Window_Loaded"
|
||||
Closing="FluentWindow_Closing"
|
||||
Closed="FluentWindow_Closed"
|
||||
Width="1100"
|
||||
Height="660">
|
||||
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="*" />
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<ui:TitleBar Title="{Binding Title}">
|
||||
<ui:TitleBar.Icon>
|
||||
<ui:ImageIcon Source="pack://application:,,,/Assets/logo.ico" />
|
||||
</ui:TitleBar.Icon>
|
||||
</ui:TitleBar>
|
||||
|
||||
<tray:NotifyIcon Grid.Row="0" FocusOnLeftClick="True" MenuOnRightClick="True" LeftClick="NotifyIcon_LeftClick" TooltipText="{Binding AppName}">
|
||||
<tray:NotifyIcon.Menu>
|
||||
<ContextMenu>
|
||||
<MenuItem Header="Settings" Click="SettingsMenuItem_Click" />
|
||||
<MenuItem Header="Quit" Click="QuitMenuItem_Click" />
|
||||
</ContextMenu>
|
||||
</tray:NotifyIcon.Menu>
|
||||
<tray:NotifyIcon.Icon>
|
||||
<BitmapImage UriSource="pack://application:,,,/Assets/logo.ico" />
|
||||
</tray:NotifyIcon.Icon>
|
||||
</tray:NotifyIcon>
|
||||
|
||||
<ui:DynamicScrollViewer Grid.Row="1" VirtualizingPanel.ScrollUnit="Pixel">
|
||||
<StackPanel Margin="24 0 24 24">
|
||||
<ui:TextBlock TextWrapping="Wrap" Margin="0 0 0 10" Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}">
|
||||
InvLock is a simple lock screen application for Windows 10/11.
|
||||
It is designed to be lightweight and easy to use.
|
||||
<LineBreak />
|
||||
If you have any issues or feature requests, please report them on GitHub.
|
||||
<LineBreak />
|
||||
<LineBreak />
|
||||
To lock and unlock your screen, press
|
||||
<ui:TextBlock Foreground="{DynamicResource AccentTextFillColorPrimaryBrush}">WIN + SHIFT + L</ui:TextBlock>
|
||||
</ui:TextBlock>
|
||||
|
||||
<ui:TextBlock Margin="0,0,0,8" FontTypography="BodyStrong" Text="Appearance & behavior" />
|
||||
<ui:CardControl Margin="0,0,0,12" Icon="{ui:SymbolIcon Color24}" Height="75">
|
||||
<ui:CardControl.Header>
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
<ui:TextBlock Grid.Row="0" FontTypography="Body" Text="App theme" />
|
||||
<ui:HyperlinkButton Grid.Row="1" Margin="0" Padding="1" FontSize="12" NavigateUri="ms-settings:colors">
|
||||
<ui:TextBlock Text="Windows color settings" />
|
||||
</ui:HyperlinkButton>
|
||||
</Grid>
|
||||
</ui:CardControl.Header>
|
||||
<ComboBox Grid.Column="1" MinWidth="200" SelectedIndex="0">
|
||||
<ComboBoxItem Content="Windows default" />
|
||||
<ComboBoxItem Content="Light" />
|
||||
<ComboBoxItem Content="Dark" />
|
||||
</ComboBox>
|
||||
</ui:CardControl>
|
||||
|
||||
<ui:CardControl Margin="0,0,0,12" Icon="{ui:SymbolIcon Symbol=Window24}" Height="75">
|
||||
<ui:CardControl.Header>
|
||||
<ui:TextBlock Grid.Row="0" FontTypography="Body" Text="Show desktop while locked" />
|
||||
</ui:CardControl.Header>
|
||||
<ui:ToggleSwitch Grid.Column="1" IsChecked="{Binding Settings.HideWindows}" OffContent="Off" OnContent="On" />
|
||||
</ui:CardControl>
|
||||
|
||||
<ui:CardControl Margin="0,0,0,12" Icon="{ui:SymbolIcon Symbol=LockClosed24}" Height="75">
|
||||
<ui:CardControl.Header>
|
||||
<ui:TextBlock Grid.Row="0" FontTypography="Body" Text="Lock text" />
|
||||
</ui:CardControl.Header>
|
||||
<ui:TextBox Grid.Column="1" MinWidth="200" Text="{Binding Settings.LockText, UpdateSourceTrigger=PropertyChanged}" PlaceholderText="Type your lock text here" />
|
||||
</ui:CardControl>
|
||||
|
||||
<ui:CardControl Margin="0,0,0,12" Icon="{ui:SymbolIcon Symbol=LockOpen24}" Height="75">
|
||||
<ui:CardControl.Header>
|
||||
<ui:TextBlock Grid.Row="0" FontTypography="Body" Text="Unlock text" />
|
||||
</ui:CardControl.Header>
|
||||
<ui:TextBox Grid.Column="1" MinWidth="200" Text="{Binding Settings.UnlockText, UpdateSourceTrigger=PropertyChanged}" PlaceholderText="Type your unlock text here" />
|
||||
</ui:CardControl>
|
||||
|
||||
<ui:TextBlock Margin="0,24,0,8" FontTypography="BodyStrong" Text="About" />
|
||||
<ui:CardExpander ContentPadding="0">
|
||||
<ui:CardExpander.Header>
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<ui:TextBlock Grid.Row="0" Grid.Column="0" FontTypography="Body" Text="{Binding AppName}" />
|
||||
<ui:TextBlock Grid.Row="1" Grid.Column="0" Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}" FontSize="12" Text="Stone_Red | MIT" />
|
||||
<TextBlock Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" Margin="0,0,16,0" VerticalAlignment="Center" Foreground="{ui:ThemeResource TextFillColorSecondaryBrush}" Text="{Binding AppVersion, Mode=OneWay}" />
|
||||
</Grid>
|
||||
</ui:CardExpander.Header>
|
||||
<StackPanel>
|
||||
<ui:Anchor Margin="0" Padding="16" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Background="Transparent" CornerRadius="0" BorderThickness="0" NavigateUri="https://github.com/Stone-Red-Code/InvLock/issues/new/choose">
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Grid.Column="0" Text="Report an issue or request a feature" />
|
||||
<ui:SymbolIcon Grid.Column="1" Symbol="Link24" />
|
||||
</Grid>
|
||||
</ui:Anchor>
|
||||
|
||||
<Separator />
|
||||
|
||||
<Grid Margin="16">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Grid.Column="0" Text="Clone repository" />
|
||||
<TextBlock Grid.Column="1" Foreground="{ui:ThemeResource TextFillColorSecondaryBrush}" Text="git clone https://github.com/Stone-Red-Code/InvLock" />
|
||||
</Grid>
|
||||
|
||||
<Separator />
|
||||
|
||||
<TextBlock Margin="16" Text="{Binding AppVersion, StringFormat='Version {0}'}" />
|
||||
</StackPanel>
|
||||
</ui:CardExpander>
|
||||
</StackPanel>
|
||||
</ui:DynamicScrollViewer>
|
||||
</Grid>
|
||||
</ui:FluentWindow>
|
||||
@@ -0,0 +1,88 @@
|
||||
using InvLock.DataContexts;
|
||||
|
||||
using System.Windows;
|
||||
|
||||
using Wpf.Ui.Appearance;
|
||||
using Wpf.Ui.Controls;
|
||||
|
||||
namespace InvLock;
|
||||
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWindow.xaml
|
||||
/// </summary>
|
||||
public partial class MainWindow : FluentWindow
|
||||
{
|
||||
private readonly MainWindowDataContext mainWindowDataContext = new MainWindowDataContext();
|
||||
private LockWindow? lockWindow;
|
||||
private bool blockWindowClosing = true;
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
DataContext = mainWindowDataContext;
|
||||
|
||||
InitializeComponent();
|
||||
|
||||
SystemThemeWatcher.Watch(this);
|
||||
}
|
||||
|
||||
internal void RestoreWindow()
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
ShowInTaskbar = true;
|
||||
Visibility = Visibility.Visible;
|
||||
SystemCommands.RestoreWindow(this);
|
||||
Topmost = true;
|
||||
_ = Activate();
|
||||
Topmost = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void SettingsMenuItem_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
RestoreWindow();
|
||||
}
|
||||
|
||||
private void QuitMenuItem_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Quit();
|
||||
}
|
||||
|
||||
private void Window_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Close();
|
||||
|
||||
lockWindow = new LockWindow(mainWindowDataContext.Settings);
|
||||
lockWindow.Show();
|
||||
}
|
||||
|
||||
private void NotifyIcon_LeftClick(Wpf.Ui.Tray.Controls.NotifyIcon sender, RoutedEventArgs e)
|
||||
{
|
||||
RestoreWindow();
|
||||
}
|
||||
|
||||
private void FluentWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
|
||||
{
|
||||
mainWindowDataContext.Settings.Save();
|
||||
|
||||
if (blockWindowClosing)
|
||||
{
|
||||
e.Cancel = true;
|
||||
|
||||
ShowInTaskbar = false;
|
||||
Visibility = Visibility.Collapsed;
|
||||
}
|
||||
}
|
||||
|
||||
private void Quit()
|
||||
{
|
||||
blockWindowClosing = false;
|
||||
Close();
|
||||
}
|
||||
|
||||
private void FluentWindow_Closed(object sender, EventArgs e)
|
||||
{
|
||||
lockWindow?.Close();
|
||||
Environment.Exit(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace InvLock;
|
||||
|
||||
public class Settings : INotifyPropertyChanged
|
||||
{
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
|
||||
private static readonly string settingsPath = Path.Combine(App.ApplicationDataPath, "settings.json");
|
||||
private bool hideWindows = false;
|
||||
private string lockText = "Lock Screen Active";
|
||||
|
||||
private string unlockText = "Lock Screen Inactive";
|
||||
|
||||
public bool HideWindows
|
||||
{
|
||||
get => hideWindows;
|
||||
set
|
||||
{
|
||||
hideWindows = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public string LockText
|
||||
{
|
||||
get => lockText;
|
||||
set
|
||||
{
|
||||
lockText = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public string UnlockText
|
||||
{
|
||||
get => unlockText;
|
||||
set
|
||||
{
|
||||
unlockText = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public static Settings Load()
|
||||
{
|
||||
if (File.Exists(settingsPath))
|
||||
{
|
||||
string json = File.ReadAllText(settingsPath);
|
||||
return JsonSerializer.Deserialize<Settings>(json) ?? new Settings();
|
||||
}
|
||||
|
||||
return new Settings();
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
string json = JsonSerializer.Serialize(this);
|
||||
File.WriteAllText(settingsPath, json);
|
||||
}
|
||||
|
||||
protected void OnPropertyChanged([CallerMemberName] string? name = null)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace InvLock.Utilities;
|
||||
|
||||
public static class WindowsApi
|
||||
{
|
||||
public const int SW_HIDE = 0;
|
||||
public const int SW_SHOWNORMAL = 1;
|
||||
public const int SW_NORMAL = 1;
|
||||
public const int SW_SHOWMINIMIZED = 2;
|
||||
public const int SW_SHOWMAXIMIZED = 3;
|
||||
public const int SW_MAXIMIZE = 3;
|
||||
public const int SW_SHOWNOACTIVATE = 4;
|
||||
public const int SW_SHOW = 5;
|
||||
public const int SW_MINIMIZE = 6;
|
||||
public const int SW_SHOWMINNOACTIVE = 7;
|
||||
public const int SW_SHOWNA = 8;
|
||||
public const int SW_RESTORE = 9;
|
||||
public const int SW_SHOWDEFAULT = 10;
|
||||
public const int SW_FORCEMINIMIZE = 11;
|
||||
|
||||
private delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);
|
||||
|
||||
public static List<IntPtr> GetOpenWindows()
|
||||
{
|
||||
IntPtr shellWindow = GetShellWindow();
|
||||
List<IntPtr> windows = [];
|
||||
|
||||
_ = EnumWindows(delegate (IntPtr hWnd, int lParam)
|
||||
{
|
||||
StringBuilder title = new StringBuilder(200);
|
||||
|
||||
_ = GetWindowText(hWnd, title, title.Capacity);
|
||||
|
||||
Debug.WriteLine($"{hWnd} | {title}");
|
||||
|
||||
if (hWnd == shellWindow)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!IsWindowVisible(hWnd) || IsIconic(hWnd))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (GetWindowTextLength(hWnd) == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
windows.Add(hWnd);
|
||||
return true;
|
||||
}, 0);
|
||||
|
||||
windows.Reverse();
|
||||
|
||||
return windows;
|
||||
}
|
||||
|
||||
public static void ShowTaskbar(bool show)
|
||||
{
|
||||
IntPtr hWnd = FindWindow("Shell_TrayWnd", null);
|
||||
_ = ShowWindow(hWnd, show ? SW_SHOW : SW_HIDE);
|
||||
|
||||
// Taskbars on other monitors
|
||||
hWnd = FindWindow("Shell_SecondaryTrayWnd", null);
|
||||
_ = ShowWindow(hWnd, show ? SW_SHOW : SW_HIDE);
|
||||
}
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
internal static extern bool LockWorkStation();
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool IsWindowVisible(IntPtr hWnd);
|
||||
|
||||
[DllImport("USER32.DLL")]
|
||||
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
|
||||
|
||||
[DllImport("USER32.DLL")]
|
||||
private static extern int GetWindowTextLength(IntPtr hWnd);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern IntPtr GetShellWindow();
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern uint GetWindowLong(IntPtr hWnd, int nIndex);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern IntPtr FindWindow(string lpClassName, string? lpWindowName);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool IsIconic(IntPtr hWnd);
|
||||
}
|
||||
Reference in New Issue
Block a user