using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Hydro.MapUI.WindowsForm { public partial class ColorPickerWinForm : Control { private Color _color; public Color Color { get { return _color; } set { _color = value; Invalidate(); } } private bool _readonly; public bool ReadOnly { get { return _readonly; } set { _readonly = value; Invalidate(); } } private BorderStyle _borderStyle = BorderStyle.None; public BorderStyle BorderStyle { get { return _borderStyle; } set { _borderStyle = value; Invalidate(); } } protected override void OnClick(EventArgs e) { if (ReadOnly) { return; } base.OnClick(e); ColorDialog colorDialog = new ColorDialog(); if (colorDialog.ShowDialog() == DialogResult.OK) { Color = colorDialog.Color; } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); // 绘制边框 if (_borderStyle != BorderStyle.None) { ControlPaint.DrawBorder(e.Graphics, ClientRectangle, ForeColor, ButtonBorderStyleFromBorderStyle(_borderStyle)); } // 绘制颜色矩形在左边 Rectangle colorRect = new Rectangle(1, 1, Height - 2, Height - 2); using (Brush brush = new SolidBrush(_color)) { e.Graphics.FillRectangle(brush, colorRect); } // 显示颜色名称和RGB值在右边 string colorName = _color.Name; string displayText = $"{colorName} ({_color.R}, {_color.G}, {_color.B})"; using (Brush brush = new SolidBrush(ForeColor)) { SizeF textSize = e.Graphics.MeasureString(displayText, Font); PointF textLocation = new PointF(Height + 10, (Height - textSize.Height) / 2); e.Graphics.DrawString(displayText, Font, brush, textLocation); } } private ButtonBorderStyle ButtonBorderStyleFromBorderStyle(BorderStyle style) { switch (style) { case BorderStyle.FixedSingle: return ButtonBorderStyle.Solid; case BorderStyle.Fixed3D: return ButtonBorderStyle.Inset; default: return ButtonBorderStyle.None; } } } }