c#自定义控件开发实例(1)

控件|控件开发

源文件: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,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;
}

}
}

时间: 2024-11-01 03:38:38

c#自定义控件开发实例(1)的相关文章

c#自定义控件开发实例

控件|控件开发 最近做一个图象的采集,需要一个图形的选择控件,但是在.net下没有类似vb中的shape控件,所以考虑了自己写一个控件. 下面我将从头创建控件,这个控件主要是用来选择图形的Rectangle,有一下几个属性Color BorderColor:边框颜色,Color BackColor:背景颜色,bool ReSizeble:是否可移动, Rectangle SelectRectangle:选择区域.打开vs2003(我用的这个版本),新建一个c#控件库,ok,拷贝如下代码到你的代码

c#自定义控件开发实例(2)

控件|控件开发 源文件:http://ded.nuaa.edu.cn/download/Windows%20Extended%20Controls.rar 示例代码:http://ded.nuaa.edu.cn/download/WindowsApplication6.rar 下面讲一下控件具体如何工作,首先要写他的属性以及重写他的属性,private Color _BorderColor=new Color(); [DefaultValue("Black"),Description(

WPF 自定义雷达图开发实例教程_C#教程

自定义雷达图表如下: 1.创建UserControl,名为"RadarChartControl" 前台: <UserControl x:Class="WpfApplication2.RadarChartControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/win

基于SharpMap扩展程序开发实例

SharpMap是基于.Net平台开发的GIS地图渲染组件.在SharpMap的内部设计了基于OGC 标准的几何模型 构架,设计了IProvider策略模式的多源矢量地图数据适配器接口,地图要素渲染的底层主要通过几何变 换将Geometry转换为.Net支持的几何模型如System.Drawing.Point.System.Drawing.Rectangle. System.Drawing.Drawing2D.GraphicsPath等,然后调用System.Drawing.Graphics类的

使用ADO封装类的数据库程序开发实例(上)

源代码运行效果图如下: 开发实例(上)-vc数据库编程实例ado"> 一.前言 用过ADO的人都知道, 调用ADO要处理很多"麻烦"的事情,如异常处理等,要写很多try - catch块. 有点不甚其烦.我干脆把常用的函数都封装起来,免去老是要写try - catch块的麻烦.做起来虽然没有什么技术含量,但也比较烦琐,所以只完成了一部分,且由于时间及个人水平有限,没有对封装的东西作全面测试,并必定有很多错误,但想到对某些朋友可能有用.所以先"捐"出来

php配置sqlite数据库开发实例

百度搜索下载SqLiteManager工具 PHP5已经绑定sqlite 1.手动添加的php的pdo的驱动扩展支持 ,在PHP.ini添加 extension=php_pdo.dll extension=php_pdo_sqlite.dll extension=php_sqlite.dll extension_dir = "C:\Program Files\Apache Group\php5\ext" 2.在C:\Program Files\Apache Group\php5\ext

Flash 8新特性开发实例教程

教程 flash 8 新特性的 API 开发实例教程-flash动画实例教程"> 看看上图,flash8特性的API主要集中在下面4块1 图形效果[Filters]多种和滤镜有关的类,可以将阴影,模糊这些特效加到你的 mc上.[Geometry Classes]提供了 Matrix, Point, Rectangle and ColorTransforms 等各种颜色变换效果[MovieClip Extensions ]除了可以让mc可以利用Filter,geometry外.还提供了 [B

标准MFC WinSock ActiveX控件开发实例(II)高级篇

回顾:在上一篇文章<标准MFC WinSock ActiveX控件开发实例>中我们详细介绍了控件的开发过程,以及接口和事件的 添加和响应方法.现在我们将继续上次没有写完的控件继续进行开发,并完善作为一个WinSock控件应该具备的功能. 二.按照前一篇文章提到的知识,现在我们来添加两个新的接口分别是SendData()和GetData(),它们看起来如下: //网络数据发送,在指定的超时时间内进行发送然后返回,成功返回实际发送字节数,否则返回负数 long CMFCWinSockCtrl::S

ExtJS与.NET结合开发实例(Grid之批量删除篇)

上接ExtJS与.NET结合开发实例(Grid之数据显示.分页.排序篇),在此基础 上实现批量删除功能. 实现的步骤如下: 1. 用WebService实现 删除的功能(上篇有一园友提出用WebService实现,这里顺便说一下,取数据源 也可以用WebService,大家可以参考删除的WebService自行实现,我这里就不在 累述了) 新建一WebService文件,命名为:DeleteProject.asmx 代 码如下: DeleteProject.asmx.cs 1using Syst