SVN-Revision: r864
This commit is contained in:
tdx
2025-07-17 08:05:01 +00:00
parent 01630a951c
commit 1e81d565c6
2 changed files with 87 additions and 178 deletions
+1 -36
View File
@@ -1,36 +1 @@
namespace Lskj.Control namespace Lskj.Cont
{
partial class CircleButton
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}
+81 -137
View File
@@ -1,179 +1,123 @@
using System; /***************************************************
using System.Collections.Generic; * CircleButton 2.0 · Red badge with built-in number
* Author : Gong Yuchao (2017-08-16), rev 2025-07-17
***************************************************/
using System;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing; using System.Drawing;
using Lskj.Business.Impl; using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace Lskj.Control namespace Lskj.Control
{ {
[DefaultEvent(nameof(Click))]
public partial class CircleButton : Button public partial class CircleButton : Button
{ {
private int radius;//半径 /* ───────── 可调属性 ───────── */
//圆形按钮的半径属性 private int _radius = 18 / 2; // 直径 18px
[CategoryAttribute("布局"), BrowsableAttribute(true), ReadOnlyAttribute(false)] [Category("布局"), Description("圆点半径(px)")]
public int Radius public int Radius
{ {
set get => _radius;
{ set { _radius = Math.Max(4, value); SyncSize(); }
radius = value;
this.Height = this.Width = Radius;
}
get
{
return radius;
}
} }
private Image imageEnter; private int _number;
[CategoryAttribute("外观"), BrowsableAttribute(true), ReadOnlyAttribute(false)] [Category("数据"), Description("角标数字(>99 显示 99)")]
public Image ImageEnter public int Number
{ {
set get => _number;
{ set { _number = Math.Max(0, value); Invalidate(); }
imageEnter = value;
}
get
{
return imageEnter;
}
} }
private Image imageNormal; [Category("外观"), Description("普通状态图(可省略)")]
[CategoryAttribute("外观"), BrowsableAttribute(true), ReadOnlyAttribute(false)]
public Image ImageNormal public Image ImageNormal
{ {
set get => _imgNormal;
{ set { _imgNormal = value; if (!_hover) BackgroundImage = value; }
imageNormal = value;
BackgroundImage = imageNormal;
}
get
{
return imageNormal;
}
} }
private Image _imgNormal;
//以下代码用于在VS中隐藏BackgroundImage属性,使得只能通过Diameter设置Height和Width [Category("外观"), Description("悬停状态图(可省略)")]
[BrowsableAttribute(false)] public Image ImageHover { get; set; }
public new Image BackgroundImage
{
get
{
return base.BackgroundImage;
}
set
{
base.BackgroundImage = value;
} [Category("外观"), Description("边框颜色(透明=无)")]
} public Color BorderColor { get; set; } = Color.Transparent;
//以下代码用于在VS中隐藏Size属性,使得只能通过Diameter设置Height和Width [Category("外观"), Description("边框粗细(px)")]
[BrowsableAttribute(false)] public float BorderThickness { get; set; } = 1f;
public new Size Size
{
get
{
return base.Size;
}
set
{
base.Size = value;
} /* ───────── 状态 ───────── */
} private bool _hover;
/* ───────── ctor ───────── */
public CircleButton() public CircleButton()
{ {
Radius = 64; SetStyle(ControlStyles.AllPaintingInWmPaint |
this.Height = this.Width = Radius; ControlStyles.UserPaint |
this.FlatStyle = FlatStyle.Flat; ControlStyles.OptimizedDoubleBuffer, true);
this.FlatAppearance.BorderSize = 0;
this.BackgroundImage = imageEnter; FlatStyle = FlatStyle.Flat;
this.BackgroundImageLayout = ImageLayout.Stretch; FlatAppearance.BorderSize = 0;
BackColor = Color.Red;
ForeColor = Color.White;
SyncSize();
MouseEnter += (_, __) => SetHover(true);
MouseLeave += (_, __) => SetHover(false);
} }
//重写OnPaint /* ───────── 重绘 ───────── */
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) protected override void OnResize(EventArgs e)
{
base.OnResize(e);
UpdateRegion();
}
protected override void OnPaint(PaintEventArgs e)
{ {
try {
base.OnPaint(e); base.OnPaint(e);
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath(); e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
path.AddEllipse(0, 0, Radius, Radius);
this.Region = new Region(path); // ① 画圆填充
} using (SolidBrush br = new SolidBrush(BackColor))
catch (Exception ex)
{ {
string Message = ErrorMessage.PromptErrorMessage(ex); e.Graphics.FillEllipse(br, 0, 0, Width, Height);
MessageUtil.Show(Message,ex.Message);
} }
// ② 画边框(可选)
if (BorderColor.A > 0 && BorderThickness > 0)
{
using (Pen pen = new Pen(BorderColor, BorderThickness))
{
Rectangle rc = new Rectangle(
(int)(BorderThickness / 2),
(int)(BorderThickness / 2),
Width - (int)BorderThickness,
Height - (int)BorderThickness);
e.Graphics.DrawEllipse(pen, rc);
}
} }
//重写OnMouseEnter // ③ 绘制数字
//protected override void OnMouseEnter(EventArgs e) if (_number > 0)
//{
// Graphics g = this.CreateGraphics();
// g.DrawEllipse(new Pen(Color.Blue), 0, 0, this.Width, this.Height);
// g.Dispose();
//}
//重写OnSizeChanged
protected override void OnSizeChanged(EventArgs e)
{ {
try { string txt = _number > 99 ? "99" : _number.ToString();
base.OnSizeChanged(e); using (SolidBrush fb = new SolidBrush(ForeColor))
if (Height != Radius) using (StringFormat sf = new StringFormat
{ {
Radius = Width = Height; Alignment = StringAlignment.Center,
} LineAlignment = StringAlignment.Center
else if (Width != Radius) })
{ {
Radius = Height = Width; e.Graphics.DrawString(txt, Font, fb,
new RectangleF(0, 0, Width, Height), sf);
} }
} }
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message,ex.Message);
} }
/* ───────── 私有助手 ───────── */
} private void SetHover(
//重写OnMouseEnter
protected override void OnMouseEnter(EventArgs e)
{
try {
base.OnMouseEnter(e);
BackgroundImage = ImageEnter;
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message,ex.Message);
}
}
//重写OnMouseLeave
protected override void OnMouseLeave(EventArgs e)
{
try {
base.OnMouseLeave(e);
BackgroundImage = ImageNormal;
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message,ex.Message);
}
}
}
}