schangxiang@126.com
2025-11-04 f5ed29dc26c7cd952d56ec5721a2efc43cd25992
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
106
107
108
109
110
111
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media;
using XCommon;
using XCommon.Log;
using Color = System.Windows.Media.Color;
using UserControl = System.Windows.Controls.UserControl;
 
namespace XHandler.View.ColorPicker
{
    /// <summary>
    /// 颜色表示按钮
    /// </summary>
    public partial class ColorPickerBtn : UserControl
    {
        #region 属性
        #region RGB字符串值(255,255,0)
        /// <summary>
        /// RGB
        /// </summary>
        public static readonly DependencyProperty RGBProperty = DependencyProperty.Register("RGB", typeof(string), typeof(ColorPickerBtn),
         new FrameworkPropertyMetadata((string)"", OnRGBChanged));
 
        public string RGB
        {
            get { return (string)GetValue(RGBProperty); }
            set { SetValue(RGBProperty, value); }
        }
 
        public static void OnRGBChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ColorPickerBtn colorPickerBtn = (ColorPickerBtn)d;
 
            // 颜色设置
            colorPickerBtn.SetColor();
        }
        #endregion
        #endregion
 
        #region 事件
        /// <summary>
        /// 颜色改变事件
        /// </summary>
        public event EventHandler SelectedColorChangedEvent;
        #endregion
 
        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public ColorPickerBtn()
        {
            InitializeComponent();
        }
        #endregion
 
        #region 初始化
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                // 颜色设置
                SetColor();
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("ERROR:", ex);
            }
        }
 
        /// <summary>
        /// 颜色设置
        /// </summary>
        public void SetColor()
        {
            btnShowColor.Background = new SolidColorBrush(ComUtility.RGBToColor(RGB));
        }
        #endregion
 
        /// <summary>
        /// 显示颜色拾取器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnShowColor_Click(object sender, RoutedEventArgs e)
        {
            ColorPickerEx colorPickerEx = new ColorPickerEx();
            colorPickerEx.ColorBrush = (SolidColorBrush)btnShowColor.Background;
            colorPickerEx.ShowDialog();
 
            if (colorPickerEx.DialogResult == true)
            {
                Color color = colorPickerEx.ColorBrush.Color;
                //btnShowColor.Background = new SolidColorBrush(color);
                RGB = $"{color.R},{color.G},{color.B}";
 
                if (SelectedColorChangedEvent != null)
                {
                    SelectedColorChangedEvent(this, EventArgs.Empty);
                }
            }
        }
    }
}