Skip to content

Commit 5c5f25c

Browse files
committed
Fixed the issue of the secondary screen's taskbar becoming transparent after restarting in mixed DPI multi-screen setups.
1 parent aec8e56 commit 5c5f25c

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
################################################################################
2+
# 此 .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。
3+
################################################################################
4+
5+
/.dotnet_cli/.dotnet
6+
/.vs
7+
/bin/Debug/net8.0-windows
8+
/LiteMonitor.Updater/obj
9+
/obj
10+
/bin/Debug

src/System/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ static void Main()
8484
try
8585
{
8686
// ★★★ 3. 启动应用 ★★★
87+
Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);
8788
ApplicationConfiguration.Initialize();
8889
Application.Run(new MainForm());
8990
}
@@ -148,4 +149,4 @@ static void LogCrash(Exception? ex, string source)
148149
}
149150
}
150151
}
151-
}
152+
}

src/UI/Helpers/TaskbarWinHelper.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ private IntPtr FindSecondaryTaskbar(Screen screen)
215215
if (screen.Bounds.Contains(r.Location) || screen.Bounds.IntersectsWith(r))
216216
return hWnd;
217217
}
218-
return FindWindow("Shell_TrayWnd", null);
218+
return IntPtr.Zero;
219219
}
220220

221221
public Rectangle GetTaskbarRect(IntPtr hTaskbar, string targetDevice)
@@ -264,7 +264,7 @@ public Rectangle GetTaskbarRect(IntPtr hTaskbar, string targetDevice)
264264

265265
if (!isVertical)
266266
{
267-
int dpi = GetTaskbarDpi();
267+
int dpi = GetTaskbarDpi(hTaskbar);
268268
int standardHeight = (int)Math.Round(48.0 * dpi / 96.0);
269269

270270
if (rectPhys.Height > (standardHeight * 0.8))
@@ -312,16 +312,21 @@ public static bool IsCenterAligned()
312312
catch { return false; }
313313
}
314314

315-
public static int GetTaskbarDpi()
315+
public static int GetTaskbarDpi(IntPtr hWnd)
316316
{
317-
IntPtr taskbar = FindWindow("Shell_TrayWnd", null);
318-
if (taskbar != IntPtr.Zero)
317+
if (hWnd != IntPtr.Zero)
319318
{
320-
try { return (int)GetDpiForWindow(taskbar); } catch { }
319+
try { return (int)GetDpiForWindow(hWnd); } catch { }
321320
}
322321
return 96;
323322
}
324323

324+
public static int GetTaskbarDpi()
325+
{
326+
IntPtr taskbar = FindWindow("Shell_TrayWnd", null);
327+
return GetTaskbarDpi(taskbar);
328+
}
329+
325330
public static int GetWidgetsWidth()
326331
{
327332
int dpi = GetTaskbarDpi();

src/UI/TaskbarForm.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,11 @@ public TaskbarForm(Settings cfg, UIController ui, MainForm mainForm)
5757
TopMost = false;
5858
DoubleBuffered = true;
5959

60-
ReloadLayout();
61-
6260
_bizHelper.CheckTheme(true);
6361
_bizHelper.FindHandles();
6462

6563
_bizHelper.AttachToTaskbar();
64+
ReloadLayout();
6665
_winHelper.ApplyLayeredStyle(_bizHelper.TransparentKey, _cfg.TaskbarClickThrough);
6766

6867
_timer.Interval = Math.Max(_cfg.RefreshMs, 60);
@@ -79,7 +78,10 @@ public void ReloadLayout()
7978
{
8079
_layout = new HorizontalLayout(ThemeManager.Current, 300, LayoutMode.Taskbar, _cfg);
8180
_lastLayoutSignature = ""; // 重置签名,强制重算
82-
_winHelper.ApplyLayeredStyle(_bizHelper.TransparentKey, _cfg.TaskbarClickThrough);
81+
if (IsHandleCreated)
82+
{
83+
_winHelper.ApplyLayeredStyle(_bizHelper.TransparentKey, _cfg.TaskbarClickThrough);
84+
}
8385
_bizHelper.CheckTheme(true);
8486

8587
// 更新悬浮窗模式 (支持热切换)

0 commit comments

Comments
 (0)