Author: willsound (willsound@163.com)
I usually prefer to use
Delphi
, Small birth, I like it.
Delphi
Usually develop (at least now)
Delphi
But I am not afraid that I have a joke, I am not used.
Delphi
Write the control, although I know, I always feel that I don't know how to start
:
L
But since I have contacted C #, which beautiful posture (code style), the wind is not indulging, and is better than Delphi, it is strong than Delphi. After a period of practice, I found that in development controls and components (other aspects, I don't dare to decompress), and it is simple to be refreshing. How, try it .j .j
Yes, my development platform is Windows 2000 Server .vs.Net official version
The control I implemented is followed by the Form Control Button, which can realize gradient background, realize pattern and texture fill text.
Ok, let's start.
1 First play vs.net
2 In the File menu, point to New, then select Project to open the New Project dialog. Select the "Windows Control Library" project template from the C # Projects list, and then type lineargradientButtonLib in the Name box, then click OK.
3 In the Solution Explorer, right-click UserControl1.cs and select View Code from the shortcut menu.
4 Locate the Class Statement Public Class UserControl1, change UserControl1 to LinearGradientButton to change the name of the component. Find constructor public userControl1 (), change it to public lineargradientButton ().
5 Change the control from System.Windows.Forms.userControl to System.Windows.Forms.Button in the Class statement. This allows inherited controls to inherit all functions of the Button control.
6 In the Solution Explorer, click UserControl1.cs and change the fileName property to lineargradientButton.cs in the Properties window.
Ok, I will tell the work now, the following work is to add attributes to our controls. Drink water, continue!
Plus the name space for use system.drawing.drawing2d;
1 Locate the Class statement. Type the following code on the back of {:
Private color frocolor; // Gradient foreground color
Private color backcolor; // Gradient background color
Private bool isusefloat; // Do not use angle transformation
Private float angle; // placement angle
Private lineargradientmode mode; // Set the angle of gradient
Private HatchStyle HatchStyle; / / Setting the filler pattern of the text
Private bool isusestyle; / / Set whether to fill the pattern with a pattern
Above these is the private domain we need, the following starts to do their corresponding properties for each private domain. In the above code, write the following code:
[Description ("Setting Button Gravity Prospect"), Category ("Appearance")]]]
Public color frontcolor {
get
{
Return frocolor;
}
set
{
Frocolor = Value;
}
}
[Description ("Set button gradient background color"), category ("APPEARANCE")]
Public Color BackgroundColor
{
get
{
Return backcolor;
}
set
{
Backcolor = Value;
}
}
[DefaultValue (false), Description ("Set whether to manually set angle")]
Public Bool Usefloat
{
get
{
Return isusefloat;
}
set
{
ISUSEFLOAT = VALUE;
}
}
[DefaultValue (false), Description ("Set whether to use the pattern fill text")]
Public Bool USTYLELELELELELELE
{
get
{
Return isusestyle;
}
set
{
ISUSESTYLE = VALUE;
}
}
[DefaultValue (0), Description ("Defines the angle of the gradient direction, the unit is measured from the x-axis."), Category ("Appearance")]
Public Float Angle
{
get
{
Return Angle;
}
set
{
Angle = Value;
}
}
[DefaultValue (0), Description ("When USEFLOAT is set to FALSE, set the gradient direction."), Category ("Appearance")]
Public LineargradientMode Mode
{
get
{
Return Mode;
}
set
{
Mode = Value;
}
}
[DefaultValue (false), Description ("Setting the text to populate the pattern"), category ("Appearance")]]
Public HatchStyle FillStyle
{
get
{
Return HatchStyle;
}
set
{
HatchStyle = Value;
}
}
Ok, we have designed the properties of the control. Here we want to write an event.
Because our control is to implement background gradient and text filled, the Override Paint event is completed.
In order to complete Override, the following preparations (written a few events used in the Paint event).
// Method for using an angle is gradually editing button
Private void DrawButtonwithangle (Graphics DBG)
{
Lineargradientbrush Brush = New Lineargradientbrush (New Rectangle (0,0, this.width, this.height), frocolor, backcolor, angle;
DBG.FillRectangle (Brush, 0,0, this.width, this.height);
Brush.dispose ();
}
//// Using mode method is gradually editing button
Private Void DrawButtonWithmode (Graphics DBG, LineargradientMode Mode)
{
LinearGradientBrush brush = new LinearGradientBrush (new Rectangle (0,0, this.Width, this.Height), froColor, backColor, Mode); dbg.FillRectangle (brush, 0,0, this.Width, this.Height);
Brush.dispose ();
}
// Heavy painting Button's text (Text), does not use the pattern fill
Private void DrawButtonTontext (Graphics DBG)
{
StringFormat Format = new stringFormat ();
Format.LineAlignment = stringalignment.center;
Format.Alignment = stringalignment.center;
Dbg.drawstring (this.text, this.font, new solidbrush (this.forecolor), New Rectangle (0,0, this.width, this.Height), Format;
}
// Override DrawButtonText function, make it possible to populate the text
Private Void DrawButtonTontext (Graphics DBG, HatchStyle HS)
{
StringFormat Format = new stringFormat ();
Format.LineAlignment = stringalignment.center;
Format.Alignment = stringalignment.center;
Dbg.drawstring (this.text, this.font, new hatchbrush (hs, this.forecolor, color.aquamarine), New Rectangle (0,0, this.width, this.height), format;
}
Ok, now start to override the Paint event.
Protected Override Void OnPaint (Painteventargs PE)
{
Graphics g = pe.graphics;
Base.onpaint (PE); // Call the method of parent control
If (isusefloat == true) / / If the angle control of the angle control gradient
DrawButtonwithangle (G);
IF (iSUSEFLOAT == FALSE)
DrawButtonWithmode (g, mode);
IF (iSuseStyle == True) / / If you use the pattern to fill the text
DrawButtonText (g, hatchstyle);
Else
DrawButtonText (g);
}
Ok, now I've got it, saved, generated.
Create test items
1. On the File menu, point to Add Project, and then click New Project to open the Add New Project dialog.
2. Select the Visual C # Project node and click Windows Applications.
3. Type Test in the Name box.
4. In the Solution Explorer, right-click the "Reference" node of the test item, then select Add Reference from the shortcut menu to display the Add Reference dialog.
5. Click the tab tagged as "Project".
6. Double-click the LinearGradientButtonLib project and note that the item appears in the Selected Component pane.
After adding a reference, new controls should be added to the toolbox. If your control has appeared in the toolbox, you should skip the next section.
Add controls to Toolbox
1. Right-click the Toolbox and select Custom Toolbox from the shortcut menu. The Custom Toolbox dialog opens.
2. Select the ".NET Framework Components" tab and click "Browse". Browse to the lineargradientbuttonlib / bin / debug folder and select LineargradientButtonLib.dll.
LineargradientButton appears in the Component list of Custom Toolbox dialogs.
3. In the Custom Toolbox dialog box, click the box next to LineargradientButton and close the window.
LineargradientButton is added to the selected toolbox tab.
Add controls to the form
1. In the Solution Explorer, right-click "Form1.cs" and select View Designer from the shortcut menu.
2. In the Toolbox, scroll down until the icon of the labeled lineargradientButton. Double-click the icon.
A "LineargradientButton" is displayed on the form.
3. Right-click "LineargradientButton" and select "Properties" from the shortcut menu.
4. Check the properties of the control in the Properties window. Note that they are the same as the standard buttons, and the difference is some of our own additions.
5. Set the foreground color and background colors of this control, then you can choose whether to fill the text, use the angle or use the system setting value to make a change in gradient angle.
6. Select "Start" from the Debug menu. FORM1 appears.
Who if you need a source of source, please send me a letter.