198 lines
7.5 KiB
C#
198 lines
7.5 KiB
C#
using System;
|
||
using System.Drawing;
|
||
using System.Windows.Forms;
|
||
|
||
namespace TimerApp
|
||
{
|
||
public class RestForm : Form
|
||
{
|
||
private Label lblMessage = null!;
|
||
private Label lblTimer = null!;
|
||
private Button btnSkip = null!;
|
||
|
||
public event EventHandler? SkipRequested;
|
||
|
||
protected override CreateParams CreateParams
|
||
{
|
||
get
|
||
{
|
||
CreateParams cp = base.CreateParams;
|
||
// Turn on WS_EX_TOOLWINDOW style bit (0x80)
|
||
// 这可以防止窗口出现在 Alt-Tab 列表中,并减少任务栏闪烁风险
|
||
cp.ExStyle |= 0x80;
|
||
return cp;
|
||
}
|
||
}
|
||
|
||
public RestForm()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
|
||
private void InitializeComponent()
|
||
{
|
||
this.lblMessage = new System.Windows.Forms.Label();
|
||
this.lblTimer = new System.Windows.Forms.Label();
|
||
// this.btnSkip = new RoundedButton(); // Removed custom button
|
||
this.SuspendLayout();
|
||
|
||
// ... (keep form settings)
|
||
//
|
||
// Form 设置
|
||
//
|
||
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
|
||
this.StartPosition = FormStartPosition.Manual;
|
||
this.Location = new System.Drawing.Point(-32000, -32000); // 初始位置在屏幕外
|
||
this.TopMost = true;
|
||
this.BackColor = System.Drawing.Color.Black;
|
||
this.Opacity = 0; // 初始隐藏,避免闪烁
|
||
this.ShowInTaskbar = false;
|
||
this.Name = "RestForm";
|
||
this.Text = "Rest Now";
|
||
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw, true);
|
||
|
||
//
|
||
// lblMessage
|
||
//
|
||
this.lblMessage.AutoSize = false;
|
||
this.lblMessage.Font = new System.Drawing.Font("Microsoft YaHei UI", 36F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point);
|
||
this.lblMessage.ForeColor = System.Drawing.Color.White;
|
||
this.lblMessage.Location = new System.Drawing.Point(100, 100);
|
||
this.lblMessage.Name = "lblMessage";
|
||
// 初始大小,会在CenterControls中动态调整
|
||
this.lblMessage.Size = new System.Drawing.Size(800, 100);
|
||
this.lblMessage.TabIndex = 0;
|
||
this.lblMessage.Text = "休息一下,看看远方";
|
||
this.lblMessage.TextAlign = ContentAlignment.MiddleCenter;
|
||
|
||
//
|
||
// lblTimer
|
||
//
|
||
this.lblTimer.AutoSize = false;
|
||
this.lblTimer.Font = new System.Drawing.Font("Segoe UI Light", 72F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
|
||
this.lblTimer.ForeColor = System.Drawing.Color.LightGreen;
|
||
this.lblTimer.Location = new System.Drawing.Point(100, 200);
|
||
this.lblTimer.Name = "lblTimer";
|
||
// 初始大小,会在CenterControls中动态调整
|
||
this.lblTimer.Size = new System.Drawing.Size(400, 180);
|
||
this.lblTimer.TabIndex = 1;
|
||
this.lblTimer.Text = "--:--";
|
||
this.lblTimer.TextAlign = ContentAlignment.MiddleCenter;
|
||
|
||
//
|
||
// btnSkip
|
||
//
|
||
this.btnSkip = new System.Windows.Forms.Button();
|
||
this.btnSkip.BackColor = System.Drawing.Color.FromArgb(63, 63, 70);
|
||
this.btnSkip.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
|
||
this.btnSkip.FlatAppearance.BorderSize = 0;
|
||
this.btnSkip.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(80, 80, 90);
|
||
this.btnSkip.Font = new System.Drawing.Font("Microsoft YaHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
|
||
this.btnSkip.ForeColor = System.Drawing.Color.White;
|
||
this.btnSkip.Location = new System.Drawing.Point(100, 400);
|
||
this.btnSkip.Name = "btnSkip";
|
||
this.btnSkip.Size = new System.Drawing.Size(120, 46);
|
||
this.btnSkip.TabIndex = 2;
|
||
this.btnSkip.Text = "跳过";
|
||
this.btnSkip.UseVisualStyleBackColor = false;
|
||
this.btnSkip.Cursor = System.Windows.Forms.Cursors.Hand;
|
||
this.btnSkip.Click += new System.EventHandler(this.btnSkip_Click);
|
||
|
||
// Add controls
|
||
this.Controls.Add(this.btnSkip);
|
||
this.Controls.Add(this.lblMessage);
|
||
this.Controls.Add(this.lblTimer);
|
||
|
||
this.btnSkip.BringToFront();
|
||
|
||
this.Load += new System.EventHandler(this.RestForm_Load);
|
||
this.Resize += new System.EventHandler(this.RestForm_Resize);
|
||
|
||
this.ResumeLayout(false);
|
||
this.PerformLayout();
|
||
}
|
||
|
||
private void RestForm_Load(object? sender, EventArgs e)
|
||
{
|
||
CenterControls();
|
||
}
|
||
|
||
protected override void OnVisibleChanged(EventArgs e)
|
||
{
|
||
base.OnVisibleChanged(e);
|
||
if (this.Visible)
|
||
{
|
||
// 移回屏幕并最大化
|
||
this.Location = new Point(0, 0);
|
||
this.WindowState = FormWindowState.Maximized;
|
||
this.Refresh();
|
||
|
||
// 延迟显示,确保布局调整完成
|
||
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
|
||
t.Interval = 50;
|
||
t.Tick += (s, ev) => {
|
||
t.Stop();
|
||
t.Dispose();
|
||
this.Opacity = 0.90;
|
||
};
|
||
t.Start();
|
||
}
|
||
}
|
||
|
||
private void RestForm_Resize(object? sender, EventArgs e)
|
||
{
|
||
CenterControls();
|
||
}
|
||
|
||
private void CenterControls()
|
||
{
|
||
// 简单的居中计算
|
||
int centerX = this.Width / 2;
|
||
int centerY = this.Height / 2;
|
||
|
||
// Equal spacing logic
|
||
int spacing = 80;
|
||
|
||
// 使用足够大的固定大小,确保文本完整显示
|
||
// 使用屏幕宽度的85%作为最大宽度,确保在不同分辨率下都能完整显示
|
||
int maxWidth = (int)(this.Width * 0.85);
|
||
|
||
// 消息文本:使用足够大的尺寸
|
||
lblMessage.Size = new Size(maxWidth, 120);
|
||
|
||
// 时间文本:使用足够大的尺寸(72pt字体需要较大空间)
|
||
lblTimer.Size = new Size(maxWidth, 200);
|
||
|
||
// Timer Center = centerY
|
||
lblTimer.Location = new Point(centerX - lblTimer.Width / 2, centerY - lblTimer.Height / 2);
|
||
|
||
// Message above Timer
|
||
lblMessage.Location = new Point(centerX - lblMessage.Width / 2, lblTimer.Top - spacing - lblMessage.Height);
|
||
|
||
// Button below Timer
|
||
btnSkip.Location = new Point(centerX - btnSkip.Width / 2, lblTimer.Bottom + spacing);
|
||
}
|
||
|
||
|
||
public void UpdateTime(TimeSpan remaining)
|
||
{
|
||
if (lblTimer.InvokeRequired)
|
||
{
|
||
lblTimer.BeginInvoke(new Action<TimeSpan>(UpdateTime), remaining);
|
||
}
|
||
else
|
||
{
|
||
// 只更新文本,不重新居中,避免闪烁
|
||
// 由于lblTimer已设置固定大小,文本居中显示,不会因为文本变化而改变位置
|
||
lblTimer.Text = $"{remaining.Minutes:D2}:{remaining.Seconds:D2}";
|
||
}
|
||
}
|
||
|
||
private void btnSkip_Click(object? sender, EventArgs e)
|
||
{
|
||
SkipRequested?.Invoke(this, EventArgs.Empty);
|
||
this.Close();
|
||
}
|
||
}
|
||
}
|