65.9K
CodeProject 正在变化。 阅读更多。
Home

一个简单的画笔颜色选择器

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.86/5 (3投票s)

2005年1月24日

1分钟阅读

viewsIcon

48120

downloadIcon

670

一个像画笔颜色选择器一样的简单颜色选择器。

引言

这个控件非常简单,但我认为它对 C# 编程初学者,尤其是 .NET UserControl 编程很有帮助。

行为

就像画笔颜色选择器一样,我们的颜色选择器必须具有以下功能:

  • 一个显示当前所选画笔和笔颜色的区域。
  • 提供一些预定义的颜色,分为两行。第一行比第二行深。
  • 用户将使用鼠标左键选择笔颜色,使用鼠标右键选择画笔颜色。
  • 当用户双击指定的颜色单元格时,应用程序会使用标准的颜色选择器对话框询问用户他们想要使用的颜色。
  • 最后,这个控件应该有一个事件,用于通知父容器画笔和笔颜色的变化。

实现

首先,我们应该创建一个新的用户控件,其中包含几个标签作为颜色单元格,您在设计时查看控件时会看到这些标签。之后,我们应该有一种方法来存储预定义的颜色以及所选的画笔和笔颜色。下面的示例详细说明了如何做到这一点

private const int TotalColors = 16;
private Color[] _Colors = new Color[TotalColors];
private int _selPenColor = 0;
private int _selBrushColor = 1;
public Color PenColor
{
    get { return this._Colors[_selPenColor];}
    set 
    {
        this._Colors[0] = value;
        this._selPenColor = 0;
    }
}
public Color BrushColor
{
    get {return this._Colors[_selBrushColor];}
    set
    {
        this._Colors[TotalColors/2] = value;
        this._selBrushColor = TotalColors/2;
    }
}

在下一步中,我们应该有一些函数来负责绘制

private void DrawSelectedColor(Graphics graph, Rectangle rect, 
                                Color clrPen, Color clrBrush)
{
    Brush brushBackground = new SolidBrush(this.BackColor);
    Brush penBrush = new SolidBrush(clrPen);
    Brush brushBrush = new SolidBrush(clrBrush);

    Point ptCenter = new Point(rect.Left + rect.Width/2, 
                              rect.Top + rect.Height/2);
    int cellWidth = 16;
    int cellHeight = 16;

    int XMargin = 4;
    int YMargin = 4;

    Rectangle rectPen = new Rectangle(XMargin, YMargin, 
                                cellWidth, cellHeight);
    Rectangle rectBrush = new Rectangle(rect.Right-XMargin-cellWidth, 
              rect.Bottom-YMargin-cellHeight, cellWidth, cellHeight);

    // clear background
    graph.FillRectangle(brushBackground, rect);

    // draw selected colors
    graph.FillRectangle(brushBrush, rectBrush);
    ControlPaint.DrawBorder3D(graph, rectBrush, Border3DStyle.Raised);

    graph.FillRectangle(penBrush, rectPen);
    ControlPaint.DrawBorder3D(graph, rectPen, Border3DStyle.Raised);
    
    // draw border 3D
    ControlPaint.DrawBorder3D(graph, rect, Border3DStyle.Sunken);

    penBrush.Dispose();
    brushBrush.Dispose();
    brushBackground.Dispose();
}
private void DrawColor(Graphics graph, Rectangle rect, Color color)
{
    Brush brush = new SolidBrush(color);
    graph.FillRectangle(brush, rect);
    
    ControlPaint.DrawBorder3D(graph, rect, Border3DStyle.SunkenOuter);
    brush.Dispose();
}

并将实现的函数附加到所有者的标准绘制路由

private void ColorButton_Paint(object sender, PaintEventArgs e)
{
    if (sender is Label)
    {
        Label btn = sender as Label;
        int colorID = Int32.Parse(btn.Tag.ToString());
        DrawColor(e.Graphics, btn.DisplayRectangle, this._Colors[colorID]);
    }            
}
private void SelectColorButton_Paint(object sender, PaintEventArgs e)
{
    DrawSelectedColor(e.Graphics, btnSelectedColors.DisplayRectangle, 
                                     this.PenColor, this.BrushColor);
}

如前所述,我们应该有一个委托来通知容器所选笔和画笔颜色的变化。

public event EventHandler    PenColorChanged = null;
public event EventHandler    BrushColorChanged = null;

...........

private void OnSelectedBrushColorChanged()
{

    if (this.BrushColorChanged != null)
        this.BrushColorChanged(this, new System.EventArgs());
}

private void OnSelectedPenColorChanged()
{
    if (this.PenColorChanged != null)
        this.PenColorChanged(this, new System.EventArgs());
}

最后,我们应该处理最终用户的选择操作

private void ColorButton_MouseDown(object sender, MouseEventArgs e)
{            
    if (!(sender is Label)) return;
    Label btn = sender as Label;
    int colorID = Int32.Parse(btn.Tag.ToString());
        
    if (e.Button == MouseButtons.Left)
        this.SelectPenColorID = colorID;
    else if (e.Button == MouseButtons.Right)
        this.SelectedBrushColorID = colorID;
    btnSelectedColors.Invalidate();
}
private void ColorButton_DoubleClick(object sender, System.EventArgs e)
{
    if (!(sender is Label)) return;
    Label btn = sender as Label;
    int colorID = Int32.Parse(btn.Tag.ToString());

    ColorDialog dlg = new ColorDialog();
    dlg.Color = this._Colors[colorID];
    dlg.FullOpen = false;
    dlg.SolidColorOnly = true;
    if (dlg.ShowDialog(this) == DialogResult.OK)
    {
        this._Colors[colorID] = dlg.Color;
        this.Refresh();
    }
}

现在,您可以构建您的项目并测试您新的类似画笔的颜色选择器。

© . All rights reserved.