一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

C# WinForm自定義控件開發(fā)實(shí)例

 天涯1990 2013-06-06

最近做一個(gè)圖象的采集,需要一個(gè)圖形的選擇控件,但是在.net下沒有類似vb中的shape控件,所以考慮了自己寫一個(gè)控件。

下面我將從頭創(chuàng)建控件,這個(gè)控件主要是用來選擇圖形的Rectangle,有一下幾個(gè)屬性Color BorderColor:邊框顏色,Color BackColor:背景顏色,bool ReSizeble:是否可移動(dòng), Rectangle SelectRectangle:選擇區(qū)域。
打開vs2003(我用的這個(gè)版本),新建一個(gè)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>
/// 必需的設(shè)計(jì)器變量。
/// </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()
{
// 該調(diào)用是 Windows.Forms 窗體設(shè)計(jì)器所必需的。
InitializeComponent();

// TODO: 在 InitComponent 調(diào)用后添加任何初始化

}
[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("運(yùn)行中控件大小是否可拖拽編輯"),Category("Behavior")]
public bool ReSizeble
{
get
{
// Insert code here.
return _ReSizeble;
}
set
{
_ReSizeble=value;
this.Invalidate();
}
}
[Description("控件選擇區(qū)域"),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
{
//實(shí)心

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 組件設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器
/// 修改此方法的內(nèi)容。
/// </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;
}

}
}
下面講一下控件具體如何工作,首先要寫他的屬性以及重寫他的屬性:
private Color _BorderColor=new Color();
[DefaultValue("Black"),Description("邊框顏色"),Category("Appearance")]
public Color BorderColor
{
get
{
// Insert code here.
return _BorderColor;
}
set
{
_BorderColor=value;
this.Invalidate();
}
}
DefaultValue:設(shè)定默認(rèn)值,Description:描述,就是屬性下面的說明,Category:屬性分類.其他的同理
設(shè)置好屬性以后應(yīng)該重寫他的繪制過程,也就是:
protected override void OnPaint(PaintEventArgs pe)
{
// Calling the base class OnPaint
base.OnPaint(pe);
ReDrawControl(pe.Graphics);
}
private void ReDrawControl(Graphics graphics)
{

try
{
//繪制邊框
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();
}
}
[Description("控件選擇區(qū)域"),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個(gè)選擇框的,基本和繪制邊框差不多,即使定義好坐標(biāo)就可以了。干好了之后不要忘了釋放資源:graphics.Dispose();

SelectRectangle:控件所選擇的Rectangle,最終要得就是它了

ok,這樣一個(gè)基本的東西出來了,下面我們要寫他的move和resize函數(shù)了,先添加事件:

this.Resize += new System.EventHandler(this.ShapeEx_Resize);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.ShapeEx_MouseUp);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.ShapeEx_MouseMove);
this.MouseLeave += new System.EventHandler(this.ShapeEx_MouseLeave);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ShapeEx_MouseDown);
原理:當(dāng)鼠標(biāo)點(diǎn)擊的時(shí)候this.MouseDown,記錄鼠標(biāo)的位置,控件的原始位置和大小,判斷位置:_rectLeftBottomSelector.Contains(e.X,e.Y):表示點(diǎn)擊的是左下,設(shè)置鼠標(biāo),記錄狀態(tài)this._SelectSelctedIndex:判斷點(diǎn)擊了那個(gè)選擇框,取值0-8:0代表移動(dòng)整個(gè)控件,1是右上移動(dòng),2-8順時(shí)針?biāo)饕x擇框。this.MouseMove處理如何改變控件的大小和位置
case 0://只移動(dòng)位置
this.Location=new Point(Cursor.Position.X-(_MouseLocation.X-_SelfLocation.X),Cursor.Position.Y-(_MouseLocation.Y-_SelfLocation.Y));
break;
1,5不僅移動(dòng)位置,還改變大小,2,3,4,6,7,8只改變大小

其他則是清理工作。

好了,那么趕緊編譯。生成就可以了。

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多

    91精品国产品国语在线不卡| 九九热在线视频观看最新| 国产欧美日韩精品自拍| 国产黄色高清内射熟女视频| 亚洲综合天堂一二三区| 欧美精品亚洲精品一区| 久热在线视频这里只有精品| 欧美一区二区日韩一区二区| 日本在线视频播放91| 国产精品尹人香蕉综合网| 暴力三级a特黄在线观看| 国产精品久久女同磨豆腐| 国产日韩欧美一区二区| 99久久国产精品亚洲| 五月婷婷综合激情啪啪| 免费福利午夜在线观看| 欧美整片精品日韩综合| 午夜精品久久久免费视频| 欧美精品一区二区三区白虎| 在线观看欧美视频一区| 久久大香蕉精品在线观看| 婷婷色国产精品视频一区| 免费在线成人午夜视频| 国产日韩欧美国产欧美日韩| 九九热这里只有免费精品| 久久热在线视频免费观看| 日韩人妻毛片中文字幕| 午夜资源在线观看免费高清| 亚洲国产香蕉视频在线观看| 高清一区二区三区四区五区| 91久久精品中文内射| 亚洲欧美日本国产有色| 日韩欧美中文字幕av| 亚洲欧美日韩精品永久| 小草少妇视频免费看视频| 激情丁香激情五月婷婷| 国产亚州欧美一区二区| 99久久精品午夜一区| 中文字幕五月婷婷免费 | 欧美一区二区不卡专区| 熟女中文字幕一区二区三区|