彩票走势图

C# WinForm自定义控件开发实例

转帖|其它|编辑:郝浩|2011-09-16 14:00:23.000|阅读 3633 次

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

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

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

  下面我将从头创建控件,这个控件主要是用来选择图形的Rectangle,有一下几个属性Color BorderColor:边框颜色,Color BackColor:背景颜色,bool ReSizeble:是否可移动, Rectangle SelectRectangle:选择区域。

  打开vs2003(我用的这个版本),新建一个c#控件库,ok,拷贝如下代码到你的代码里。

u​s​i​n​g ​S​y​s​t​e​m​;​
u​s​i​n​g ​S​y​s​t​e​m​.​C​o​l​l​e​c​t​i​o​n​s​;​
u​s​i​n​g ​S​y​s​t​e​m​.​C​o​m​p​o​n​e​n​t​M​o​d​e​l​;​
u​s​i​n​g ​S​y​s​t​e​m​.​D​r​a​w​i​n​g​;​
u​s​i​n​g ​S​y​s​t​e​m​.​D​a​t​a​;​
u​s​i​n​g ​S​y​s​t​e​m​.​W​i​n​d​o​w​s​.​F​o​r​m​s​;​

n​a​m​e​s​p​a​c​e ​W​i​n​d​o​w​s​E​x​t​e​n​d​e​d​C​o​n​t​r​o​l​s​
{​
/​/​/ <​s​u​m​m​a​r​y​>
/​/​/ ​控​件​
/​/​/ <​/​s​u​m​m​a​r​y​>
p​u​b​l​i​c c​l​a​s​s ​S​h​a​p​e​E​x​ ​:​ ​S​y​s​t​e​m​.​W​i​n​d​o​w​s​.​F​o​r​m​s​.​C​o​n​t​r​o​l​
{​
/​/​/ <​s​u​m​m​a​r​y​>
/​/​/ ​必​需​的​设​计​器​变​量​。​
/​/​/ <​/​s​u​m​m​a​r​y​>
/​/​/
p​r​i​v​a​t​e ​C​o​l​o​r​ ​_​B​o​r​d​e​r​C​o​l​o​r=n​e​w ​C​o​l​o​r​(​)​;​
p​r​i​v​a​t​e ​C​o​l​o​r​ ​_​B​a​c​k​C​o​l​o​r=n​e​w ​C​o​l​o​r​(​)​;​
p​r​i​v​a​t​e b​o​o​l ​_​R​e​S​i​z​e​b​l​e​;​
p​r​i​v​a​t​e ​P​o​i​n​t​ ​_​S​e​l​f​L​o​c​a​t​i​o​n=n​e​w ​P​o​i​n​t​(​)​;​
p​r​i​v​a​t​e ​P​o​i​n​t​ ​_​M​o​u​s​e​L​o​c​a​t​i​o​n=n​e​w ​P​o​i​n​t​(​)​;​
p​r​i​v​a​t​e i​n​t ​_​S​e​l​f​W​i​d​t​h​;​
p​r​i​v​a​t​e i​n​t ​_​S​e​l​f​H​e​i​g​h​t​;​
p​r​i​v​a​t​e i​n​t ​_​S​e​l​e​c​t​S​e​l​c​t​e​d​I​n​d​e​x​;/​/0​-​8​,​0​:​S​i​z​e​A​l​l
p​r​i​v​a​t​e 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,this.Height/2-3,6,6);
graphics.FillClosedCurve(SelectorPen, RightPoints);

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

PointF[] LeftTopPoints=getPointF(0,0,6,6);
graphics.FillClosedCurve(SelectorPen, LeftTopPoints);

PointF[] RightTopPoints=getPointF(this.Width-7,0,6,6);
graphics.FillClosedCurve(SelectorPen, RightTopPoints);

PointF[] RightBottomPoints=getPointF(this.Width-7,this.Height-7,6,6);
graphics.FillClosedCurve(SelectorPen, RightBottomPoints);

PointF[] LeftBottomPoints=getPointF(0,this.Height-7,6,6);
graphics.FillClosedCurve(SelectorPen, LeftBottomPoints);
//边框
_rectLeftSelector.X = 0;
_rectLeftSelector.Y = this.Height/2-3;
_rectLeftSelector.Height = 6;
_rectLeftSelector.Width = 6;
graphics.DrawRectangle(borderPen, _rectLeftSelector);

_rectTopSelector.X = this.Width/2-3;
_rectTopSelector.Y = 0;
_rectTopSelector.Height = 6;
_rectTopSelector.Width = 6;
graphics.DrawRectangle(borderPen, _rectTopSelector);

_rectRightSelector.X = this.Width-7;
_rectRightSelector.Y = this.Height/2-3;
_rectRightSelector.Height = 6;
_rectRightSelector.Width = 6;
graphics.DrawRectangle(borderPen, _rectRightSelector);

_rectBottomSelector.X = this.Width/2-3;
_rectBottomSelector.Y = this.Height-7;
_rectBottomSelector.Height = 6;
_rectBottomSelector.Width = 6;
graphics.DrawRectangle(borderPen, _rectBottomSelector);

_rectLeftTopSelector.X=0;
_rectLeftTopSelector.Y=0;
_rectLeftTopSelector.Width=6;
_rectLeftTopSelector.Height=6;
graphics.DrawRectangle(borderPen, _rectLeftTopSelector);

_rectRightTopSelector.X=this.Width-7;
_rectRightTopSelector.Y=0;
_rectRightTopSelector.Width=6;
_rectRightTopSelector.Height=6;
graphics.DrawRectangle(borderPen, _rectRightTopSelector);

_rectRightBottomSelector.X=this.Width-7;
_rectRightBottomSelector.Y=this.Height-7;
_rectRightBottomSelector.Width=6;
_rectRightBottomSelector.Height=6;
graphics.DrawRectangle(borderPen, _rectRightBottomSelector);

_rectLeftBottomSelector.X=0;
_rectLeftBottomSelector.Y=this.Height-7;
_rectLeftBottomSelector.Width=6;
_rectLeftBottomSelector.Height=6;
graphics.DrawRectangle(borderPen, _rectLeftBottomSelector);
}
catch(Exception E)
{
throw E;
}
finally
{
SelectorPen.Dispose();
borderPen.Dispose();
}

}
private void ReDrawControl(Graphics graphics)
{

try
{

//绘制背景
/*
graphics.Clear(this._BackColor);
SolidBrush backPen=new SolidBrush(this._BackColor);
PointF point1 = new PointF(1,1);
PointF point2 = new PointF(this.Width-2,1);
PointF point3 = new PointF(this.Width-2,this.Height-2);
PointF point4 = new PointF(1,this.Height-2);
PointF[] points = {point1, point2, point3, point4};
graphics.FillClosedCurve(backPen, points);
*/
//绘制边框
Rectangle rectBorder=new Rectangle();
Pen borderPen=new Pen(this._BorderColor,1);
rectBorder.X = 7;
rectBorder.Y = 7;
rectBorder.Height = this.Height-15;
rectBorder.Width = this.Width-15;
graphics.DrawRectangle(borderPen, rectBorder);
//绘制编辑框
if (_ReSizeble)
{
DrawSelector(graphics);
}
}
catch(Exception E)
{
throw E;
}
finally
{
graphics.Dispose();
}
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
private PointF[] getPointF(int x,int y,int Width,int Height){
PointF point1 = new PointF(x,y);
PointF point2 = new PointF(x+Width,y);
PointF point3 = new PointF(x+Width,y+Height);
PointF point4 = new PointF(x,y+Height);
PointF[] points = {point1, point2, point3, point4};
return points;
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}

#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.Resize+=new EventHandler(ShapeEx_Resize);
this.MouseDown+=new MouseEventHandler(ShapeEx_MouseDown);
this.MouseMove+=new MouseEventHandler(ShapeEx_MouseMove);
this.MouseLeave+=new EventHandler(ShapeEx_MouseLeave);
this.MouseUp+=new MouseEventHandler(ShapeEx_MouseUp);

this._BorderColor=Color.Black;
this._BackColor=Color.FromName("Control");
this._ReSizeble=false;
this._SelectSelctedIndex=-1;
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
}
#endregion

private void ShapeEx_Resize(object sender, EventArgs e)
{
if (this.Width<16 || this.Height<16)
{
this.Width=16;
this.Height=16;
}
this.Invalidate();
}



private void ShapeEx_MouseDown

(object sender, MouseEventArgs e)
{
if (_ReSizeble)
{
if (_rectLeftSelector.Contains(e.X,e.Y) || _

rectRightSelector.Contains(e.X,e.Y) || _

rectTopSelector.Contains(e.X,e.Y) || _

rectBottomSelector.Contains(e.X,e.Y) ||_

rectLeftTopSelector.Contains(e.X,e.Y) || _

rectRightTopSelector.Contains(e.X,e.Y) || _

rectRightBottomSelector.Contains(e.X,e.Y) ||

_rectLeftBottomSelector.Contains(e.X,e.Y))
{
if (_rectLeftTopSelector.Contains(e.X,e.Y) )
{
this.Cursor=Cursors.SizeNWSE;
this._SelectSelctedIndex=1;
}

if (_rectTopSelector.Contains(e.X,e.Y) )
{
this.Cursor=Cursors.SizeNS;
this._SelectSelctedIndex=2;
}
if (_rectRightTopSelector.Contains(e.X,e.Y) )
{
this.Cursor=Cursors.SizeNESW;
this._SelectSelctedIndex=3;
}
if (_rectRightSelector.Contains(e.X,e.Y) )
{
this.Cursor=Cursors.SizeWE;
this._SelectSelctedIndex=4;
}
if (_rectRightBottomSelector.Contains(e.X,e.Y) )
{
this.Cursor=Cursors.SizeNWSE;
this._SelectSelctedIndex=5;
}
if (_rectBottomSelector.Contains(e.X,e.Y))
{
this.Cursor=Cursors.SizeNS;
this._SelectSelctedIndex=6;
}
if (_rectLeftBottomSelector.Contains(e.X,e.Y) )
{
this.Cursor=Cursors.SizeNESW;
this._SelectSelctedIndex=7;
}
if (_rectLeftSelector.Contains(e.X,e.Y))
{
this.Cursor=Cursors.SizeWE;
this._SelectSelctedIndex=8;
}

}
else
{
this.Cursor=Cursors.SizeAll;
this._SelectSelctedIndex=0;
}
this._SelfLocation.X=this.Location.X;
this._SelfLocation.Y=this.Location.Y;
this._MouseLocation.X=Cursor.Position.X;
this._MouseLocation.Y=Cursor.Position.Y;
this._SelfWidth=this.Width;
this._SelfHeight=this.Height;
}
}

private void ShapeEx_MouseMove(object sender, MouseEventArgs e)
{
//move and resize
switch (this._SelectSelctedIndex)
{
case 0:
this.Location=new Point(Cursor.Position.X-

(_MouseLocation.X-_SelfLocation.X),Cursor.Position.Y-

(_MouseLocation.Y-_SelfLocation.Y));
break;
case 1:
this.Height=this._SelfHeight-(Cursor.Position.Y-_MouseLocation.Y);
this.Width=this._SelfWidth-(Cursor.Position.X-_MouseLocation.X);
this.Location=new Point(Cursor.Position.X-_

MouseLocation.X+_SelfLocation.X,Cursor.Position.Y-_

MouseLocation.Y+_SelfLocation.Y);
break;
case 2:
this.Height=this._SelfHeight-(Cursor.Position.Y-_MouseLocation.Y);
this.Location=new Point(_SelfLocation.X,Cursor.Position.Y-_

MouseLocation.Y+_SelfLocation.Y);
break;
case 3:
this.Height=this._SelfHeight-(Cursor.Position.Y-_MouseLocation.Y);
this.Width=this._SelfWidth+(Cursor.Position.X-_MouseLocation.X);
this.Location=new Point(_SelfLocation.X,Cursor.Position.Y

-(_MouseLocation.Y-_SelfLocation.Y));
break;
case 4:
this.Width=this._SelfWidth+(Cursor.Position.X-_MouseLocation.X);
break;
case 5:
this.Height=this._SelfHeight+(Cursor.Position.Y-_MouseLocation.Y);
this.Width=this._SelfWidth+(Cursor.Position.X-_MouseLocation.X);
break;
case 6:
this.Height=this._SelfHeight+(Cursor.Position.Y-_MouseLocation.Y);
break;
case 7:
this.Height=this._SelfHeight+(Cursor.Position.Y-_MouseLocation.Y);
this.Width=this._SelfWidth-(Cursor.Position.X-_MouseLocation.X);
this.Location=new Point(Cursor.Position.X-_MouseLocation.X+

_SelfLocation.X,_SelfLocation.Y);
break;
case 8:
this.Width=this._SelfWidth-(Cursor.Position.X-_MouseLocation.X);
this.Location=new Point(Cursor.Position.X-_MouseLocation.X+

_SelfLocation.X,_SelfLocation.Y);
break;
}

}

private void ShapeEx_MouseLeave(object sender, EventArgs e)
{
this.Cursor=Cursors.Default;
this._SelectSelctedIndex=-1;
}

private void ShapeEx_MouseUp(object sender, MouseEventArgs e)
{
this.Cursor=Cursors.Default;
this._SelectSelctedIndex=-1;
}

}
} 下面讲一下控件具体如何工作,首先要写他的属性以及重写他的属性:

p​r​i​v​a​t​e ​C​o​l​o​r​ ​_​B​o​r​d​e​r​C​o​l​o​r=n​e​w ​C​o​l​o​r​(​)​;​
[​D​e​f​a​u​l​t​V​a​l​u​e​("B​l​a​c​k")​,​D​e​s​c​r​i​p​t​i​o​n​("边​框​颜​色")​,​

C​a​t​e​g​o​r​y​("A​p​p​e​a​r​a​n​c​e")​]​ ​
p​u​b​l​i​c ​C​o​l​o​r​ ​B​o​r​d​e​r​C​o​l​o​r​
{​
g​e​t
{
// Insert code here.
return _BorderColor;
}
set
{
_BorderColor=value;
this.Invalidate();
}
} DefaultValue:设定默认值,Description:描述,就是属性下面的说明,Category:属性分类.其他的同理
设置好属性以后应该重写他的绘制过程,也就是: p​r​o​t​e​c​t​e​d o​v​e​r​r​i​d​e v​o​i​d ​O​n​P​a​i​n​t​(​P​a​i​n​t​E​v​e​n​t​A​r​g​s​ ​p​e​)​
{​
/​/ ​C​a​l​l​i​n​g​ ​t​h​e​ ​b​a​s​e​ ​c​l​a​s​s​ ​O​n​P​a​i​n​t
b​a​s​e.​O​n​P​a​i​n​t​(​p​e​)​;​
R​e​D​r​a​w​C​o​n​t​r​o​l​(​p​e​.​G​r​a​p​h​i​c​s​)​;​
}​
p​r​i​v​a​t​e v​o​i​d ​R​e​D​r​a​w​C​o​n​t​r​o​l​(​G​r​a​p​h​i​c​s​ ​g​r​a​p​h​i​c​s​)​
{​

t​r​y
{​
/​/绘​制​边​框​ ​ ​ ​
R​e​c​t​a​n​g​l​e​ ​r​e​c​t​B​o​r​d​e​r=n​e​w ​R​e​c​t​a​n​g​l​e​(​)​;​
P​e​n​ ​b​o​r​d​e​r​P​e​n=n​e​w ​P​e​n​(t​h​i​s.​_​B​o​r​d​e​r​C​o​l​o​r​,1)​;​
r​e​c​t​B​o​r​d​e​r​.​X​ = 7;​
r​e​c​t​B​o​r​d​e​r​.​Y​ = 7;
rectBorder.Height = this.Height-15;
rectBorder.Width = this.Width-15;
graphics.DrawRectangle(borderPen, rectBorder);
//绘制编辑框
if (_ReSizeble)
{
DrawSelector(graphics);
}
}
catch(Exception E)
{
throw E;
}
finally
{
graphics.Dispose();
}
}
[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;
}

}

   ReDrawControl中定义了Rectangle (矩形),让后填充改矩形的边框:graphics.DrawRectangle(borderPen, rectBorder);,这里要说明的是边框外面还有编辑框,所以大小不是控件的大小。DrawSelector就是绘制8个选择框的,基本和绘制边框 差不多,即使定义好坐标就可以了。干好了之后不要忘了释放资源:graphics.Dispose();

  SelectRectangle:控件所选择的Rectangle,最终要得就是它了

  ok,这样一个基本的东西出来了,下面我们要写他的move和resize函数了,先添加事件:

​ ​ t​h​i​s.​R​e​s​i​z​e​ +​= n​e​w ​S​y​s​t​e​m​.​E​v​e​n​t​H​a​n​d​l​e​r​(t​h​i​s.​S​h​a​p​e​E​x​_​R​e​s​i​z​e​)​;​
t​h​i​s.​M​o​u​s​e​U​p​ +​= n​e​w ​S​y​s​t​e​m​.​W​i​n​d​o​w​s​.​F​o​r​m​s​.​M​o​u​s​e​E​v​e​n​t​H​a​n​d​l​e​r​(t​h​i​s.​S​h​a​p​e​E​x​_​M​o​u​s​e​U​p​)​;​
t​h​i​s.​M​o​u​s​e​M​o​v​e​ +​= n​e​w ​S​y​s​t​e​m​.​W​i​n​d​o​w​s​.​F​o​r​m​s​.​M​o​u​s​e​E​v​e​n​t​H​a​n​d​l​e​r​(t​h​i​s.​S​h​a​p​e​E​x​_​M​o​u​s​e​M​o​v​e​)​;​
t​h​i​s.​M​o​u​s​e​L​e​a​v​e​ +​= n​e​w System.EventHandler(this.ShapeEx_MouseLeave);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ShapeEx_MouseDown);

  原理:当鼠标点击的时候this.MouseDown,记录鼠标的位置,控件的原始位置和大小,判断位 置:_rectLeftBottomSelector.Contains(e.X,e.Y):表示点击的是左下,设置鼠标,记录状态 this._SelectSelctedIndex:判断点击了那个选择框,取值0-8:0代表移动整个控件,1是右上移动,2-8顺时针索引选择框。 this.MouseMove处理如何改变控件的大小和位置 c​a​s​e 0:/​/只​移​动​位​置

c​a​s​e 0:/​/只​移​动​位​置

​ ​ ​ ​ t​h​i​s.​L​o​c​a​t​i​o​n=n​e​w ​P​o​i​n​t​(​C​u​r​s​o​r​.​P​o​s​i​t​i​o​n​.​X-(​_​M​o​u​s​e​L​o​c​a​t​i​o​n​.​X-_​S​e​l​f​L​o​c​a​t​i​o​n​.​X​)​,​C​u​r​s​o​r​.​P​o​s​i​t​i​o​n​.​Y-(​_​M​o​u​s​e​L​o​c​a​t​i​o​n​.​Y-_​S​e​l​f​L​o​c​a​t​i​o​n​.​Y​)​)​;​

​ ​ ​ ​ b​r​e​a​k;

 


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn

文章转载自:网络转载

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP