Skip to content

Commit f38008e

Browse files
committed
Fix dpi for setting form
1 parent 5c5f25c commit f38008e

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

.gitignore

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
/.dotnet_cli/.dotnet
66
/.vs
7-
/bin/Debug/net8.0-windows
8-
/LiteMonitor.Updater/obj
9-
/obj
10-
/bin/Debug
7+
/bin/*
8+
/obj/*
9+
/LiteMonitor.Updater/*

src/UI/Settings/SettingsBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class SettingsPageBase : UserControl, ISettingsPage
2626

2727
public SettingsPageBase()
2828
{
29+
this.AutoScaleMode = AutoScaleMode.None;
2930
this.BackColor = GlobalBackColor;
3031
this.Dock = DockStyle.Fill;
3132
this.DoubleBuffered = true;

src/UI/SettingsForm.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class SettingsForm : Form
1616
private Settings _draftCfg; // Draft Settings
1717
private UIController _ui;
1818
private MainForm _mainForm;
19+
private int _lastDpi = 96;
1920

2021
private FlowLayoutPanel _pnlNavContainer;
2122
private BufferedPanel _pnlContent; // 使用现有的 BufferedPanel
@@ -57,13 +58,17 @@ public SettingsForm(Settings cfg, UIController ui, MainForm mainForm)
5758
_draftCfg = _cfg.DeepClone();
5859

5960
InitializeComponent();
61+
_lastDpi = this.DeviceDpi;
6062

6163
// ★★★ 关键点 1:构造时就初始化所有页面 ★★★
6264
InitPages();
6365
}
6466

6567
private void InitializeComponent()
6668
{
69+
// SettingsForm uses manual UIUtils scaling during construction.
70+
// Disable WinForms auto-rescaling on cross-monitor DPI changes to avoid double scaling.
71+
this.AutoScaleMode = AutoScaleMode.None;
6772
UIUtils.ScaleFactor = this.DeviceDpi / 96f;
6873

6974
this.Size = new Size(UIUtils.S(820), UIUtils.S(680));
@@ -248,5 +253,41 @@ private void ApplySettings()
248253
// 将 Live 环境中由插件生成的最新监控项同步回 Draft,并保留动态显示属性
249254
SettingsChanger.RebaseDraftMonitorItems(_cfg, _draftCfg);
250255
}
256+
257+
protected override void OnDpiChanged(DpiChangedEventArgs e)
258+
{
259+
base.OnDpiChanged(e);
260+
261+
// Settings UI uses manual sizing based on the initial DPI.
262+
// Keep fonts visually stable across monitors to avoid extra whitespace
263+
// when the form moves between 150% and 100% displays.
264+
if (_lastDpi > 0 && e.DeviceDpiNew > 0 && _lastDpi != e.DeviceDpiNew)
265+
{
266+
float ratio = (float)_lastDpi / e.DeviceDpiNew;
267+
RescaleFonts(this, ratio);
268+
foreach (var page in _pages.Values)
269+
{
270+
if (!page.IsDisposed && page.Parent == null)
271+
{
272+
RescaleFonts(page, ratio);
273+
}
274+
}
275+
_lastDpi = e.DeviceDpiNew;
276+
}
277+
}
278+
279+
private static void RescaleFonts(Control root, float ratio)
280+
{
281+
if (root.Font != null)
282+
{
283+
float newSize = Math.Max(1f, root.Font.Size * ratio);
284+
root.Font = new Font(root.Font.FontFamily, newSize, root.Font.Style);
285+
}
286+
287+
foreach (Control child in root.Controls)
288+
{
289+
RescaleFonts(child, ratio);
290+
}
291+
}
251292
}
252-
}
293+
}

src/UI/TaskbarForm.cs

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

60+
ReloadLayout();
61+
6062
_bizHelper.CheckTheme(true);
6163
_bizHelper.FindHandles();
6264

6365
_bizHelper.AttachToTaskbar();
64-
ReloadLayout();
6566
_winHelper.ApplyLayeredStyle(_bizHelper.TransparentKey, _cfg.TaskbarClickThrough);
6667

6768
_timer.Interval = Math.Max(_cfg.RefreshMs, 60);

0 commit comments

Comments
 (0)