fix: 修复初次启动时背景色不一致问题

This commit is contained in:
2026-01-17 13:08:58 +08:00
parent 5fba4ff110
commit aa8467ccce
2 changed files with 83 additions and 10 deletions

View File

@@ -35,18 +35,56 @@ namespace TimerApp
public MainForm()
{
InitializeComponent();
// 在窗口显示前设置背景色,避免白色闪烁
// 确保所有控件在显示前都有正确的背景色
this.SuspendLayout();
// 先加载设置,以便知道应该使用什么背景色
_settings = AppSettings.Load();
bool dark = _settings.IsDarkMode;
Color bg = dark ? _darkBg : _lightBg;
this.BackColor = bg;
pnlSettings.BackColor = bg;
lblTimeLeft.BackColor = bg;
lblStatus.BackColor = bg;
label1.BackColor = bg;
label2.BackColor = bg;
// 设置文本框和按钮的初始背景色
Color panelColor = dark ? _darkPanel : _lightPanel;
txtWork.BackColor = panelColor;
txtRest.BackColor = panelColor;
btnWorkMinus.BackColor = panelColor;
btnWorkPlus.BackColor = panelColor;
btnRestMinus.BackColor = panelColor;
btnRestPlus.BackColor = panelColor;
btnStartStop.BackColor = dark ? Color.FromArgb(63, 63, 70) : Color.White;
btnReset.BackColor = dark ? Color.FromArgb(63, 63, 70) : Color.White;
// 优化绘制,减少闪烁
this.SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.DoubleBuffer |
ControlStyles.ResizeRedraw, true);
this.ResumeLayout(false);
}
private void MainForm_Load(object sender, EventArgs e)
{
// Load settings
_settings = AppSettings.Load();
// Settings already loaded in constructor, just update text
txtWork.Text = _settings.WorkMinutes.ToString();
txtRest.Text = _settings.RestMinutes.ToString();
// Apply Theme
// Apply Theme - 确保在窗口显示前应用主题
ApplyTheme();
// 强制刷新背景色,确保所有控件都正确显示
this.Invalidate(true);
pnlSettings.Invalidate(true);
this.Update();
// Init monitor
_monitor = new ActivityMonitor();
ApplySettings();
@@ -78,6 +116,16 @@ namespace TimerApp
SetupTextBoxPanel(txtWork, pnlSettings);
SetupTextBoxPanel(txtRest, pnlSettings);
// 确保容器背景色正确(在 SetupTextBoxPanel 之后再次应用主题)
UpdateTextBoxStyle(txtWork, _settings.IsDarkMode);
UpdateTextBoxStyle(txtRest, _settings.IsDarkMode);
// 再次确保 pnlSettings 的背景色正确
bool dark = _settings.IsDarkMode;
Color bg = dark ? _darkBg : _lightBg;
pnlSettings.BackColor = bg;
pnlSettings.Refresh();
_monitor.Start();
// Set tray icon
@@ -122,6 +170,7 @@ namespace TimerApp
Panel container = new Panel();
container.Size = txt.Size; // 60x30
container.Location = txt.Location;
// 确保容器背景色与文本框背景色一致
container.BackColor = txt.BackColor;
// Adjust textbox to be centered inside
@@ -136,6 +185,9 @@ namespace TimerApp
// Ensure correct tab order and tagging
container.Tag = txt.Tag; // Copy tag if any
// 确保容器背景色与父容器一致(如果文本框背景色与父容器不同)
// 这里容器应该使用文本框的背景色,因为它是文本框的容器
}
private void ValidateRange(TextBox txt, int min, int max)
@@ -175,9 +227,25 @@ namespace TimerApp
Color text = dark ? _darkText : _lightText;
Color btnBg = dark ? Color.FromArgb(45, 45, 48) : Color.White;
// 确保窗口和所有面板的背景色一致
this.BackColor = bg;
pnlTitle.BackColor = panel;
pnlSettings.BackColor = bg; // Revert to bg color (user preference)
// 强制设置 pnlSettings 的背景色,确保与窗口背景色一致
pnlSettings.BackColor = bg;
pnlSettings.Invalidate(); // 强制重绘
lblTimeLeft.BackColor = bg;
lblStatus.BackColor = bg;
label1.BackColor = bg;
label2.BackColor = bg;
// 确保所有标签都使用正确的背景色
foreach (Control ctrl in pnlSettings.Controls)
{
if (ctrl is Label label && label.BackColor != bg)
{
label.BackColor = bg;
}
}
lblTitle.ForeColor = dark ? Color.LightGray : Color.FromArgb(64, 64, 64);
lblTimeLeft.ForeColor = text;