Using system;
Using system. collections;
Using system. componentmodel;
Using system. drawing;
Using system. Drawing. drawing2d;
Using system. Data;
Using system. Windows. forms;
Namespace lineargradientbuttonlib
{
/// <Summary>
/// Summary of usercontrol1.
/// </Summary>
Public class lineargradientbutton: system. Windows. Forms. Button
{
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL;
Private color frontcolor; // The foreground color of the button gradient.
Private color backcolor; // the background color of the button gradient.
Private bool isuseangle; // whether to use Angle
Private float angle; // sets the angle.
Private lineargradientmode mode; // sets the direction of the linear gradient.
Private stringalignment textlinealignment; // sets the horizontal alignment of button text.
Private stringalignment textvertalignment; // sets the vertical alignment of button text.
Private hatchstyle; // sets the filling pattern of the text.
Private bool isusehatchstyle; // you can specify whether to fill the pattern.
Public lineargradientbutton ()
{
// This call is required by the windows. Forms Form Designer.
Initializecomponent ();
This. setstyle (controlstyles. userpaint | controlstyles. allpaintinginwmpaint |
Controlstyles. doublebuffer, true );
// Todo: add any initialization after the initcomponent call
}
[Description ("set the foreground color of the button gradient "),
Category ("appearance "),
System. componentmodel. refreshproperties (refreshproperties. repaint)]
Public color frontcolor
{
Get
{
If (frontcolor = color. Empty)
{
Frontcolor = color. Black;
}
Return frontcolor;
}
Set
{
Frontcolor = value;
}
}
[Description ("set the background color of the button gradient "),
Category ("appearance "),
System. componentmodel. refreshproperties (refreshproperties. repaint)]
Public color backgroundcolor
{
Get
{
If (backcolor = color. Empty)
{
Backcolor = color. White;
}
Return backcolor;
}
Set
{
Backcolor = value;
}
}
[Defaultvalue (false), description ("whether the worker sets the gradient angle")]
Public bool useangle
{
Get
{
Return isuseangle;
}
Set
{
Isuseangle = value;
}
}
[Description ("set gradient angle"), category ("appearance "),
System. componentmodel. refreshproperties (refreshproperties. repaint)]
Public float angle
{
Get
{
/*
While (angle <0)
{
Angle + = 360;
}
While (angular> 360)
{
Angle-= 360;
}
*/
Return angle;
}
Set
{
Angle = value;
}
}
/*
[Defaultvalue (stringalignment. Center), description ("sets the horizontal alignment of text"), category ("appearance")]
Public stringalignment textlinealignment
{
Get
{
Return textlinealignment;
}
Set
{
Value = textlinealignment;
}
}
[Defaultvalue (stringalignment. Center), description ("set vertical alignment of text"), category ("appearance")]
Public stringalignment textvertalignment
{
Get
{
Return textvertalignment;
}
Set
{
Value = textvertalignment;
}
}
*/
[Defaultvalue (false), description ("whether to fill text with pattern "),
System. componentmodel. refreshproperties (refreshproperties. All)]
Public bool usehatchstyle
{
Get
{
Return isusehatchstyle;
}
Set
{
Isusehatchstyle = value;
}
}
[Defaultvalue (0), description ("When useangle = false, set the Gradient Direction"), category ("appearance "),
System. componentmodel. refreshproperties (refreshproperties. repaint)]
Public lineargradientmode Mode
{
Get
{
Return mode;
}
Set
{
Mode = value;
}
}
[Defaultvalue (1), description ("set the pattern to be filled in text "),
Category ("appearance "),
System. componentmodel. refreshproperties (refreshproperties. repaint)]
Public hatchstyle fillstyle
{
Get
{
Return hatchstyle;
}
Set
{
Hatchstyle = value;
}
}
/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
Components. Dispose ();
}
Base. Dispose (disposing );
}
# 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 ()
{
Components = new system. componentmodel. Container ();
}
# Endregion
// Use the angle redraw button
Private void drawbuttonwithangle (Graphics g)
{
Lineargradientbrush LGB
= New lineargradientbrush (
New rectangle (0, 0, this. Width, this. Height ),
Frontcolor, backcolor, Angle
);
G. fillrectangle (LGB, 0, 0, this. Width, this. Height );
LGB. Dispose ();
}
// Fill the button in gradient mode
Private void drawbuttonwithmode (Graphics g, lineargradientmode Mode)
{
Lineargradientbrush LGB
= New lineargradientbrush (
New rectangle (0, 0, this. Width, this. Height ),
Frontcolor, backcolor, mode );
G. fillrectangle (LGB, 0, 0, this. Width, this. Height );
LGB. Dispose ();
}
// No graphic text
Private void drawbuttontext (Graphics g)
{
Stringformat Sf = new stringformat ();
// SF. linealignment = textlinealignment;
// SF. Alignment = textvertalignment;
SF. linealignment = stringalignment. Center;
SF. Alignment = stringalignment. Center;
G. drawstring (this. text, this. Font,
New solidbrush (this. forecolor ),
New rectangle (0, 0, this. Width, this. Height ),
SF );
}
// Draw text with a pattern
Private void drawbuttontext (Graphics g, hatchstyle HS)
{
Stringformat Sf = new stringformat ();
// SF. linealignment = textlinealignment;
// SF. Alignment = textvertalignment;
SF. linealignment = stringalignment. Center;
SF. Alignment = stringalignment. Center;
G. drawstring (this. text, this. Font,
New hatchbrush (HS, this. forecolor, color. Aquamarine ),
New rectangle (0, 0, this. Width, this. Height ),
SF );
}
// Override the onpaint event
Protected override void onpaint (painteventargs PEA)
{
Graphics G = pea. graphics;
Base. onpaint (PEA );
If (useangle)
Drawbuttonwithangle (g );
Else
Drawbuttonwithmode (G, mode );
If (usehatchstyle)
Drawbuttontext (G, fillstyle );
Else
Drawbuttontext (g );
}
}
}