/*
完成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 sender, MouseEventArgs e)
{
image1.Image = move_A.Image;
image2.Image = move_B.Image;
}
private void image1_MouseLeave(object sender, EventArgs e)
{
if (!toolStripDropDownOpend)
{
image1.Image = default_A.Image;
image2.Image = default_B.Image;
}
}
private void image1_MouseDown(object sender, MouseEventArgs e)
{
image1.Image = down_A.Image;
image2.Image = down_B.Image;
}
private void image1_MouseUp(object sender, MouseEventArgs e)
{
if (!toolStripDropDownOpend)
{
image1.Image = move_A.Image;
image2.Image = move_B.Image;
}
}
#endregion
private void image3_Click(object sender, EventArgs e)
{
OnColorChanged(sender, e);
}
private void ColorPicker_Load(object sender, EventArgs e)
{
}
private void toolStripDropDown1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
toolStripDropDownOpend = false;
image1.Image = default_A.Image;
image2.Image = default_B.Image;
}
private void toolStripDropDown1_Opening(object sender, CancelEventArgs e)
{
toolStripDropDownOpend = true;
}
//more color clicked
private void btnColorDlg_Click(object sender, EventArgs 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(sender, new EventArgs());
}
}
private void button32_MouseMove(object sender, MouseEventArgs e)
{
back1.Left = ((Button)sender).Left - 2;
back1.Top = ((Button)sender).Top - 2;
}
//三角按钮被单击
private void image2_MouseDown(object sender, MouseEventArgs e)
{
image1.Image = down_A.Image;
image2.Image = down_B.Image;
Point location = PointToScreen(image1.Location);
toolStripDropDown1.Show(location.X, location.Y + image1.Height);
}
//设置颜色
private void button32_Click(object sender, EventArgs e)
{
SelectedColor = ((Button)sender).BackColor;
this.label1.BackColor = SelectedColor;
//event
OnColorChanged(sender, e);
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 sender, EventArgs e);
public event EventHandler ColorChanged;
public event EventHandler Clicked;
//颜色改变事件
protected void OnColorChanged(object sender, EventArgs e)
{
if (ColorChanged != null )
{
ColorChanged(sender, new EventArgs());
}
}
protected void OnClicked(object sender, EventArgs e)
{
if (Clicked != null)
{
Clicked(sender, e);
}
}
ToolStripDropDown toolStripDropDown1 = new ToolStripDropDown();
ToolStripControlHost toolStripControlHost1 = null;
private Color SelectedColor = Color.Black;
private bool toolStripDropDownOpend = false;
}
}