cloudflight
2024-05-07 2646029d2b97f6835062d45d2cfa632838c527ca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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;
            }
        }
    }
 
}