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

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

最近做一个图象的采集,需要一个图形的选择控件,但是在.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{ /// /// 控件 /// public class ShapeEx : System.Windows.Forms.Control { /// /// 必需的设计器变量。 /// /// 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(\边框颜色

\ public Color BorderColor { get { // Insert code here. return

_BorderColor; } set { _BorderColor=value; this.Invalidate(); } }

[DefaultValue(\背景颜色\ public override Color BackColor { get { // Insert code here.

return _BackColor; } set { _BackColor=value;

this.Invalidate(); } }

[DefaultValue(false),Description(\运行中控件大小是否可拖拽编辑\ public bool ReSizeble { get { // Insert code here. return _ReSizeble; } set { _ReSizeble=value; this.Invalidate(); } } [Description(\控件选择区域\ 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);

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4