1
0
Fork 0
This repository has been archived on 2026-06-16. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
colorclock-screensaver/MainWindow.cs
2023-06-26 18:44:50 +02:00

70 lines
1.8 KiB
C#

using System;
using System.Drawing;
using System.Timers;
using System.Windows.Forms;
namespace App
{
class MainWindow : Window
{
private readonly ClockLabel ClockLabel = new();
private readonly Label ColorLabel = new();
private readonly FlowLayoutPanel Panel= new();
public MainWindow(Rectangle rect) : base(rect)
{
InitializeComponent();
}
private void InitializeComponent()
{
// ClockLabel
ClockLabel.Font = new Font("Microsoft Sans Serif", 100F, FontStyle.Regular, GraphicsUnit.Pixel, ((byte)(0)));
ClockLabel.Size = new Size(450, 120);
ClockLabel.TextAlign = ContentAlignment.MiddleCenter;
Panel.Controls.Add(ClockLabel);
// ColorLabel
ColorLabel.Size = new Size(ClockLabel.Size.Width, 50);
ColorLabel.Font = new Font("Microsoft Sans Serif", 18F, FontStyle.Regular, GraphicsUnit.Pixel, ((byte)(0)));
ColorLabel.TextAlign = ContentAlignment.MiddleCenter;
Panel.Controls.Add(ColorLabel);
// Panel
Panel.FlowDirection = FlowDirection.TopDown;
Panel.AutoSizeMode = AutoSizeMode.GrowAndShrink;
Panel.AutoSize = true;
Controls.Add(Panel);
// Events
BackColorChanged += MainWindow_BackColorChanged;
SizeChanged += MainWindow_SizeChanged;
// Trigger an size changed event.
OnSizeChanged(new EventArgs());
}
private void MainWindow_SizeChanged(object? sender, EventArgs e)
{
// Center panel
Panel.Left = (Bounds.Width - Panel.ClientRectangle.Width) / 2;
Panel.Top = (Bounds.Height - Panel.ClientRectangle.Height) / 3;
}
private void MainWindow_BackColorChanged(object? sender, EventArgs e)
{
// Update Foreground color to contrast the background color.
if (ColorUtil.GetLuminance(BackColor) < 0.5f) {
ForeColor = Color.White;
} else {
ForeColor = Color.Black;
}
ColorLabel.Text = ColorUtil.ToHex(BackColor);
}
}
}