Skip to content

Commit

Permalink
Version 3.16.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaskohl committed Apr 18, 2022
1 parent 633b5e5 commit b88a9f4
Show file tree
Hide file tree
Showing 46 changed files with 3,154 additions and 413 deletions.
12 changes: 12 additions & 0 deletions CapsLockIndicatorV3/AdvancedSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ private void ListView1_ItemActivate(object sender, EventArgs e)
value = AskColor((Color)value);
else if (type == typeof(Font))
value = AskFont((Font)value);
else if (type == typeof(Padding))
value = AskPadding((Padding)value);
else
{
MessageBox.Show("Unsupported type: " + type.FullName, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Expand Down Expand Up @@ -135,6 +137,16 @@ private Font AskFont(Font value)
}
}

private Padding AskPadding(Padding value)
{
using (var d = new PaddingInputDialog(value))
{
if (d.ShowDialog() == DialogResult.OK)
return d.Value;
return value;
}
}

private void AdvancedSettings_DarkModeChanged(object sender, EventArgs e)
{
var dark = DarkModeProvider.IsDark;
Expand Down
298 changes: 252 additions & 46 deletions CapsLockIndicatorV3/BetterCheckBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Reflection;
using System.Text;
Expand All @@ -12,6 +13,9 @@ namespace CapsLockIndicatorV3
{
public class BetterCheckBox : CheckBox
{
const int WIN11_CORNER_RADIUS = 2;

private bool isWindows11 = Versions.IsWindows11;
private bool _darkMode;

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
Expand Down Expand Up @@ -40,6 +44,7 @@ private TextFormatFlags TextRenderingFlags
}
}

// Win10 style colors
private static readonly Color N_Border = Color.FromArgb(137, 137, 137);
private static readonly Color N_Background = Color.FromArgb(0, 0, 0);
private static readonly Color N_Check = Color.FromArgb(222, 222, 222);
Expand All @@ -50,6 +55,18 @@ private TextFormatFlags TextRenderingFlags
private static readonly Color A_Background = Color.FromArgb(0, 9, 16);
private static readonly Color A_Check = Color.FromArgb(120, 130, 187);

// Win11 style colors
private static readonly Color N_U_Background_11 = Color.FromArgb(unchecked((int)0xff383838));
private static readonly Color H_U_Background_11 = Color.FromArgb(unchecked((int)0xff424242));
private static readonly Color A_U_Background_11 = Color.FromArgb(unchecked((int)0xff202020));
private static readonly Color N_C_Background_11 = Color.FromArgb(unchecked((int)0xff8FCBFF));
private static readonly Color H_C_Background_11 = Color.FromArgb(unchecked((int)0xffBCE0FF));
private static readonly Color A_C_Background_11 = Color.FromArgb(unchecked((int)0xff74ABDC));
private static readonly Color N_C_Check_11 = Color.FromArgb(unchecked((int)0xff191919));
private static readonly Color H_C_Check_11 = Color.FromArgb(unchecked((int)0xff202020));
private static readonly Color A_C_Check_11 = Color.FromArgb(unchecked((int)0xff101010));
private static readonly Color Border_11 = Color.FromArgb(unchecked((int)0xff8b8b8b));

public BetterCheckBox() : base()
{
DoubleBuffered = true;
Expand Down Expand Up @@ -90,37 +107,14 @@ protected override void OnPaint(PaintEventArgs e)
}
else
{
var scaling = DPIHelper.GetScalingFactorPercent(Handle);
var border = N_Border;
var background = N_Background;
var check = N_Check;
var fg = ForeColor;

if (!Enabled)
{
border = N_Border.Blend(bgColor, 0.5d);
background = N_Background.Blend(bgColor, 0.5d);
check = N_Check.Blend(bgColor, 0.5d);
fg = ForeColor.Blend(bgColor, 0.5d);
}
else if (Native.GetButtonFlag(this, Native.ButtonFlags.MouseDown))
{
border = A_Border;
background = A_Background;
check = A_Check;
}
else if (Native.GetButtonFlag(this, Native.ButtonFlags.MouseOver))
{
border = H_Border;
background = H_Background;
check = H_Check;
}
Color border, background, check;

var fg = ForeColor;
var scaling = DPIHelper.GetScalingFactorPercent(Handle);
int boxSize = (int)(13 * scaling);
int boxRectInnerMargin = (int)(2 * scaling);
int checkPenWidth = (int)(2 * scaling);
int textMargin = (int)(3 * scaling);

var boxY = Height / 2 - boxSize / 2;

var boxRect = new Rectangle(0, boxY, boxSize, boxSize);
Expand All @@ -133,33 +127,144 @@ protected override void OnPaint(PaintEventArgs e)
boxRectInner.Width -= 2 * boxRectInnerMargin;
boxRectInner.Height -= 2 * boxRectInnerMargin;

var checkPoints = new Point[]
var isChecked = CheckState != CheckState.Unchecked;
var radius = (int)(WIN11_CORNER_RADIUS * scaling);

if (isWindows11)
{
new Point(boxRectInner.X, boxRectInner.Y + boxRectInner.Height / 2),
new Point(boxRectInner.X + boxRectInner.Width / 3, boxRectInner.Bottom - checkPenWidth),
new Point(boxRectInner.Right - (checkPenWidth / 2), boxRectInner.Y)
};
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

e.Graphics.Clear(bgColor);
background = isChecked ? N_C_Background_11 : N_U_Background_11;
check = N_C_Check_11;
border = Border_11;

using (var b = new SolidBrush(background))
e.Graphics.FillRectangle(b, boxRect);
if (!Enabled)
{
border = border.Blend(bgColor, 0.5d);
background = background.Blend(bgColor, 0.5d);
check = check.Blend(bgColor, 0.5d);
fg = ForeColor.Blend(bgColor, 0.5d);
}
else if (Native.GetButtonFlag(this, Native.ButtonFlags.MouseDown))
{
background = isChecked ? A_C_Background_11 : A_U_Background_11;
check = A_C_Check_11;
}
else if (Native.GetButtonFlag(this, Native.ButtonFlags.MouseOver))
{
background = isChecked ? H_C_Background_11 : H_U_Background_11;
check = H_C_Check_11;
}

using (var p = new Pen(border))
e.Graphics.DrawRectangle(p, boxRectPen);
var innerBoxSize = (int)Math.Ceiling(boxRect.Width / 2d);

if (CheckState == CheckState.Indeterminate)
{
using (var b = new SolidBrush(check))
e.Graphics.FillRectangle(b, boxRectInner);
boxRectInner = new Rectangle(
boxRect.X + innerBoxSize / 2,
boxRect.Y + innerBoxSize / 2,
innerBoxSize,
innerBoxSize
);
var boxRectFPen = new RectangleF(
boxRect.X + 0.5f,
boxRect.Y + 0.5f,
boxRect.Width - 1.0f,
boxRect.Height - 1.0f
);
//checkPenWidth = (int)scaling / 2;

var checkPoints = new Point[]
{
new Point(boxRectInner.X, boxRectInner.Y + boxRectInner.Height / 2),
new Point(boxRectInner.X + boxRectInner.Width / 3, boxRectInner.Bottom - checkPenWidth),
new Point(boxRectInner.Right - (checkPenWidth / 2), boxRectInner.Y + (boxRectInner.Height / 4))
};

e.Graphics.Clear(bgColor);

using (var b = new SolidBrush(background))
FillRoundedRectangle(e.Graphics, b, boxRect, radius);

if (!isChecked)
using (var p = new Pen(border) { Alignment = PenAlignment.Center })
DrawRoundedRectangle(e.Graphics, p, boxRectFPen, radius);

if (CheckState == CheckState.Indeterminate)
{
//using (var b = new SolidBrush(check))
// FillRoundedRectangle(e.Graphics, b, boxRectInner, WIN11_CORNER_RADIUS);
using (var b = new SolidBrush(check))
e.Graphics.FillRectangle(b, new Rectangle(
boxRectInner.X,
boxRectInner.Y + (boxRectInner.Height - checkPenWidth) / 2,
boxRectInner.Width,
checkPenWidth
));
}
else if (CheckState == CheckState.Checked)
{
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var p = new Pen(check, checkPenWidth))
e.Graphics.DrawLines(p, checkPoints);
}
}
else if (CheckState == CheckState.Checked)
else
{
var _m = e.Graphics.SmoothingMode;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
using (var p = new Pen(check, checkPenWidth))
e.Graphics.DrawLines(p, checkPoints);
e.Graphics.SmoothingMode = _m;
border = N_Border;
background = isWindows11 ? N_U_Background_11 : N_Background;
check = isWindows11 ? N_C_Check_11 : N_Check;

if (!Enabled)
{
border = background.Blend(N_Border, 0.5d);
background = background.Blend(N_Background, 0.5d);
check = check.Blend(N_Check, 0.5d);
fg = ForeColor.Blend(bgColor, 0.5d);
}
else if (Native.GetButtonFlag(this, Native.ButtonFlags.MouseDown))
{
border = A_Border;
background = A_Background;
check = A_Check;
}
else if (Native.GetButtonFlag(this, Native.ButtonFlags.MouseOver))
{
border = H_Border;
background = H_Background;
check = H_Check;
}

var checkPoints = new Point[]
{
new Point(boxRectInner.X, boxRectInner.Y + boxRectInner.Height / 2),
new Point(boxRectInner.X + boxRectInner.Width / 3, boxRectInner.Bottom - checkPenWidth),
new Point(boxRectInner.Right - (checkPenWidth / 2), boxRectInner.Y)
};

e.Graphics.Clear(bgColor);

using (var b = new SolidBrush(background))
e.Graphics.FillRectangle(b, boxRect);

using (var p = new Pen(border))
e.Graphics.DrawRectangle(p, boxRectPen);

if (CheckState == CheckState.Indeterminate)
{
using (var b = new SolidBrush(check))
e.Graphics.FillRectangle(b, boxRectInner);
}
else if (CheckState == CheckState.Checked)
{
var _m = e.Graphics.SmoothingMode;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
using (var p = new Pen(check, checkPenWidth))
e.Graphics.DrawLines(p, checkPoints);
e.Graphics.SmoothingMode = _m;
}
}

var textSz = TextRenderer.MeasureText(e.Graphics, Text, Font);
Expand All @@ -176,5 +281,106 @@ protected override void OnPaint(PaintEventArgs e)
}
}
}
public static GraphicsPath RoundedRect(Rectangle bounds, int radius)
{
int diameter = radius * 2;
Size size = new Size(diameter, diameter);
Rectangle arc = new Rectangle(bounds.Location, size);
GraphicsPath path = new GraphicsPath();

if (radius == 0)
{
path.AddRectangle(bounds);
return path;
}

// top left arc
path.AddArc(arc, 180, 90);

// top right arc
arc.X = bounds.Right - diameter;
path.AddArc(arc, 270, 90);

// bottom right arc
arc.Y = bounds.Bottom - diameter;
path.AddArc(arc, 0, 90);

// bottom left arc
arc.X = bounds.Left;
path.AddArc(arc, 90, 90);

path.CloseFigure();
return path;
}
public static GraphicsPath RoundedRect(RectangleF bounds, int radius)
{
int diameter = radius * 2;
Size size = new Size(diameter, diameter);
RectangleF arc = new RectangleF(bounds.Location, size);
GraphicsPath path = new GraphicsPath();

if (radius == 0)
{
path.AddRectangle(bounds);
return path;
}

// top left arc
path.AddArc(arc, 180, 90);

// top right arc
arc.X = bounds.Right - diameter;
path.AddArc(arc, 270, 90);

// bottom right arc
arc.Y = bounds.Bottom - diameter;
path.AddArc(arc, 0, 90);

// bottom left arc
arc.X = bounds.Left;
path.AddArc(arc, 90, 90);

path.CloseFigure();
return path;
}

public static void DrawRoundedRectangle(Graphics graphics, Pen pen, Rectangle bounds, int cornerRadius)
{
if (graphics == null)
throw new ArgumentNullException("graphics");
if (pen == null)
throw new ArgumentNullException("pen");

using (GraphicsPath path = RoundedRect(bounds, cornerRadius))
{
graphics.DrawPath(pen, path);
}
}

public static void DrawRoundedRectangle(Graphics graphics, Pen pen, RectangleF bounds, int cornerRadius)
{
if (graphics == null)
throw new ArgumentNullException("graphics");
if (pen == null)
throw new ArgumentNullException("pen");

using (GraphicsPath path = RoundedRect(bounds, cornerRadius))
{
graphics.DrawPath(pen, path);
}
}

public static void FillRoundedRectangle(Graphics graphics, Brush brush, Rectangle bounds, int cornerRadius)
{
if (graphics == null)
throw new ArgumentNullException("graphics");
if (brush == null)
throw new ArgumentNullException("brush");

using (GraphicsPath path = RoundedRect(bounds, cornerRadius))
{
graphics.FillPath(brush, path);
}
}
}
}
Loading

0 comments on commit b88a9f4

Please sign in to comment.