fix: 修复初次启动时背景色不一致问题
This commit is contained in:
17
MainForm.Designer.cs
generated
17
MainForm.Designer.cs
generated
@@ -91,6 +91,7 @@ namespace TimerApp
|
|||||||
//
|
//
|
||||||
// btnClose
|
// btnClose
|
||||||
//
|
//
|
||||||
|
this.btnClose.BackColor = System.Drawing.Color.Transparent;
|
||||||
this.btnClose.Dock = System.Windows.Forms.DockStyle.Right;
|
this.btnClose.Dock = System.Windows.Forms.DockStyle.Right;
|
||||||
this.btnClose.FlatAppearance.BorderSize = 0;
|
this.btnClose.FlatAppearance.BorderSize = 0;
|
||||||
this.btnClose.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Red;
|
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.Size = new System.Drawing.Size(40, 40);
|
||||||
this.btnClose.TabIndex = 1;
|
this.btnClose.TabIndex = 1;
|
||||||
this.btnClose.Text = "✕";
|
this.btnClose.Text = "✕";
|
||||||
this.btnClose.UseVisualStyleBackColor = true;
|
this.btnClose.UseVisualStyleBackColor = false;
|
||||||
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
|
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
|
||||||
|
|
||||||
//
|
//
|
||||||
// btnTheme
|
// btnTheme
|
||||||
//
|
//
|
||||||
|
this.btnTheme.BackColor = System.Drawing.Color.Transparent;
|
||||||
this.btnTheme.Dock = System.Windows.Forms.DockStyle.Right;
|
this.btnTheme.Dock = System.Windows.Forms.DockStyle.Right;
|
||||||
this.btnTheme.FlatAppearance.BorderSize = 0;
|
this.btnTheme.FlatAppearance.BorderSize = 0;
|
||||||
this.btnTheme.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
|
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.Size = new System.Drawing.Size(40, 40);
|
||||||
this.btnTheme.TabIndex = 3;
|
this.btnTheme.TabIndex = 3;
|
||||||
this.btnTheme.Text = "☀";
|
this.btnTheme.Text = "☀";
|
||||||
this.btnTheme.UseVisualStyleBackColor = true;
|
this.btnTheme.UseVisualStyleBackColor = false;
|
||||||
this.btnTheme.Click += new System.EventHandler(this.btnTheme_Click);
|
this.btnTheme.Click += new System.EventHandler(this.btnTheme_Click);
|
||||||
|
|
||||||
//
|
//
|
||||||
// btnMin
|
// btnMin
|
||||||
//
|
//
|
||||||
|
this.btnMin.BackColor = System.Drawing.Color.Transparent;
|
||||||
this.btnMin.Dock = System.Windows.Forms.DockStyle.Right;
|
this.btnMin.Dock = System.Windows.Forms.DockStyle.Right;
|
||||||
this.btnMin.FlatAppearance.BorderSize = 0;
|
this.btnMin.FlatAppearance.BorderSize = 0;
|
||||||
this.btnMin.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
|
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.Size = new System.Drawing.Size(40, 40);
|
||||||
this.btnMin.TabIndex = 2;
|
this.btnMin.TabIndex = 2;
|
||||||
this.btnMin.Text = "―";
|
this.btnMin.Text = "―";
|
||||||
this.btnMin.UseVisualStyleBackColor = true;
|
this.btnMin.UseVisualStyleBackColor = false;
|
||||||
this.btnMin.Click += new System.EventHandler(this.btnMin_Click);
|
this.btnMin.Click += new System.EventHandler(this.btnMin_Click);
|
||||||
|
|
||||||
//
|
//
|
||||||
// lblTimeLeft
|
// 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.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.Font = new System.Drawing.Font("Segoe UI Light", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
|
||||||
this.lblTimeLeft.ForeColor = System.Drawing.Color.White;
|
this.lblTimeLeft.ForeColor = System.Drawing.Color.White;
|
||||||
@@ -150,6 +154,7 @@ namespace TimerApp
|
|||||||
//
|
//
|
||||||
// lblStatus
|
// 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.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.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
|
||||||
this.lblStatus.ForeColor = System.Drawing.Color.Gray;
|
this.lblStatus.ForeColor = System.Drawing.Color.Gray;
|
||||||
@@ -163,6 +168,7 @@ namespace TimerApp
|
|||||||
//
|
//
|
||||||
// pnlSettings
|
// 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.btnReset);
|
||||||
this.pnlSettings.Controls.Add(this.btnStartStop);
|
this.pnlSettings.Controls.Add(this.btnStartStop);
|
||||||
this.pnlSettings.Controls.Add(this.btnRestPlus);
|
this.pnlSettings.Controls.Add(this.btnRestPlus);
|
||||||
@@ -183,6 +189,7 @@ namespace TimerApp
|
|||||||
// label1
|
// label1
|
||||||
//
|
//
|
||||||
this.label1.AutoSize = true;
|
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.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.ForeColor = System.Drawing.Color.LightGray;
|
||||||
this.label1.Location = new System.Drawing.Point(40, 20);
|
this.label1.Location = new System.Drawing.Point(40, 20);
|
||||||
@@ -238,6 +245,7 @@ namespace TimerApp
|
|||||||
// label2
|
// label2
|
||||||
//
|
//
|
||||||
this.label2.AutoSize = true;
|
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.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.ForeColor = System.Drawing.Color.LightGray;
|
||||||
this.label2.Location = new System.Drawing.Point(40, 60);
|
this.label2.Location = new System.Drawing.Point(40, 60);
|
||||||
@@ -390,9 +398,6 @@ namespace TimerApp
|
|||||||
this.Load += new System.EventHandler(this.MainForm_Load);
|
this.Load += new System.EventHandler(this.MainForm_Load);
|
||||||
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);
|
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.contextMenuStrip1.ResumeLayout(false);
|
||||||
this.pnlTitle.ResumeLayout(false);
|
this.pnlTitle.ResumeLayout(false);
|
||||||
this.pnlTitle.PerformLayout();
|
this.pnlTitle.PerformLayout();
|
||||||
|
|||||||
76
MainForm.cs
76
MainForm.cs
@@ -35,18 +35,56 @@ namespace TimerApp
|
|||||||
public MainForm()
|
public MainForm()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
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)
|
private void MainForm_Load(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
// Load settings
|
// Settings already loaded in constructor, just update text
|
||||||
_settings = AppSettings.Load();
|
|
||||||
txtWork.Text = _settings.WorkMinutes.ToString();
|
txtWork.Text = _settings.WorkMinutes.ToString();
|
||||||
txtRest.Text = _settings.RestMinutes.ToString();
|
txtRest.Text = _settings.RestMinutes.ToString();
|
||||||
|
|
||||||
// Apply Theme
|
// Apply Theme - 确保在窗口显示前应用主题
|
||||||
ApplyTheme();
|
ApplyTheme();
|
||||||
|
|
||||||
|
// 强制刷新背景色,确保所有控件都正确显示
|
||||||
|
this.Invalidate(true);
|
||||||
|
pnlSettings.Invalidate(true);
|
||||||
|
this.Update();
|
||||||
|
|
||||||
// Init monitor
|
// Init monitor
|
||||||
_monitor = new ActivityMonitor();
|
_monitor = new ActivityMonitor();
|
||||||
ApplySettings();
|
ApplySettings();
|
||||||
@@ -78,6 +116,16 @@ namespace TimerApp
|
|||||||
SetupTextBoxPanel(txtWork, pnlSettings);
|
SetupTextBoxPanel(txtWork, pnlSettings);
|
||||||
SetupTextBoxPanel(txtRest, 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();
|
_monitor.Start();
|
||||||
|
|
||||||
// Set tray icon
|
// Set tray icon
|
||||||
@@ -122,6 +170,7 @@ namespace TimerApp
|
|||||||
Panel container = new Panel();
|
Panel container = new Panel();
|
||||||
container.Size = txt.Size; // 60x30
|
container.Size = txt.Size; // 60x30
|
||||||
container.Location = txt.Location;
|
container.Location = txt.Location;
|
||||||
|
// 确保容器背景色与文本框背景色一致
|
||||||
container.BackColor = txt.BackColor;
|
container.BackColor = txt.BackColor;
|
||||||
|
|
||||||
// Adjust textbox to be centered inside
|
// Adjust textbox to be centered inside
|
||||||
@@ -136,6 +185,9 @@ namespace TimerApp
|
|||||||
|
|
||||||
// Ensure correct tab order and tagging
|
// Ensure correct tab order and tagging
|
||||||
container.Tag = txt.Tag; // Copy tag if any
|
container.Tag = txt.Tag; // Copy tag if any
|
||||||
|
|
||||||
|
// 确保容器背景色与父容器一致(如果文本框背景色与父容器不同)
|
||||||
|
// 这里容器应该使用文本框的背景色,因为它是文本框的容器
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ValidateRange(TextBox txt, int min, int max)
|
private void ValidateRange(TextBox txt, int min, int max)
|
||||||
@@ -175,9 +227,25 @@ namespace TimerApp
|
|||||||
Color text = dark ? _darkText : _lightText;
|
Color text = dark ? _darkText : _lightText;
|
||||||
Color btnBg = dark ? Color.FromArgb(45, 45, 48) : Color.White;
|
Color btnBg = dark ? Color.FromArgb(45, 45, 48) : Color.White;
|
||||||
|
|
||||||
|
// 确保窗口和所有面板的背景色一致
|
||||||
this.BackColor = bg;
|
this.BackColor = bg;
|
||||||
pnlTitle.BackColor = panel;
|
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);
|
lblTitle.ForeColor = dark ? Color.LightGray : Color.FromArgb(64, 64, 64);
|
||||||
lblTimeLeft.ForeColor = text;
|
lblTimeLeft.ForeColor = text;
|
||||||
|
|||||||
Reference in New Issue
Block a user