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