按钮颜色选择器

 
 
 
 
主要使用自定义组件 组合起来的
 
按钮颜色选择器_第1张图片 按钮颜色选择器_第2张图片
 
按钮颜色选择器_第3张图片
 
 
 

 
/*   
 完成2014年6月26日8:10:01
 
 
 ColorPicker 属性   
 Image    
 Color
 
 事件
 ColorChanged
 Clicked
   
已知问题
 private void button1_Click(object sender, EventArgs e)
 
 {
 1 设置colorPicker1颜色的属性的时候 没有触发ColorChanged
 2 设置colorPicker1颜色的属性的时候 希望自动选中已存在的名子
 3 PictureBox 要背景能透明的
    colorPicker1.Color = Color.Red;
}
 
private void colorPicker1_ColorChanged(object sender, EventArgs e)
{
    label1.BackColor = colorPicker1.Color;
}
 
private void colorPicker1_Clicked(object sender, EventArgs e)
{
    label1.BackColor = colorPicker1.Color;
}
 
 */
 
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
 
namespace System.Windows.Forms
{
    public partial class ColorPicker : UserControl
    {
        public ColorPicker()
        {
            InitializeComponent();
 
            this.MaximumSize = new System.Drawing.Size(44, 28);
 
            Form form = new Form();
            form.StartPosition = FormStartPosition.Manual;
            form.FormBorderStyle = FormBorderStyle.None;
            panel1.Left = 0;
            panel1.Top = 0;
 
            form.Controls.Add(panel1);
            form.TopLevel = false;
            //===============================================================
            this.AutoSize = true;
 
            //===============================================================
            toolStripControlHost1 = new ToolStripControlHost(form);
            toolStripDropDown1.Margin = Padding.Empty;
            toolStripDropDown1.Padding = Padding.Empty;
            toolStripControlHost1.Margin = Padding.Empty;
            toolStripControlHost1.Padding = Padding.Empty;
            toolStripDropDown1.Items.Add(toolStripControlHost1);
 
            toolStripDropDown1.Closed += new System.Windows.Forms.ToolStripDropDownClosedEventHandler(toolStripDropDown1_Closed);
            toolStripDropDown1.Opening += new System.ComponentModel.CancelEventHandler(toolStripDropDown1_Opening);
        }
        #region image1_MouseMove image1_MouseLeave image1_MouseUp image1_MouseDown
        private void image1_MouseMove(object senderMouseEventArgs e)
        {
            image1.Image = move_A.Image;
            image2.Image = move_B.Image;
        }
 
        private void image1_MouseLeave(object senderEventArgs e)
        {
            if (!toolStripDropDownOpend)
            {
                image1.Image = default_A.Image;
                image2.Image = default_B.Image;
            }
        }
 
        private void image1_MouseDown(object senderMouseEventArgs e)
        {
            image1.Image = down_A.Image;
            image2.Image = down_B.Image;
        }
 
        private void image1_MouseUp(object senderMouseEventArgs e)
        {
            if (!toolStripDropDownOpend)
            {
                image1.Image = move_A.Image;
                image2.Image = move_B.Image;
            }
        }
   
        #endregion
 
        private void image3_Click(object senderEventArgs e)
        {
            OnColorChanged(sendere);
        }
 
 
        private void ColorPicker_Load(object senderEventArgs e)
        {
 
          
        }
 
 
        private void toolStripDropDown1_Closed(object senderToolStripDropDownClosedEventArgs e)
        {
            toolStripDropDownOpend = false;
            image1.Image = default_A.Image;
            image2.Image = default_B.Image;
        }
 
 
        private void toolStripDropDown1_Opening(object senderCancelEventArgs e)
        {
            toolStripDropDownOpend = true;
        }
 
        //more color clicked
        private void btnColorDlg_Click(object senderEventArgs e)
        {
            toolStripDropDown1.Close();
            ColorDialog c = new ColorDialog();
            c.FullOpen = true;
            c.AllowFullOpen = true;
            c.AnyColor = true;
            if (c.ShowDialog() == DialogResult.OK)
            {
                SelectedColor = c.Color;
                this.label1.BackColor = SelectedColor;
                //event
                ColorChanged(sendernew EventArgs());
            }
        }
 
        private void button32_MouseMove(object senderMouseEventArgs e)
        {
            back1.Left = ((Button)sender).Left - 2;
            back1.Top = ((Button)sender).Top - 2; 
        }
 
        //三角按钮被单击
        private void image2_MouseDown(object senderMouseEventArgs e)
        {
            image1.Image = down_A.Image;
            image2.Image = down_B.Image;
 
            Point location = PointToScreen(image1.Location);
            toolStripDropDown1.Show(location.Xlocation.Y + image1.Height);
        }
 
        //设置颜色
        private void button32_Click(object senderEventArgs e)
        {
            SelectedColor = ((Button)sender).BackColor;
            this.label1.BackColor = SelectedColor;
            //event
            OnColorChanged(sendere);
 
            toolStripDropDown1.Close();
        }
 
   
        // 摘要:
        //     获取或设置控件的背景色。
        //
        // 返回结果:
        //     表示控件背景色的 System.Drawing.Color。默认为 System.Windows.Forms.Control.DefaultBackColor
        //     属性的值。
        public Color Color
        {
            get {
                return SelectedColor;
            }
            set {
                //没有触发ColorChanged
                
                SelectedColor = value;
                this.label1.BackColor = value;
 
                //if (ColorChanged != null)
                //{
                //    ColorChanged(new object(), new EventArgs());
                //    return;
                //}
 
            }
        }
 
        //
        // 摘要:
        //     获取或设置由 System.Windows.Forms.PictureBox 显示的图像。
        //
        // 返回结果:
        //     要显示的 System.Drawing.Image。
        [Localizable(true)]
        [Bindable(true)]
        public Image Image
        {
            get
            {
                return image3.Image;
            }
            set
            {
                image3.Image = value;
            }
        }
        //单击事件
       
 
        public delegate void EventHandler(object senderEventArgs e);
        public event EventHandler ColorChanged;
        public event EventHandler Clicked;
 
 
        //颜色改变事件
        protected void OnColorChanged(object senderEventArgs e)
        {
            if (ColorChanged != null )
            {
                ColorChanged(sendernew EventArgs());    
            }
        }
 
 
        protected void OnClicked(object senderEventArgs e)
        {
            if (Clicked != null)
            {
                Clicked(sendere);
            }
        }   
 
 
 
 
 
        ToolStripDropDown toolStripDropDown1 = new ToolStripDropDown();
        ToolStripControlHost toolStripControlHost1 = null;
 
        private Color SelectedColor = Color.Black;
        private bool toolStripDropDownOpend = false;
    }
}
 
 





你可能感兴趣的:(选择器)