C # Custom Control Development Instance (1)

Source: Internet
Author: User
Tags bool border color contains rar
Control | Control development source file: Http://ded.nuaa.edu.cn/download/Windows%20Extended%20Controls.rar
Sample code: Http://ded.nuaa.edu.cn/download/WindowsApplication6.rar

A recent image acquisition requires a graphical selection control, but there is no shape control in. NET similar to VB, so consider writing a control yourself.

I'm going to create the control from scratch, which is mainly used to select the rectangle of the graphic, with a few properties color bordercolor: Border color, color BackColor: Background color, bool resizeble: whether removable, Rectangle selectrectangle: Select the area.
Open vs2003 (this version I use), create a new C # Control Library, OK, and copy the following code into your code.

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Drawing;
Using System.Data;
Using System.Windows.Forms;

Namespace Windowsextendedcontrols
{
<summary>
Control
</summary>
public class ShapeEx:System.Windows.Forms.Control
{
<summary>
The required designer variable.
</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 ()
{
This call is required for the Windows.Forms form designer.
InitializeComponent ();

TODO: Add any initialization after the initcomponent call

}
[DefaultValue ("Black"), Description ("Border Color"), Category ("appearance")]
Public Color BorderColor
{
Get
{
Insert code here.
return _bordercolor;
}
Set
{
_bordercolor=value;
This. Invalidate ();
}
}
[DefaultValue ("Control"), Description ("Background color"), Category ("appearance")]
public override Color BackColor
{
Get
{
Insert code here.
return _backcolor;
}
Set
{
_backcolor=value;
This. Invalidate ();
}
}
[DefaultValue (False), Description ("control size can be dragged and edited in Run"), Category ("Behavior")]
public bool Resizeble
{
Get
{
Insert code here.
return _resizeble;
}
Set
{
_resizeble=value;
This. Invalidate ();
}
}
[Description ("Control selection Area"), 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
{
Solid

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);
Border
_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
{

Draw background
/*
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);
*/
Draw Border
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);
Draw an edit box
if (_resizeble)
{
Drawselector (graphics);
}
}
catch (Exception E)
{
Throw E;
}
Finally
{
Graphics. Dispose ();
}
}
<summary>
Clean up all resources that are in use.
</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);
}

Code generated #region Component Designer
<summary>
Designer supports the desired method-do not use the Code Editor
Modify the contents of this method.
</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&LT;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;
}

}
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.