【转】C# 实现完整功能的截图控件(2)-实现颜色和字体选择控件

上一篇文章介绍了怎样实现绘图工具栏控件,这篇文章介绍截图控件需要用到的另一个控件,就是颜色和字体选择控件ColorSelector。有了它,在绘制图形和文字的时候,就可以选择不同的颜色和字体大小了。先来看下控件的最终效果,然后就来介绍怎样实现它。

1、跟前面一样,还是继承UserControl控件,然后绘制它的背景和边框,因为前面有了,这里就不详细介绍了。

    2、为了方便,实现一个ColorLabel控件用来显示颜色,ColorLabel控件继承Control,实现背景和边框的重绘,使它符合截图控件的整体风格。看看它的详细实现代码:

public class ColorLabel : Control
...{
Fields#region Fields

private Color _borderColor = Color.FromArgb(65, 173, 236);

#endregion

Constructors#region Constructors

public ColorLabel()
: base()
...{
SetStyles();
}

#endregion

Properties#region Properties

[DefaultValue(typeof(Color),"65, 173, 236")]
public Color BorderColor
...{
get ...{ return _borderColor; }
set
...{
_borderColor = value;
base.Invalidate();
}
}

protected override Size DefaultSize
...{
get ...{ return new Size(16, 16); }
}

#endregion

Private Methods#region Private Methods

private void SetStyles()
...{
base.SetStyle(
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw, true);
base.UpdateStyles();
}

#endregion

OverideMethods#region OverideMethods

protected override void OnPaint(PaintEventArgs e)
...{
base.OnPaint(e);

Graphics g = e.Graphics;
Rectangle rect = ClientRectangle;
using (SolidBrush brush = new SolidBrush(base.BackColor))
...{
g.FillRectangle(
brush,
rect);
}

ControlPaint.DrawBorder(
g,
rect,
_borderColor,
ButtonBorderStyle.Solid);

rect.Inflate(-1, -1);
ControlPaint.DrawBorder(
g,
rect,
Color.White,
ButtonBorderStyle.Solid);
}

#endregion
}

3、在UserControl上添加上需要的一些控件,像ComboBox、ColorLabel等等,并把它们排列好,并添加相应的事件。

4、给ColorSelector控件添加上相应的属性和事件,主要是ColorSelector的颜色样式(ColorTable)、选中的颜色(SelectedColor)和字体的大小(FontSize)这三个属性,选中颜色改变(ColorChanged)和字体大小选择改变(FontSizeChanged)这两个事件。看看代码:

Events#region Events

public event EventHandler ColorChanged
...{
add ...{ base.Events.AddHandler(EventColorChanged, value); }
remove ...{ base.Events.RemoveHandler(EventColorChanged, value); }
}

public event EventHandler FontSizeChanged
...{
add ...{ base.Events.AddHandler(EventFontSizeChanged, value); }
remove ...{ base.Events.RemoveHandler(EventFontSizeChanged, value); }
}

#endregion

Properties#region Properties

[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public CaptureToolStripColorTable ColorTable
...{
get
...{
if (_colorTable == null)
...{
_colorTable = new CaptureToolStripColorTable();
}
return _colorTable;
}
set
...{
_colorTable = value;
base.Invalidate();
SetColorLabelBorderColor(ColorTable.BorderColor);
}
}

[Browsable(false)]
public Color SelectedColor
...{
get ...{ return colorLabelSelected.BackColor; }
}

[Browsable(false)]
public int FontSize
...{
get ...{ return int.Parse(comboBoxFontSize.Text); }
}

#endregion

5、因为只有绘制文字的时候,才需要选择字体的大小,所以当不是绘制文字的时候,就把字体大小选择隐藏起来,绘制文字的时候就把它显示出来。用下面这两个函数来实现这个功能:

Public Methods#region Public Methods

public void Reset()
...{
colorLabelSelected.BackColor = Color.Red;
comboBoxFontSize.Text = "12";

panelLeft.Visible = false;
Width = 189;
}

public void ChangeToFontStyle()
...{
panelLeft.Visible = true;
Width = 268;
}

#endregion

通过以上几个步骤,ColorSelector控件就实现了,源代码还是等到整个截图控件全部完成的时候一起放出来吧。

声明:

本文版权归作者和CS 程序员之窗所有,欢迎转载,转载必须保留以下版权信息,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

作者:Starts_2000

出处:CS 程序员之窗 http://www.csharpwin.com

你可以免费使用或修改提供的源代码,但请保留源代码中的版权信息,详情请查看:

CS程序员之窗开源协议 http://www.csharpwin.com/csol.html


posted on 2010-09-27 08:51  shaya  阅读(696)  评论(0编辑  收藏  举报

导航