Write the control yourself in C #

Source: Internet
Author: User
Tags bool
Control to write controls by themselves in C #



Willsound (willsound@163.com)



Keywords

C#,.net, controls, GDI +



I usually prefer to use Delphi, the niche is not, I like the Delphi, peacetime development (at least now) more use Delphi, but not afraid of you master jokes, I did not use Delphi to write the control, although the principle of know, but always feel I do not know how to do: L

But since contact with C #, she which graceful posture (code style), coquettish but not indulgent character (to object-oriented embodiment is better, than the Delphi Strong), deeply touched me. After a period of practice, I found that in the development of controls and components (other aspects, the niche I dare not 妄断), its simplicity really makes me refreshing. What do you say, give it a try. J

Yes, my development platform is the official version of Windows Server+.vs.net.

The control I implemented is a step-by-step from the form control button that enables the gradient background to be implemented, and the pattern and texture fill text.

All right, let's start with the opening.

1 First, make a vs.net.

2 On the File menu, point to New, and then select Project to open the New Project dialog box. Select the Windows Control Library project template from the C # project list, and then type Lineargradientbuttonlib in the Name box, and then click OK.

3 In Solution Explorer, right-click UserControl1.cs and choose View Code from the shortcut menu.

4 Find the Class statement public class UserControl1, change the UserControl1 to Lineargradientbutton to alter the name of the component. Locate the constructor public UserControl1 () and change it to public Lineargradientbutton ().

5 in the Class statement, change the control from the System.Windows.Forms.UserControl inherited type to System.Windows.Forms.Button. This allows inherited controls to inherit all the functionality of the Button control.

6 In Solution Explorer, click UserControl1.cs, and in the Properties window, change the FileName property to LinearGradientButton.cs.

Well, now that the work is over, the next job is to add attributes to our controls. Drink water, go on!

First add the name space using System.Drawing.Drawing2D;

1 Find the class statement. Type the following code immediately after {:

Private Color Frocolor; Gradient foreground color

Private color backcolor;//Gradient background color

private bool isusefloat;//Whether use angle change

private float angle; Placement Angle

Private LinearGradientMode mode;//Set the angle of the gradient

Private Hatchstyle Hatchstyle; Set the fill pattern for text

private bool isusestyle;//Set whether to fill patterns with patterns



These are the private domains that our controls need, and the following starts with their corresponding properties for each private domain. Under the above code, write the following code:

[Description ("Set the foreground color of the button gradient"), Category ("appearance")]

Public Color Frontcolor

{

Get

{

return frocolor;

}

Set

{

Frocolor=value;

}

}

[Description ("Set the background color of the button gradient"), Category ("appearance")]

Public Color BackgroundColor

{

Get

{

return BackColor;

}

Set

{

Backcolor=value;

}

}

[DefaultValue (False), Description ("Set the angle Manually")]

public bool Usefloat

{

Get

{

return isusefloat;

}

Set

{

Isusefloat=value;

}

}

[DefaultValue (False), Description ("Set whether to fill text with pattern")]

public bool Usestyle

{

Get

{

return isusestyle;

}

Set

{

Isusestyle=value;

}

}

[DefaultValue (0), Description ("Defines the angle of the gradient direction, measured clockwise from the X axis)." "), Category (" appearance ")]

public float Angle

{

Get

{

return angle;

}

Set

{

Angle=value;

}

}

[DefaultValue (0), Description ("Set the gradient direction when Usefloat is set to false.) "), Category (" appearance ")]

Public LinearGradientMode Mode

{

Get

{

return mode;

}

Set

{

Mode=value;

}

}

[DefaultValue (False), Description ("Set the pattern to be filled with text"), Category ("appearance")]

Public Hatchstyle FillStyle

{

Get

{

return hatchstyle;

}

Set

{

Hatchstyle=value;

}

}

OK, so we've designed the properties of the control, and we're going to write the event here.


Because our control implements a background gradient and a text fill, override paint events to complete the painting itself.

In order to complete the override, now the following preparation work (write several events in the Paint event).

Asymptotic Redraw button with angle method

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 pattern method to redraw the button gradually

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 ();

}

Repaint the button's text (text) without using a pattern fill

private void Drawbuttontext (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 the Drawbuttontext function so that it can fill the text with a pattern

private void Drawbuttontext (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);

}

Okay, now it's time to rewrite the paint event.

protected override void OnPaint (PaintEventArgs pe)

{



Graphics G=pe. Graphics;

Base. OnPaint (PE); Methods to invoke the parent control

if (isusefloat==true)//If the angle is used to control the gradient

Drawbuttonwithangle (g);

if (Isusefloat==false)

Drawbuttonwithmode (G,mode);

if (isusestyle==true)//If use pattern to fill text

Drawbuttontext (G,hatchstyle);

Else

Drawbuttontext (g);

}

Okay, now it's done, save it, build it.

To create a test project

1. On the File menu, point to Add Item, and then click New Project to open the Add New Project dialog box.

2. Select the Visual C # project node, and then click Windows Application.

3. In the Name box, type Test.

4. In Solution Explorer, right-click the References node of the test project and choose Add Reference from the shortcut menu to display the Add Reference dialog box.

5. Click the tab labeled Project.

6. Double-click the Lineargradientbuttonlib item and note that the item appears in the Selected Components pane at this time.

After you add a reference, you should add the new control to the toolbox. If your control already appears in the toolbox, you should skip the next section.

To add a control to the toolbox

1. Right-click the Toolbox and choose Customize Toolbox from the shortcut menu.

The Customize Toolbox dialog box 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 list of components in the Customize Toolbox dialog box.

3. In the Customize Toolbox dialog box, click the box next to Lineargradientbutton and close the window.

Lineargradientbutton is added to the Selected Toolbox tab.

To add a control to a form

1. In Solution Explorer, right-click "Form1.cs" and choose View Designer from the shortcut menu.

2. In the Toolbox, scroll down until you reach the icon labeled Lineargradientbutton. Double-click the icon.

A "Lineargradientbutton" appears on the form.

3. Right-click "Lineargradientbutton" and choose "Properties" from the shortcut menu.

4. Check the properties of the control in the Properties window. Note that they are the same as the properties that are exposed by the standard buttons, and the difference is that some of the attributes we add ourselves

5. Set the foreground color and background color of this control, and then you can choose whether to fill the text, using the angle or the system settings to change the angle of the gradient.

6. From the Debug menu, choose Start. Appear Form1.

Anyone who needs the source code, please send me a letter.




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.