[C #]-custom controls (including source code)-rounded panel

Source: Internet
Author: User

Rounded panelCode

 

  Using  System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. diagnostics;
Using System. LINQ;
Using System. text;

Namespace Mycontrollibrary
{
Public Partial Class Roundpanel: system. Windows. Forms. Panel
{
Public Roundpanel ()
{
Initializecomponent ();

This . Padding = New System. Windows. Forms. Padding ( 0 , 0 , 0 , 0 );
This . Margin = New System. Windows. Forms. Padding ( 0 , 0 , 0 , 0 );
This . Backgroundimagelayout = System. Windows. Forms. imagelayout. Stretch;
}


// Rounded corner
// ========================================================== ========================================================== ====================
Private Int _ Radius; // Corner radian

/// <Summary> Rounded corner radians (0 indicates not rounded corner) </Summary>
[Browsable ( True )]
[Description ( " Rounded corner radians (0 indicates not rounded corner) " )]
Public Int _ Setroundradius
{
Get
{
Return _ Radius;
}
Set
{
If (Value < 0 ) {_ Radius = 0 ;}
Else {_ Radius = Value ;}
Base . Refresh ();
}
}


// Rounded corner code
Public Void Round (system. Drawing. region Region)
{
// Bytes -----------------------------------------------------------------------------------------------
// It is already the easiest way to modify the form Attributes provided by. Net (You have to call the API yourself before)
System. Drawing. drawing2d. graphicspath opath = New System. Drawing. drawing2d. graphicspath ();
Int X = 0 ;
Int Y = 0 ;
Int Thiswidth = This . Width;
Int Thisheight = This . Height;
Int Angle = _ Radius;
If (Angle > 0 )
{
System. Drawing. Graphics g = Creategraphics ();
Opath. addarc (X, Y, angle, angle, 180 , 90 ); // Upper left corner
Opath. addarc (thiswidth - Angle, Y, angle, angle, 270 , 90 ); // Upper right corner
Opath. addarc (thiswidth - Angle, thisheight - Angle, 0 , 90 ); // Bottom right corner
Opath. addarc (x, thisheight - Angle, 90 , 90 ); // Lower left corner
Opath. closeallfigures ();
Region = New System. Drawing. region (opath );
}
// Bytes -----------------------------------------------------------------------------------------------
Else
{
Opath. addline (x + Angle, Y, thiswidth - Angle, y ); // Top
Opath. addline (thiswidth, y + Angle, thiswidth, thisheight - Angle ); // Right side
Opath. addline (thiswidth - Angle, thisheight, X + Angle, thisheight ); // Bottom edge
Opath. addline (X, Y + Angle, X, thisheight - Angle ); // Left
Opath. closeallfigures ();
Region = New System. Drawing. region (opath );
}
}
// ========================================================== ========================================================== ====================


Public Roundpanel (icontainer container)
{
Container. Add ( This );

Initializecomponent ();
}

Protected Override Void Onpaint (system. Windows. Forms. painteventargs PE)
{
Base . Onpaint (PE );
Round ( This . Region ); // Rounded corner
}

Protected Override Void Onresize (eventargs)
{
Base . Onresize (eventargs );
Base . Refresh ();
}
}
}

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.