自写的拾色器分享(界面布局类似ps)

rayyu1989 2012-09-29 03:09:18
加精
按惯例AD下搜索易:http://www.370b.com/

---------------------------------------------------------------
欢迎使用颜色选择器
Copyright At Rayyu,All Rights Reserved.
允许您在任意场合使用该dll的功能,但禁止篡改我们的代码.
---------------------------------------------------------------
Rayyu.ColorPicker.net2.0.zip 适用于.Net2.0、.Net3.0、.Net3.5
Rayyu.ColorPicker.net4.0.zip 适用于.Net4.0、.Net4.5
demo.zip 需在vs2010和vs2012里打开 是2.0的实例版本.






资源文件下载:http://download.csdn.net/detail/rayyu1989/4608611

vb.net范例:
Private Sub MyColorBtn1_Click(sender As Object, e As EventArgs) Handles MyColorBtn1.Click  
'初始化
Using s As New Rayyu.ColorPicker(sender, MyColorBtn1.BackColor, 50)
'注册颜色变更事件
AddHandler s.OnColorChanged, AddressOf OnColorChange
'注册拾色器完成/退出事件
AddHandler s.OnColorCompleted, AddressOf OnColorCompleted
'拾色器窗口的标题
s.Text = "提示" & Now.ToString("yyyy-MM-dd HH:mm:ss")
'打开拾色器
s.Show()
End Using
End Sub
''' <summary>
''' 颜色更改时
''' </summary>
''' <param name="sender">回调对象</param>
''' <param name="e"></param>
Private Sub OnColorChange(sender As Object, e As Rayyu.ColorPickChangedEventArgs)
'dosomething
End Sub
''' <summary>
''' 点击了确定或取消并退出了颜色选择器
''' </summary>
''' <param name="sender">回调对象</param>
''' <param name="e"></param>
Private Sub OnColorCompleted(sender As Object, e As Rayyu.ColorPickCompletedEventArgs)
'dosomething
If e.iscancel Then
MsgBox("操作被取消!")
ElseIf e.NewColor.ToArgb = e.OldColor.ToArgb Then
MsgBox("颜色没有变化!")
Else
MsgBox("这是1个新的颜色!")
End If
End Sub


C#范例:
private void MyColorBtn1_Click(object sender, EventArgs e)  
{
//初始化
using (Rayyu.ColorPicker s = new Rayyu.ColorPicker(sender, MyColorBtn1.BackColor, 50)) {
//注册颜色变更事件
s.OnColorChanged += OnColorChange;
//注册拾色器完成/退出事件
s.OnColorCompleted += OnColorCompleted;
//拾色器窗口的标题
s.Text = "提示" + DateAndTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
//打开拾色器
s.Show();
}
}
/// <summary>
/// 颜色更改时
/// </summary>
/// <param name="sender">回调对象</param>
/// <param name="e"></param>
private void OnColorChange(object sender, Rayyu.ColorPickChangedEventArgs e)
{
//dosomething
}
/// <summary>
/// 点击了确定或取消并退出了颜色选择器
/// </summary>
/// <param name="sender">回调对象</param>
/// <param name="e"></param>
private void OnColorCompleted(object sender, Rayyu.ColorPickCompletedEventArgs e)
{
//dosomething
if (e.iscancel) {
Interaction.MsgBox("操作被取消!");
} else if (e.NewColor.ToArgb == e.OldColor.ToArgb) {
Interaction.MsgBox("颜色没有变化!");
} else {
Interaction.MsgBox("这是1个新的颜色!");
}
}


更多说明:http://blog.csdn.net/rayyu1989/article/details/8032354
...全文
2535 77 打赏 收藏 转发到动态 举报
写回复
用AI写文章
77 条回复
切换为时间正序
请发表友善的回复…
发表回复
ivanwfy 2015-01-05
  • 打赏
  • 举报
回复
收藏了,感谢分享
assky124 2013-01-29
  • 打赏
  • 举报
回复
放个我写的,其实就一点点代码而已
效果如下:


internal partial class ColorPanel : Control
{
private float _fX = 0.5f;
private float _fY = 0.5f;

private Bitmap _imgColor = null;
private Color _colSelect = Color.Black;

public ColorPanel()
{
InitializeComponent();
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.BackColor = Color.Red;
XValue = 0.5f;
YValue = 0.5f;
}

public event EventHandler SelectColorChanged;

public float XValue
{
get { return _fX; }
private set { _fX = value; }
}

public float YValue
{
get { return _fY; }
private set { _fY = value; }
}

public Color SelectColor
{
get { return _colSelect; }
private set
{
_colSelect = value;
OnSelectChanged(EventArgs.Empty);
}
}

public override Color BackColor
{
get
{
return base.BackColor;
}
set
{
base.BackColor = value;
CreateColorImage();
SelectColor = GetSelectColor(XValue, YValue);
}
}

protected Rectangle DrawRect
{
get;
set;
}

protected bool IsMouseDown
{
get;
set;
}

public void SetLocation(float x, float y)
{
x = x < 0 ? 0 : x; x = x > 1 ? 1 : x;
y = y < 0 ? 0 : y; y = y > 1 ? 1 : y;
XValue = x;
YValue = y;
this.Invalidate();
SelectColor = GetSelectColor(x, y);
//OnSelectChanged(EventArgs.Empty);
}

private Color GetSelectColor(float x, float y)
{
if (_imgColor != null)
{
int px = Convert.ToInt32(x * _imgColor.Width);
int py = Convert.ToInt32(y * _imgColor.Height);
if (px == _imgColor.Width)
{
px--;
}
if (py == _imgColor.Height)
{
py--;
}
Color c = _imgColor.GetPixel(px, py);
return c;
}
return Color.Black;
}

protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
CreateColorImage();
ComputeDrawRect();
}

protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
if (pe.ClipRectangle.IntersectsWith(this.ClientRectangle))
{
//pe.Graphics.Clear(Color.Black);
if (_imgColor == null)
{
CreateColorImage();
}
if (_imgColor != null)
{
pe.Graphics.DrawImage(_imgColor, new Rectangle(0, 0, this.Width, this.Height));
}
if (DrawRect.IsEmpty)
{
ComputeDrawRect();
}
pe.Graphics.DrawRectangle(Pens.Black, DrawRect);
DrawSelect(pe);
}
}

protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
IsMouseDown = true;
}

protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
IsMouseDown = false;
SetLocation(e.Location);
}

protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (IsMouseDown)
{
SetLocation(e.Location);
}
}

protected virtual void OnSelectChanged(EventArgs e)
{
if (SelectColorChanged != null)
{
SelectColorChanged(this, e);
}
}

protected void DrawSelect(PaintEventArgs e)
{
Point pt = new Point(Convert.ToInt32(XValue * this.Width),
Convert.ToInt32(YValue * this.Height));
pt.X = pt.X < 1 ? 1 : pt.X;
pt.X = pt.X > this.Width - 1 ? this.Width - 1 : pt.X;
pt.Y = pt.Y < 1 ? 1 : pt.Y;
pt.Y = pt.Y > this.Height - 1 ? this.Height - 1 : pt.Y;
Rectangle rect = new Rectangle(pt.X - 4, pt.Y - 4, 8, 8);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle rectDraw = rect;

rectDraw = Rectangle.Inflate(rect, 1, 1);
e.Graphics.DrawEllipse(Pens.White, rectDraw);
rectDraw = Rectangle.Inflate(rect, -1, -1);
e.Graphics.DrawEllipse(Pens.White, rectDraw);
rectDraw = rect;
e.Graphics.DrawEllipse(Pens.Black, rectDraw);
}

private void ComputeDrawRect()
{
if (this.Width > 1 && this.Height > 1)
{
DrawRect = new Rectangle(0, 0, this.Width + 1, this.Height - 1);
}
}

private void CreateColorImage()
{
if (this.Width > 0 && this.Height > 0)
{
int width = 250;
int height = 200;
_imgColor = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(_imgColor))
{
LinearGradientBrush brush1 = new LinearGradientBrush(
Point.Empty, new Point(250, 0),
Color.White,
Color.Transparent);
LinearGradientBrush brush2 = new LinearGradientBrush(
Point.Empty, new Point(0, 200),
Color.FromArgb(0, Color.White),
Color.Black);

Rectangle rect = new Rectangle(0, 0, 250, 200);

g.FillRectangle(new SolidBrush(this.BackColor), rect);
g.FillRectangle(brush1, rect);
g.FillRectangle(brush2, rect);
}
}
}

private void SetLocation(Point pt)
{
if (Width > 0 && Height > 0)
{
float x = Convert.ToSingle(pt.X) / this.Width;
float y = Convert.ToSingle(pt.Y) / this.Height;
SetLocation(x, y);
}
}
}
assky124 2013-01-29
  • 打赏
  • 举报
回复
这个真没啥难度。
liduoduo 2013-01-29
  • 打赏
  • 举报
回复
谢谢分享,不错
_猫了个咪 2013-01-29
  • 打赏
  • 举报
回复
liyongjieailiting 2013-01-28
  • 打赏
  • 举报
回复
刚好在找,收下了
jojoova 2012-10-16
  • 打赏
  • 举报
回复
感觉挺实用的,感谢分享
tzg1975 2012-10-16
  • 打赏
  • 举报
回复
努力学习中
renegade2008 2012-10-16
  • 打赏
  • 举报
回复
顶下,呵呵
绿领巾童鞋 2012-10-16
  • 打赏
  • 举报
回复
大雄,快出来,技安来欺负你了~
tang12s 2012-10-16
  • 打赏
  • 举报
回复
真好,太强
descom 2012-10-15
  • 打赏
  • 举报
回复
不懂。。。。
dengqiang2016 2012-10-13
  • 打赏
  • 举报
回复
选择颜色的吗,还是做什么的,没有看明白。
youhao1999 2012-10-12
  • 打赏
  • 举报
回复
路过帮顶
maomao58 2012-10-12
  • 打赏
  • 举报
回复
厉害了!!!
奋斗吧_骚年 2012-10-10
  • 打赏
  • 举报
回复
楼主 厉害了 学习下先
w20128ban 2012-10-10
  • 打赏
  • 举报
回复
厉害啊
zaizaidezz 2012-10-09
  • 打赏
  • 举报
回复
非常感谢
ssa 2012-10-09
  • 打赏
  • 举报
回复
很棒,学习,学习
wdran8 2012-10-09
  • 打赏
  • 举报
回复
非常棒~希望能更进一步,没有最好,
加载更多回复(40)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧