Write the control yourself in C # (bottom)

Source: Internet
Author: User
Control

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 Lineargradientbuttonlibindebug 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.