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

17
MainForm.Designer.cs generated
View File

@@ -91,6 +91,7 @@ namespace TimerApp
//
// btnClose
//
this.btnClose.BackColor = System.Drawing.Color.Transparent;
this.btnClose.Dock = System.Windows.Forms.DockStyle.Right;
this.btnClose.FlatAppearance.BorderSize = 0;
this.btnClose.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Red;
@@ -101,12 +102,13 @@ namespace TimerApp
this.btnClose.Size = new System.Drawing.Size(40, 40);
this.btnClose.TabIndex = 1;
this.btnClose.Text = "✕";
this.btnClose.UseVisualStyleBackColor = true;
this.btnClose.UseVisualStyleBackColor = false;
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// btnTheme
//
this.btnTheme.BackColor = System.Drawing.Color.Transparent;
this.btnTheme.Dock = System.Windows.Forms.DockStyle.Right;
this.btnTheme.FlatAppearance.BorderSize = 0;
this.btnTheme.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
@@ -116,12 +118,13 @@ namespace TimerApp
this.btnTheme.Size = new System.Drawing.Size(40, 40);
this.btnTheme.TabIndex = 3;
this.btnTheme.Text = "☀";
this.btnTheme.UseVisualStyleBackColor = true;
this.btnTheme.UseVisualStyleBackColor = false;
this.btnTheme.Click += new System.EventHandler(this.btnTheme_Click);
//
// btnMin
//
this.btnMin.BackColor = System.Drawing.Color.Transparent;
this.btnMin.Dock = System.Windows.Forms.DockStyle.Right;
this.btnMin.FlatAppearance.BorderSize = 0;
this.btnMin.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
@@ -131,12 +134,13 @@ namespace TimerApp
this.btnMin.Size = new System.Drawing.Size(40, 40);
this.btnMin.TabIndex = 2;
this.btnMin.Text = "―";
this.btnMin.UseVisualStyleBackColor = true;
this.btnMin.UseVisualStyleBackColor = false;
this.btnMin.Click += new System.EventHandler(this.btnMin_Click);
//
// lblTimeLeft
//
this.lblTimeLeft.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30)))));
this.lblTimeLeft.Dock = System.Windows.Forms.DockStyle.Top;
this.lblTimeLeft.Font = new System.Drawing.Font("Segoe UI Light", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.lblTimeLeft.ForeColor = System.Drawing.Color.White;
@@ -150,6 +154,7 @@ namespace TimerApp
//
// lblStatus
//
this.lblStatus.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30)))));
this.lblStatus.Dock = System.Windows.Forms.DockStyle.Top;
this.lblStatus.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.lblStatus.ForeColor = System.Drawing.Color.Gray;
@@ -163,6 +168,7 @@ namespace TimerApp
//
// pnlSettings
//
this.pnlSettings.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30)))));
this.pnlSettings.Controls.Add(this.btnReset);
this.pnlSettings.Controls.Add(this.btnStartStop);
this.pnlSettings.Controls.Add(this.btnRestPlus);
@@ -183,6 +189,7 @@ namespace TimerApp
// label1
//
this.label1.AutoSize = true;
this.label1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30)))));
this.label1.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.label1.ForeColor = System.Drawing.Color.LightGray;
this.label1.Location = new System.Drawing.Point(40, 20);
@@ -238,6 +245,7 @@ namespace TimerApp
// label2
//
this.label2.AutoSize = true;
this.label2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30)))));
this.label2.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.label2.ForeColor = System.Drawing.Color.LightGray;
this.label2.Location = new System.Drawing.Point(40, 60);
@@ -390,9 +398,6 @@ namespace TimerApp
this.Load += new System.EventHandler(this.MainForm_Load);
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);
// Ensure pnlSettings matches form background initially
this.pnlSettings.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30)))));
this.contextMenuStrip1.ResumeLayout(false);
this.pnlTitle.ResumeLayout(false);
this.pnlTitle.PerformLayout();

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;