日期:2013-02-06  浏览次数:20426 次

源文件:http://ded.nuaa.edu.cn/download/Windows%20Extended%20Controls.rar
示例代码:http://ded.nuaa.edu.cn/download/WindowsApplication6.rar

最近做一个图象的采集,需要一个图形的选择控件,但是在.net下没有类似vb中的shape控件,所以考虑了自己写一个控件。

下面我将从头创建控件,这个控件主要是用来选择图形的Rectangle,有一下几个属性Color BorderColor:边框颜色,Color BackColor:背景颜色,bool ReSizeble:是否可移动, Rectangle SelectRectangle:选择区域。
打开vs2003(我用的这个版本),新建一个c#控件库,ok,拷贝如下代码到你的代码里。

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace WindowsExtendedControls
{
/// <summary>
/// 控件
/// </summary>
public class ShapeEx : System.Windows.Forms.Control
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
///
private Color _BorderColor=new Color();
private Color _BackColor=new Color();
private bool _ReSizeble;
private Point _SelfLocation=new Point();
private Point _MouseLocation=new Point();
private int _SelfWidth;
private int _SelfHeight;
private int _SelectSelctedIndex;//0-8,0:SizeAll
private Rectangle _rectLeftSelector=new Rectangle();
private Rectangle _rectTopSelector=new Rectangle();
private Rectangle _rectRightSelector=new Rectangle();
private Rectangle _rectBottomSelector=new Rectangle();
private Rectangle _rectLeftTopSelector=new Rectangle();
private Rectangle _rectRightTopSelector=new Rectangle();
private Rectangle _rectRightBottomSelector=new Rectangle();
private Rectangle _rectLeftBottomSelector=new Rectangle();
private System.ComponentModel.Container components = null;
public ShapeEx()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();

// TODO: 在 InitComponent 调用后添加任何初始化

}
[DefaultValue("Black"),Description("边框颜色"),Category("Appearance")]
public Color BorderColor
{
get
{
// Insert code here.
return _BorderColor;
}
set
{
_BorderColor=value;
this.Invalidate();
}
}
[DefaultValue("Control"),Description("背景颜色"),Category("Appearance")]
public override Color BackColor
{
get
{
// Insert code here.
return _BackColor;
}
set
{
_BackColor=value;
this.Invalidate();
}
}
[DefaultValue(false),Description("运行中控件大小是否可拖拽编辑"),Category("Behavior")]
public bool ReSizeble
{
get
{
// Insert code here.
return _ReSizeble;
}
set
{
_ReSizeble=value;
this.Invalidate();
}
}
[Description("控件选择区域"),Category("Behavior")]
public Rectangle SelectRectangle
{
get
{
Rectangle selectRectangler=new Rectangle();
selectRectangler.X = this.Location.X+7;
selectRectangler.Y = this.Location.Y+7;
selectRectangler.Height = this.Height-15;
selectRectangler.Width = this.Width-15;
return selectRectangler;
}

}

protected override void OnPaint(PaintEventArgs pe)
{
// Calling the base class OnPaint
base.OnPaint(pe);
ReDrawControl(pe.Graphics);
}
private void DrawSelector(Graphics graphics)
{
SolidBrush SelectorPen=new SolidBrush(Color.White);
Pen borderPen=new Pen(this._BorderColor,1);
try
{
//实心

PointF[] LeftPoints=getPointF(0,this.Height/2-3,6,6);
graphics.FillClosedCurve(SelectorPen, LeftPoints);

PointF[] TopPoints=getPointF(this.Width/2-3,0,6,6);
graphics.FillClosedCurve(SelectorPen, TopPoints);

PointF[] RightPoints=getPointF(this.Width-7,thi