Skip to content

Commit

Permalink
#22 锁屏时支持隐藏鼠标光标
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuLing-zhang committed Dec 28, 2024
1 parent 827899f commit caab73c
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/ComputerLock/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ private void Init()
});
services.AddSingleton(LogManager.GetLogger());
services.AddSingleton<KeyboardHook>();
services.AddSingleton<MouseHook>();
services.AddSingleton<UpdateHelper>();
services.AddSingleton<AutostartHook>();
services.AddSingleton<TaskManagerHook>();
Expand Down
5 changes: 5 additions & 0 deletions src/ComputerLock/Configuration/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,9 @@ public class AppSettings
/// 密码框激活方式
/// </summary>
public PasswordBoxActiveMethodEnum PasswordBoxActiveMethod { get; set; } = PasswordBoxActiveMethodEnum.KeyboardDown | PasswordBoxActiveMethodEnum.MouseDown;

/// <summary>
/// 自动隐藏鼠标光标
/// </summary>
public bool IsHideMouseCursor { get; set; } = false;
}
50 changes: 50 additions & 0 deletions src/ComputerLock/Hooks/MouseHook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Runtime.InteropServices;

namespace ComputerLock.Hooks;
internal class MouseHook
{
[DllImport("user32.dll")]
private static extern int ShowCursor(bool bShow);

private int _cursorCount = 0;

/// <summary>
/// 隐藏鼠标光标。
/// </summary>
public void HideCursor()
{
if (_cursorCount >= 0) // 如果光标可见
{
_cursorCount = ShowCursor(false); // 隐藏光标
}
}

/// <summary>
/// 显示鼠标光标。
/// </summary>
public void ShowCursor()
{
if (_cursorCount < 0) // 如果光标不可见
{
_cursorCount = ShowCursor(true); // 显示光标
}
}

/// <summary>
/// 重置光标显示状态。
/// </summary>
public void ResetCursorState()
{
while (_cursorCount < 0) // 如果光标隐藏
{
ShowCursor(true); // 显示光标
_cursorCount++;
}

while (_cursorCount > 0) // 如果光标多次显示
{
ShowCursor(false); // 隐藏光标
_cursorCount--;
}
}
}
5 changes: 5 additions & 0 deletions src/ComputerLock/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ else
Color="Color.Primary" />
</MudTooltip>

<MudSwitch @bind-Value="@(AppSettings.IsHideMouseCursor)"
@bind-Value:after="SaveSettings"
Label="@(Lang["HideMouseCursor"])"
Color="Color.Primary" />

<div class="d-flex align-center">
<MudSwitch @bind-Value="@(AppSettings.EnablePasswordBox)"
@bind-Value:after="SaveSettings"
Expand Down
9 changes: 9 additions & 0 deletions src/ComputerLock/Resources/Lang.Designer.cs

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

3 changes: 3 additions & 0 deletions src/ComputerLock/Resources/Lang.en.resx
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,7 @@
<data name="SetPasswordFinished" xml:space="preserve">
<value>Next</value>
</data>
<data name="HideMouseCursor" xml:space="preserve">
<value>Hide the mouse cursor</value>
</data>
</root>
3 changes: 3 additions & 0 deletions src/ComputerLock/Resources/Lang.resx
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,7 @@
<data name="SetPasswordFinished" xml:space="preserve">
<value></value>
</data>
<data name="HideMouseCursor" xml:space="preserve">
<value></value>
</data>
</root>
3 changes: 3 additions & 0 deletions src/ComputerLock/Resources/Lang.zh.resx
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,7 @@
<data name="SetPasswordFinished" xml:space="preserve">
<value>完成</value>
</data>
<data name="HideMouseCursor" xml:space="preserve">
<value>锁定时隐藏鼠标光标</value>
</data>
</root>
16 changes: 15 additions & 1 deletion src/ComputerLock/Services/LockService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ internal class LockService
private readonly AppSettings _appSettings;
private readonly ILogger _logger;
private readonly TaskManagerHook _taskManagerHook;
private readonly MouseHook _mouseHook;
public event EventHandler? OnLock;
public event EventHandler? OnUnlock;
public LockService(IServiceProvider serviceProvider, SystemKeyHook systemKeyHook, IStringLocalizer<Lang> lang, AppSettings appSettings, ILogger logger, TaskManagerHook taskManagerHook)
public LockService(IServiceProvider serviceProvider, SystemKeyHook systemKeyHook, IStringLocalizer<Lang> lang, AppSettings appSettings, ILogger logger, TaskManagerHook taskManagerHook, MouseHook mouseHook)
{
_serviceProvider = serviceProvider;
_systemKeyHook = systemKeyHook;
_lang = lang;
_appSettings = appSettings;
_logger = logger;
_taskManagerHook = taskManagerHook;
_mouseHook = mouseHook;

// 防止锁屏时系统崩溃、重启等问题导致任务栏被禁用
// 启动时默认启用一次
Expand Down Expand Up @@ -92,6 +94,11 @@ public void Lock()
_blankScreens.Add(blankScreen);
}

if (_appSettings.IsHideMouseCursor)
{
_logger.Write("锁定服务 -> 隐藏鼠标光标");
_mouseHook.HideCursor();
}
OnLock?.Invoke(this, EventArgs.Empty);
}

Expand All @@ -114,6 +121,13 @@ private void FmLockScreen_OnUnlock(object? sender, EventArgs e)
_logger.Write("锁定服务 -> 解锁动画");
ShowPopup(_lang["UnLocked"]);
}

if (_appSettings.IsHideMouseCursor)
{
_logger.Write("锁定服务 -> 恢复鼠标光标");
_mouseHook.ResetCursorState();
}

_logger.Write("锁定服务 -> 通知解锁");
OnUnlock?.Invoke(this, EventArgs.Empty);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ComputerLock/WindowMain.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
xmlns:ui="http://schemas.modernwpf.com/2019"
mc:Ignorable="d"
Title="透明锁屏"
Height="490"
Height="530"
Width="530"
ResizeMode="NoResize"
WindowState="Normal"
Expand Down

0 comments on commit caab73c

Please sign in to comment.