Creation of circular and elliptical button controls

Source: Internet
Author: User

This is what my friends often ask me. Is a non-square button, that is, the circular, elliptical button control

The first step is to create a Windows form control library and delete the usercontrol1.cs file.

Add a new project, select the component library, and name it ellipsebutton. CS. Add two events in ellipsebutton. Designer. CS.

The control length and text change events are displayed respectively. Add header files

Using system. Windows. forms;
Using system. Drawing. drawing2d;

Code
/// <Summary>
/// The designer supports the required methods-do not
/// Use the code editor to modify the content of this method.
/// </Summary>
Private void initializecomponent ()
{
Components = new system. componentmodel. Container ();
This. suspendlayout ();
This. Resize + = new system. eventhandler (this. ellipsebutton_resize );
This. textchanged + = new system. eventhandler (this. ellipsebutton_textchanged );
}

Switch to the Code view and add the following code:

 

Code
/// Modify component to Inherit System. Windows. Forms. Button
Public partial class ellipsebutton: system. Windows. Forms. Button
{
Private color startcolor = color. blue;
// Private color costartcolor = color. White;
Private color endcolor = color. greenyellow;
[Description ("set the starting color of the gradient"), category ("appearance")] // watch
Public color startcolor
{
Get
{Return startcolor;
}
Set
{
Startcolor = value;
Repaint ();
}
}
[Description ("set the ending color of the gradient"), category ("appearance")]
Public color endcolor
{
Get
{
Return endcolor;
}
Set
{
Endcolor = value;
Repaint ();
}

}
Public ellipsebutton ()
{
Initializecomponent ();
This. Length = 100;
This. Height = 100;
}
// Draw a circular area
Protected override void onpaint (painteventargs PE)
{
Base. onpaint (PE );

Graphics G = PE. graphics;
G. Clear (this. backcolor );
Rectangle rect = new rectangle (0, 0, this. Width, this. Height );
Lineargradientbrush mybrush = new lineargradientbrush (rect, startcolor, endcolor, lineargradientmode. forwarddiagonal );
G. fillellipse (mybrush, rect );
Mybrush. Dispose ();
Stringformat format = new stringformat ();
Format. linealignment = stringalignment. Center;
Format. Alignment = stringalignment. Center;
G. drawstring (this. Text, Font, new solidbrush (this. forecolor), rect, format );


}
// Re-draw the circular area
Private void repaint ()
{
Rectangle rect = new rectangle (0, 0, this. Width, this. Height );
Onpaint (New painteventargs (this. creategraphics (), rect ));
}
Private void ellipsebutton_resize (Object sender, system. eventargs E)
{
Repaint ();
}
Private void ellipsebutton_textchanged (Object sender, system. eventargs E)
{
Repaint ();
}

}
Author: freecomputer

 

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.