C # Custom Control Development Instance (2)

Source: Internet
Author: User
Tags border color 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

Let's talk about how the control works, first by writing his attributes, and by overriding his attributes,
Private color _bordercolor=new color ();
[DefaultValue ("Black"), Description ("Border Color"), Category ("appearance")]
Public Color BorderColor
{
Get
{
Insert code here.
return _bordercolor;
}
Set
{
_bordercolor=value;
This. Invalidate ();
}
}
DefaultValue: Set default value, Description: Description, is the description of the attribute, Category: attribute classification. The other same thing.
When you set a property, you should rewrite his drawing process, which is
protected override void OnPaint (PaintEventArgs pe)
{
Calling the base class OnPaint
Base. OnPaint (PE);
Redrawcontrol (PE. Graphics);
}
private void Redrawcontrol (Graphics Graphics)
{

Try
{
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 ();
}
}
[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;
}

}

The rectangle (rectangle) is defined in Redrawcontrol, so that the rectangle is filled with a rectangular border: graphics. DrawRectangle (Borderpen, rectborder); Here's what you're going to say. There is also an edit box outside the border, so the size is not the size of the control. Drawselector is to draw 8 selection boxes, basic and draw the border almost, even if the definition of good coordinates. Don't forget to release the resource after you've done it: graphics. Dispose ();

Selectrectangle: The rectangle that the control chooses, it's the end of it.

OK, so a basic thing came out, and here we're going to write his move and resize functions, add the event first:


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);
Principle: When the mouse clicks this. MouseDown, record the position of the mouse, the original position and size of the control, determine the location: _rectleftbottomselector.contains (E.X,E.Y): Click on the left, set the mouse, record status this._ Selectselctedindex: Judge clicks on the selection box, takes a value of 0-8:0 means to move the entire control, 1 is to move the top right, 2-8 clockwise index selection box. This. MouseMove How to change the size and position of a control
Case 0://only Move position
This. Location=new Point (cursor.position.x-(_mouselocation.x-_selflocation.x), cursor.position.y-(_MouseLocation.Y-_ SELFLOCATION.Y));
Break
1,5 not only move the position, but also change the size, 2,3,4,6,7,8 only change the size

Others are clean-up work.

Well, then hurry up and compile. Build on it.




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.