Skip to content

Commit

Permalink
#19 屏幕缩放比例过大、分辨率不一致时可能导致窗口位置异常
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuLing-zhang committed Jan 14, 2025
1 parent a6c27db commit 12e7411
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
53 changes: 44 additions & 9 deletions src/ComputerLock/Services/LockService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,13 @@ public void Lock()

_logger.Write("锁定服务 -> 准备主屏幕");
_windowLockScreen = _serviceProvider.GetRequiredService<WindowLockScreen>();
_windowLockScreen.Left = primaryScreen.WorkingArea.Left;
_windowLockScreen.Top = primaryScreen.WorkingArea.Top;
_windowLockScreen.OnUnlock += FmLockScreen_OnUnlock;
_windowLockScreen.Closing += (_, _) =>
{
_windowLockScreen.OnUnlock -= FmLockScreen_OnUnlock;
};
_windowLockScreen.Show();
_windowLockScreen.Activate();
ShowWindowOnScreen(_windowLockScreen, primaryScreen);

_logger.Write("锁定服务 -> 激活主屏幕");
for (var i = 0; i <= Screen.AllScreens.Length - 1; i++)
{
Expand All @@ -84,12 +82,9 @@ public void Lock()
_logger.Write($"锁定服务 -> 准备副屏幕{i}");

var blankScreen = _serviceProvider.GetRequiredService<WindowBlankScreen>();
blankScreen.WindowStartupLocation = WindowStartupLocation.Manual;
blankScreen.Left = screen.WorkingArea.Left;
blankScreen.Top = screen.WorkingArea.Top;
blankScreen.OnDeviceInput += BlankScreen_OnDeviceInput;
blankScreen.Show();
blankScreen.Activate();
ShowWindowOnScreen(blankScreen, screen);

_logger.Write("锁定服务 -> 激活副屏幕");
_blankScreens.Add(blankScreen);
}
Expand All @@ -102,6 +97,46 @@ public void Lock()
OnLock?.Invoke(this, EventArgs.Empty);
}

private void ShowWindowOnScreen(Window window, Screen screen)
{
// 获取包括任务栏的完整屏幕区域
var bounds = screen.Bounds;

// 设置窗口初始位置和大小
window.WindowStartupLocation = WindowStartupLocation.Manual;
window.Left = bounds.Left;
window.Top = bounds.Top;
window.Width = bounds.Width;
window.Height = bounds.Height;

// 在窗口加载后,根据屏幕的 DPI 重新调整位置和大小
// 必须先显示窗口,然后才能获取 DPI,所以窗口大小和位置需要二次调整
window.Loaded += (sender, e) =>
{
var dpiFactor = GetDpiFactor(window);
window.Left = bounds.Left / dpiFactor.X;
window.Top = bounds.Top / dpiFactor.Y;
window.Width = bounds.Width / dpiFactor.X;
window.Height = bounds.Height / dpiFactor.Y;
};

window.WindowStyle = WindowStyle.None;
window.ResizeMode = System.Windows.ResizeMode.NoResize;

window.Show();
window.Activate();
}

private (double X, double Y) GetDpiFactor(Window window)
{
var source = PresentationSource.FromVisual(window);
if (source?.CompositionTarget != null)
{
var transform = source.CompositionTarget.TransformToDevice;
return (transform.M11, transform.M22);
}
return (1.0, 1.0); // 默认比例
}
private void FmLockScreen_OnUnlock(object? sender, EventArgs e)
{
_logger.Write("锁定服务 -> 准备解锁");
Expand Down
1 change: 0 additions & 1 deletion src/ComputerLock/WindowBlankScreen.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title=""
WindowStyle="None"
Topmost="True"
ShowInTaskbar="False"
Background="Transparent"
Expand Down
1 change: 0 additions & 1 deletion src/ComputerLock/WindowBlankScreen.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public WindowBlankScreen(AppSettings appSettings, ILogger logger)
private void Window_Loaded(object sender, RoutedEventArgs e)
{
_logger.Write("副屏幕 -> 准备锁定");
WindowState = WindowState.Maximized;
}

private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
Expand Down
2 changes: 0 additions & 2 deletions src/ComputerLock/WindowLockScreen.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title=""
WindowStyle="None"
Topmost="True"
WindowState="Maximized"
Background="Transparent"
AllowsTransparency="True"
ShowInTaskbar="False"
Expand Down

0 comments on commit 12e7411

Please sign in to comment.