🧀 Checkbox 新增 CheckState

This commit is contained in:
Tom
2026-01-16 16:27:10 +08:00
parent 0acba563df
commit 05be95d1f0
6 changed files with 163 additions and 49 deletions

View File

@@ -104,6 +104,8 @@ namespace AntdUI
}
}
#region
bool AnimationCheck = false;
float AnimationCheckValue = 0;
bool _checked = false;
@@ -118,8 +120,49 @@ namespace AntdUI
{
if (_checked == value) return;
_checked = value;
ThreadCheck?.Dispose();
if (IsHandleCreated && Config.HasAnimation(nameof(Checkbox), Name))
OnCheck();
CheckState = value ? CheckState.Checked : CheckState.Unchecked;
OnCheckedChanged(value);
OnPropertyChanged(nameof(Checked));
}
}
internal CheckState checkStateOld = CheckState.Unchecked;
CheckState checkState = CheckState.Unchecked;
/// <summary>
/// 选中状态
/// </summary>
[Description("选中状态"), Category("行为"), DefaultValue(CheckState.Unchecked)]
public CheckState CheckState
{
get => checkState;
set
{
if (checkState == value) return;
checkState = value;
bool __checked = value == CheckState.Checked;
if (_checked == __checked) Invalidate();
else
{
_checked = __checked;
OnCheck();
}
if (value != CheckState.Unchecked) checkStateOld = value;
OnPropertyChanged(nameof(CheckState));
}
}
void OnCheck()
{
ThreadCheck?.Dispose();
if (IsHandleCreated && Config.HasAnimation(nameof(Checkbox), Name))
{
if (!_checked && checkStateOld == CheckState.Checked && CheckState == CheckState.Indeterminate)
{
AnimationCheckValue = 1F;
Invalidate();
}
else
{
AnimationCheck = true;
ThreadCheck = new AnimationTask(new AnimationLinearFConfig(this, i =>
@@ -127,15 +170,18 @@ namespace AntdUI
AnimationCheckValue = i;
Invalidate();
return true;
}, 20).SetValue(AnimationCheckValue, value, 0.2F).SetEnd(() => AnimationCheck = false));
}, 20).SetValue(AnimationCheckValue, _checked, 0.2F).SetEnd(() => AnimationCheck = false));
}
else AnimationCheckValue = value ? 1F : 0F;
}
else
{
AnimationCheckValue = _checked ? 1F : 0F;
Invalidate();
OnCheckedChanged(value);
OnPropertyChanged(nameof(Checked));
}
}
#endregion
/// <summary>
/// 点击时自动改变选中状态
/// </summary>
@@ -268,21 +314,35 @@ namespace AntdUI
var color = fill ?? Colour.Primary.Get(nameof(Checkbox), ColorScheme);
if (AnimationCheck)
{
float dot = dot_size * 0.3F, alpha = 255 * AnimationCheckValue;
g.Fill(Helper.ToColor(alpha, color), path);
using (var pen = new Pen(Helper.ToColor(alpha, Colour.BgBase.Get(nameof(Checkbox), ColorScheme)), 2.6F * Dpi))
var alpha = 255 * AnimationCheckValue;
if (checkState == CheckState.Indeterminate || (checkStateOld == CheckState.Indeterminate && !_checked))
{
g.DrawLines(pen, icon_rect.CheckArrow());
g.Draw(Colour.BorderColor.Get(nameof(Checkbox), ColorScheme), bor2, path);
g.Fill(Helper.ToColor(alpha, Colour.Primary.Get(nameof(Checkbox), ColorScheme)), PaintBlock(icon_rect));
}
if (_checked)
else
{
float max = icon_rect.Height + ((rect.Height - icon_rect.Height) * AnimationCheckValue), alpha2 = 100 * (1F - AnimationCheckValue);
using (var brush = new SolidBrush(Helper.ToColor(alpha2, color)))
var dot = dot_size * 0.3F;
g.Fill(Helper.ToColor(alpha, color), path);
using (var pen = new Pen(Helper.ToColor(alpha, Colour.BgBase.Get(nameof(Checkbox), ColorScheme)), 2.6F * Dpi))
{
g.FillEllipse(brush, new RectangleF(icon_rect.X + (icon_rect.Width - max) / 2F, icon_rect.Y + (icon_rect.Height - max) / 2F, max, max));
g.DrawLines(pen, icon_rect.CheckArrow());
}
if (_checked)
{
float max = icon_rect.Height + ((rect.Height - icon_rect.Height) * AnimationCheckValue), alpha2 = 100 * (1F - AnimationCheckValue);
using (var brush = new SolidBrush(Helper.ToColor(alpha2, color)))
{
g.FillEllipse(brush, new RectangleF(icon_rect.X + (icon_rect.Width - max) / 2F, icon_rect.Y + (icon_rect.Height - max) / 2F, max, max));
}
}
g.Draw(color, bor2, path);
}
g.Draw(color, bor2, path);
}
else if (checkState == CheckState.Indeterminate)
{
g.Draw(Colour.BorderColor.Get(nameof(Checkbox), ColorScheme), bor2, path);
g.Fill(Colour.Primary.Get(nameof(Checkbox), ColorScheme), PaintBlock(icon_rect));
}
else if (_checked)
{
@@ -305,6 +365,12 @@ namespace AntdUI
}
}
internal static RectangleF PaintBlock(RectangleF rect)
{
float size = rect.Height * 0.2F, size2 = size * 2F;
return new RectangleF(rect.X + size, rect.Y + size, rect.Width - size2, rect.Height - size2);
}
#endregion
#endregion

View File

@@ -39,21 +39,35 @@ namespace AntdUI
var color = fill ?? Colour.Primary.Get(nameof(Checkbox), ColorScheme);
if (AnimationCheck)
{
float dot = dot_size * 0.3F, alpha = 255 * AnimationCheckValue;
g.Fill(Helper.ToColor(alpha, color), path);
using (var pen = new Pen(Helper.ToColor(alpha, Colour.BgBase.Get(nameof(Checkbox), ColorScheme)), 2.6F * Dpi))
var alpha = 255 * AnimationCheckValue;
if (checkState == System.Windows.Forms.CheckState.Indeterminate || (checkStateOld == System.Windows.Forms.CheckState.Indeterminate && !_checked))
{
g.DrawLines(pen, icon_rect.CheckArrow());
g.Draw(Colour.BorderColor.Get(nameof(Checkbox), ColorScheme), bor2, path);
g.Fill(Helper.ToColor(alpha, Colour.Primary.Get(nameof(Checkbox), ColorScheme)), Checkbox.PaintBlock(icon_rect));
}
if (_checked)
else
{
float max = icon_rect.Height + ((rect.Height - icon_rect.Height) * AnimationCheckValue), alpha2 = 100 * (1F - AnimationCheckValue);
using (var brush = new SolidBrush(Helper.ToColor(alpha2, color)))
var dot = dot_size * 0.3F;
g.Fill(Helper.ToColor(alpha, color), path);
using (var pen = new Pen(Helper.ToColor(alpha, Colour.BgBase.Get(nameof(Checkbox), ColorScheme)), 2.6F * Dpi))
{
g.FillEllipse(brush, new RectangleF(icon_rect.X + (icon_rect.Width - max) / 2F, icon_rect.Y + (icon_rect.Height - max) / 2F, max, max));
g.DrawLines(pen, icon_rect.CheckArrow());
}
if (_checked)
{
float max = icon_rect.Height + ((rect.Height - icon_rect.Height) * AnimationCheckValue), alpha2 = 100 * (1F - AnimationCheckValue);
using (var brush = new SolidBrush(Helper.ToColor(alpha2, color)))
{
g.FillEllipse(brush, new RectangleF(icon_rect.X + (icon_rect.Width - max) / 2F, icon_rect.Y + (icon_rect.Height - max) / 2F, max, max));
}
}
g.Draw(color, bor2, path);
}
g.Draw(color, bor2, path);
}
else if (checkState == System.Windows.Forms.CheckState.Indeterminate)
{
g.Draw(Colour.BorderColor.Get(nameof(Checkbox), ColorScheme), bor2, path);
g.Fill(Colour.Primary.Get(nameof(Checkbox), ColorScheme), Checkbox.PaintBlock(icon_rect));
}
else if (_checked)
{

View File

@@ -6,6 +6,7 @@
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace AntdUI
{
@@ -136,6 +137,8 @@ namespace AntdUI
}
}
#region
bool AnimationCheck = false;
float AnimationCheckValue = 0;
bool _checked = false;
@@ -149,10 +152,48 @@ namespace AntdUI
{
if (_checked == value) return;
_checked = value;
ThreadCheck?.Dispose();
try
OnCheck();
CheckState = value ? CheckState.Checked : CheckState.Unchecked;
CheckedChanged?.Invoke(this, new BoolEventArgs(value));
OnPropertyChanged();
}
}
internal CheckState checkStateOld = CheckState.Unchecked;
CheckState checkState = CheckState.Unchecked;
/// <summary>
/// 选中状态
/// </summary>
public CheckState CheckState
{
get => checkState;
set
{
if (checkState == value) return;
checkState = value;
bool __checked = value == CheckState.Checked;
if (_checked != __checked)
{
if (PARENT.PARENT.IsHandleCreated && Config.HasAnimation(nameof(Table)))
_checked = __checked;
OnCheck();
}
if (value != CheckState.Unchecked) checkStateOld = value;
}
}
void OnCheck()
{
ThreadCheck?.Dispose();
try
{
if (PARENT.PARENT.IsHandleCreated && Config.HasAnimation(nameof(Table)))
{
if (!_checked && checkStateOld == CheckState.Checked && CheckState == CheckState.Indeterminate)
{
AnimationCheckValue = 1F;
OnPropertyChanged();
}
else
{
AnimationCheck = true;
ThreadCheck = new AnimationTask(new AnimationLinearFConfig(PARENT.PARENT, i =>
@@ -160,16 +201,15 @@ namespace AntdUI
AnimationCheckValue = i;
OnPropertyChanged();
return true;
}, 20).SetValue(AnimationCheckValue, value, 0.2F).SetEnd(() => AnimationCheck = false));
}, 20).SetValue(AnimationCheckValue, _checked, 0.2F).SetEnd(() => AnimationCheck = false));
}
else AnimationCheckValue = value ? 1F : 0F;
}
catch { }
CheckedChanged?.Invoke(this, new BoolEventArgs(value));
OnPropertyChanged();
else AnimationCheckValue = _checked ? 1F : 0F;
}
catch { }
}
#endregion
AnimationTask? ThreadCheck, ThreadHover;
@@ -250,6 +290,11 @@ namespace AntdUI
_checked = value;
return this;
}
public CellCheckbox SetChecked(CheckState value)
{
checkState = value;
return this;
}
public CellCheckbox SetEnabled(bool value = false)
{
enabled = value;

View File

@@ -1299,11 +1299,6 @@ namespace AntdUI
#region /
internal static RectangleF PaintBlock(RectangleF rect)
{
float size = rect.Height * 0.2F, size2 = size * 2F;
return new RectangleF(rect.X + size, rect.Y + size, rect.Width - size2, rect.Height - size2);
}
internal static PointF[] PaintArrow(RectangleF rect)
{
float size = rect.Height * 0.15F, size2 = rect.Height * 0.2F, size3 = rect.Height * 0.26F;

View File

@@ -1142,7 +1142,7 @@ namespace AntdUI
if (columnCheck.CheckState == CheckState.Indeterminate || (columnCheck.checkStateOld == CheckState.Indeterminate && !columnCheck.Checked))
{
g.Draw(Colour.BorderColor.Get(nameof(Checkbox), colorScheme), PARENT.check_border, path_check);
g.Fill(Helper.ToColor(alpha, Colour.Primary.Get(nameof(Checkbox), colorScheme)), PaintBlock(RECT_REAL));
g.Fill(Helper.ToColor(alpha, Colour.Primary.Get(nameof(Checkbox), colorScheme)), Checkbox.PaintBlock(RECT_REAL));
}
else
{
@@ -1166,7 +1166,7 @@ namespace AntdUI
{
g.Fill(Colour.BgBase.Get(nameof(Checkbox), colorScheme), path_check);
g.Draw(Colour.BorderColor.Get(nameof(Checkbox), colorScheme), PARENT.check_border, path_check);
g.Fill(Colour.Primary.Get(nameof(Checkbox), colorScheme), PaintBlock(RECT_REAL));
g.Fill(Colour.Primary.Get(nameof(Checkbox), colorScheme), Checkbox.PaintBlock(RECT_REAL));
}
else if (columnCheck.Checked)
{

View File

@@ -625,7 +625,7 @@ namespace AntdUI
if (item.CheckState == CheckState.Indeterminate || (item.checkStateOld == CheckState.Indeterminate && !item.Checked))
{
g.Draw(Colour.BorderColor.Get(nameof(Tree), ColorScheme), bor2, path_check);
g.Fill(Helper.ToColor(alpha, Colour.Primary.Get(nameof(Tree), ColorScheme)), PaintBlock(item.check_rect));
g.Fill(Helper.ToColor(alpha, Colour.Primary.Get(nameof(Tree), ColorScheme)), Checkbox.PaintBlock(item.check_rect));
}
else
{
@@ -648,7 +648,7 @@ namespace AntdUI
else if (item.CheckState == CheckState.Indeterminate)
{
g.Draw(Colour.BorderColor.Get(nameof(Tree), ColorScheme), bor2, path_check);
g.Fill(Colour.Primary.Get(nameof(Tree), ColorScheme), PaintBlock(item.check_rect));
g.Fill(Colour.Primary.Get(nameof(Tree), ColorScheme), Checkbox.PaintBlock(item.check_rect));
}
else if (item.Checked)
{
@@ -660,7 +660,7 @@ namespace AntdUI
else
{
g.Fill(Colour.FillQuaternary.Get(nameof(Tree), "bgDisabled", ColorScheme), path_check);
if (item.CheckState == CheckState.Indeterminate) g.Fill(Colour.TextQuaternary.Get(nameof(Tree), "foreDisabled", ColorScheme), PaintBlock(item.check_rect));
if (item.CheckState == CheckState.Indeterminate) g.Fill(Colour.TextQuaternary.Get(nameof(Tree), "foreDisabled", ColorScheme), Checkbox.PaintBlock(item.check_rect));
else if (item.Checked) g.DrawLines(Colour.TextQuaternary.Get(nameof(Tree), "foreDisabled", ColorScheme), bor2, PaintArrow(item.check_rect));
g.Draw(Colour.BorderColorDisable.Get(nameof(Tree), "borderColorDisabled", ColorScheme), bor2, path_check);
}
@@ -703,11 +703,6 @@ namespace AntdUI
}
internal RectangleF PaintBlock(RectangleF rect)
{
float size = rect.Height * 0.2F, size2 = size * 2F;
return new RectangleF(rect.X + size, rect.Y + size, rect.Width - size2, rect.Height - size2);
}
internal PointF[] PaintArrow(RectangleF rect)
{
float size = rect.Height * 0.15F, size2 = rect.Height * 0.2F, size3 = rect.Height * 0.26F;
@@ -1988,15 +1983,14 @@ namespace AntdUI
ThreadCheck = null;
if (PARENT != null && PARENT.IsHandleCreated && (ParentItem == null || ParentItem.expand) && show && Config.HasAnimation(nameof(Tree)))
{
AnimationCheck = true;
if (!_checked && checkStateOld == CheckState.Checked && CheckState == CheckState.Indeterminate)
{
AnimationCheck = false;
AnimationCheckValue = 1F;
Invalidate();
}
else
{
AnimationCheck = true;
ThreadCheck = new AnimationTask(new AnimationLinearFConfig(PARENT, i =>
{
AnimationCheckValue = i;