From c9548af0081ac7cb849c95318ac5832a52a96b77 Mon Sep 17 00:00:00 2001
From: CJK_mkp <113243675+CJKmkp@users.noreply.github.com>
Date: Tue, 17 Jun 2025 18:52:34 +0800
Subject: [PATCH] fix:issue #3
---
Ink Canvas/Helpers/ForegroundWindowInfo.cs | 74 +++++++++++++++++++
.../MainWindow_cs/MW_FloatingBarIcons.cs | 12 +--
2 files changed, 80 insertions(+), 6 deletions(-)
diff --git a/Ink Canvas/Helpers/ForegroundWindowInfo.cs b/Ink Canvas/Helpers/ForegroundWindowInfo.cs
index 7968fc49..3a5312af 100644
--- a/Ink Canvas/Helpers/ForegroundWindowInfo.cs
+++ b/Ink Canvas/Helpers/ForegroundWindowInfo.cs
@@ -23,6 +23,26 @@ namespace Ink_Canvas.Helpers
[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
+ [DllImport("shell32.dll")]
+ private static extern IntPtr SHAppBarMessage(uint dwMessage, ref APPBARDATA pData);
+
+ [DllImport("user32.dll")]
+ private static extern int SystemParametersInfo(uint uiAction, uint uiParam, IntPtr pvParam, uint fWinIni);
+
+ private const uint ABM_GETTASKBARPOS = 0x00000005;
+ private const int SPI_GETWORKAREA = 0x0030;
+
+ [StructLayout(LayoutKind.Sequential)]
+ private struct APPBARDATA
+ {
+ public int cbSize;
+ public IntPtr hWnd;
+ public uint uCallbackMessage;
+ public uint uEdge;
+ public RECT rc;
+ public IntPtr lParam;
+ }
+
[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public int Left;
@@ -34,6 +54,60 @@ namespace Ink_Canvas.Helpers
public int Height => Bottom - Top;
}
+ ///
+ /// 获取Windows任务栏的高度(仅计算任务栏,不包括其他应用的停靠栏)
+ ///
+ /// 当前屏幕
+ /// DPI缩放Y值
+ /// 任务栏高度
+ public static double GetTaskbarHeight(System.Windows.Forms.Screen screen, double dpiScaleY)
+ {
+ try
+ {
+ // 创建APPBARDATA结构
+ var abd = new APPBARDATA();
+ abd.cbSize = Marshal.SizeOf(abd);
+
+ // 获取任务栏信息
+ IntPtr result = SHAppBarMessage(ABM_GETTASKBARPOS, ref abd);
+ if (result != IntPtr.Zero)
+ {
+ // 获取当前屏幕的工作区
+ RECT workArea = new RECT();
+ SystemParametersInfo(SPI_GETWORKAREA, 0, Marshal.AllocHGlobal(Marshal.SizeOf(workArea)), 0);
+
+ // 根据任务栏位置计算高度
+ int taskbarHeight = 0;
+
+ // 任务栏的uEdge: 0=左, 1=上, 2=右, 3=下
+ switch (abd.uEdge)
+ {
+ case 1: // 上
+ taskbarHeight = abd.rc.Height;
+ break;
+ case 3: // 下
+ taskbarHeight = abd.rc.Height;
+ break;
+ case 0: // 左
+ case 2: // 右
+ // 水平任务栏不影响高度
+ taskbarHeight = 0;
+ break;
+ }
+
+ // 考虑DPI缩放
+ return taskbarHeight / dpiScaleY;
+ }
+ }
+ catch (Exception ex)
+ {
+ Debug.WriteLine($"获取任务栏高度出错: {ex.Message}");
+ }
+
+ // 如果获取失败,回退到通用方法
+ return (screen.Bounds.Height - screen.WorkingArea.Height) / dpiScaleY;
+ }
+
public static string WindowTitle() {
IntPtr foregroundWindowHandle = GetForegroundWindow();
diff --git a/Ink Canvas/MainWindow_cs/MW_FloatingBarIcons.cs b/Ink Canvas/MainWindow_cs/MW_FloatingBarIcons.cs
index 9bae70b4..9f8ebffc 100644
--- a/Ink Canvas/MainWindow_cs/MW_FloatingBarIcons.cs
+++ b/Ink Canvas/MainWindow_cs/MW_FloatingBarIcons.cs
@@ -1078,8 +1078,8 @@ namespace Ink_Canvas {
var windowHandle = new WindowInteropHelper(this).Handle;
var screen = System.Windows.Forms.Screen.FromHandle(windowHandle);
double screenWidth = screen.Bounds.Width / dpiScaleX, screenHeight = screen.Bounds.Height / dpiScaleY;
- var toolbarHeight = SystemParameters.PrimaryScreenHeight - SystemParameters.FullPrimaryScreenHeight -
- SystemParameters.WindowCaptionHeight;
+ // 仅计算Windows任务栏高度,不考虑其他程序对工作区的影响
+ var toolbarHeight = ForegroundWindowInfo.GetTaskbarHeight(screen, dpiScaleY);
pos.X = (screenWidth - ViewboxFloatingBar.ActualWidth * ViewboxFloatingBarScaleTransform.ScaleX) / 2;
if (PosXCaculatedWithTaskbarHeight == false)
@@ -1141,8 +1141,8 @@ namespace Ink_Canvas {
var windowHandle = new WindowInteropHelper(this).Handle;
var screen = System.Windows.Forms.Screen.FromHandle(windowHandle);
double screenWidth = screen.Bounds.Width / dpiScaleX, screenHeight = screen.Bounds.Height / dpiScaleY;
- var toolbarHeight = SystemParameters.PrimaryScreenHeight - SystemParameters.FullPrimaryScreenHeight -
- SystemParameters.WindowCaptionHeight;
+ // 仅计算Windows任务栏高度,不考虑其他程序对工作区的影响
+ var toolbarHeight = ForegroundWindowInfo.GetTaskbarHeight(screen, dpiScaleY);
pos.X = (screenWidth - ViewboxFloatingBar.ActualWidth * ViewboxFloatingBarScaleTransform.ScaleX) / 2;
pos.Y = screenHeight - ViewboxFloatingBar.ActualHeight * ViewboxFloatingBarScaleTransform.ScaleY -
@@ -1184,8 +1184,8 @@ namespace Ink_Canvas {
var windowHandle = new WindowInteropHelper(this).Handle;
var screen = System.Windows.Forms.Screen.FromHandle(windowHandle);
double screenWidth = screen.Bounds.Width / dpiScaleX, screenHeight = screen.Bounds.Height / dpiScaleY;
- var toolbarHeight = SystemParameters.PrimaryScreenHeight - SystemParameters.FullPrimaryScreenHeight -
- SystemParameters.WindowCaptionHeight;
+ // 仅计算Windows任务栏高度,不考虑其他程序对工作区的影响
+ var toolbarHeight = ForegroundWindowInfo.GetTaskbarHeight(screen, dpiScaleY);
pos.X = (screenWidth - ViewboxFloatingBar.ActualWidth * ViewboxFloatingBarScaleTransform.ScaleX) / 2;
pos.Y = screenHeight - 55 * ViewboxFloatingBarScaleTransform.ScaleY;