首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 操作系统-> Windows 2003教程
c#自定义控件开发实例(1)-.NET教程,C#语言
作者:网友供稿 点击:690
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[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;
}

}
}


文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·c#自定义控件开发实例(1)-.NET教程,C#语言
·为DataGrid添加CheckBox控件-.NET教程,组件控件开发
·windows 2003 64位正式版抢先体验
·显示你个性的鼠标指针-网页设计,HTML/CSS
·水晶报表中子报表的数据绑定问题-.NET教程,报表/图形/Office
·windows 2003组策略和安全模板的应用
·快速关闭windows 2003的三种方法
·数据库设计范式(实例)-数据库专栏,SQL Server
·windows 2003服务器防止海洋木马的安全设置
·《用C#和数据库实现无限级分类法》修正程序-.NET教程,C#语言
最新文章
·对.net framework 反射的反思_asp.net技巧
·google工具条要跟pagerank说再见?_google推广
·windows vista为什么不允许两个用户同时登录_windows vista
·田锋林:seo博客细节调整_seo网站优化
·将ssh与php相连接 确保传输数据的安全_php技巧
·photoshop照片合成实例:添加烟花_photoshop教程
·深入了解flash as中的setinterval方法_flash教程
·feedburner的rss广告收益分析_网赚技巧
·如何允许google的抓取工具访问我的需要登录的网页_网赚技巧
·王通:adsense出现的一些严重问题_网赚技巧
相关主题
  • C#自定义控件制作篇-.NET教程,C#语言
  • c#自定义控件开发实例(2)-.NET教程,C#语言
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭