Draw focus selection box

Source: Internet
Author: User
Recently, I want to prepare a custom control for a project. I need to know more about drawing. Fortunately, the codeproject has such an article drawtools, it provides me with a lot of sample code to learn.

The draw focus selection box for the first thing to learn.

I thought it would be like drawing a box to draw, and I don't want GDI to have a special method for use.

Controlpaint. drawfocusrectangle (E. Graphics, this. focusrectangle, color. Black, color. Transparent );

The following is part of the code for the usercontrol of the responsibility plotting work.

Code generated by the component designer # code generated by the region component designer
/** // <Summary>
/// The designer supports the required methods-do not use the code editor
/// Modify the content of this method.
/// </Summary>
Private void initializecomponent ()
{
// Omit Part of the Code
This. mouseup + = new system. Windows. Forms. mouseeventhandler (this. drawarea_mouseup );
This. Paint + = new system. Windows. Forms. painteventhandler (this. drawarea_paint );
This. mousemove + = new system. Windows. Forms. mouseeventhandler (this. drawarea_mousemove );
This. mousedown + = new system. Windows. Forms. mouseeventhandler (this. drawarea_mousedown );
This. resumelayout (false );

}
# Endregion

Private point startpoint;
Private rectangle focusrectangle;
Private bool bdrawfocusrectangle;

Public void Init (bool isfill)
{
Setstyle (controlstyles. allpaintinginwmpaint |
Controlstyles. userpaint | controlstyles. doublebuffer, true );
If (isfill)
This. Dock = dockstyle. Fill;
}

/** // <Summary>
/// Obtain the drawn box.
/// We may choose from left to right, from top to bottom, or from bottom to top, from right to left.
/// Some programs need to be judged.
/// </Summary>
/// <Param name = "X1"> </param>
/// <Param name = "Y1"> </param>
/// <Param name = "X2"> </param>
/// <Param name = "Y2"> </param>
/// <Returns> </returns>
Public static rectangle getnormalizedrectangle (INT X1, int Y1, int X2, int Y2)
{
If (X2 <X1)
{
Int TMP = x2;
X2 = x1;
X1 = TMP;
}

If (Y2 <Y1)
{
Int TMP = Y2;
Y2 = Y1;
Y1 = TMP;
}

Return new rectangle (x1, Y1, x2-X1, Y2-Y1 );
}

Private void drawarea_mousemove (Object sender, system. Windows. Forms. mouseeventargs E)
{
If (E. Button = mousebuttons. Left)
{
This. bdrawfocusrectangle = true;
This. focusrectangle = drawarea. getnormalizedrectangle (this. startpoint. X, this. startpoint. Y, E. X, E. y );
This. Refresh (); // redraw
}
}

Private void drawarea_mousedown (Object sender, system. Windows. Forms. mouseeventargs E)
{
This. startpoint = new point (E. X, E. y );
}

Private void drawarea_paint (Object sender, system. Windows. Forms. painteventargs E)
{
If (this. bdrawfocusrectangle)
Controlpaint. drawfocusrectangle (E. Graphics, this. focusrectangle, color. Black, color. Transparent );
}

Private void drawarea_mouseup (Object sender, system. Windows. Forms. mouseeventargs E)
{
This. bdrawfocusrectangle = false;
This. Refresh ();
}

Use this control in Form

Private void form1_load (Object sender, system. eventargs E)
{
This. drawarea1.init (true );
}
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.