Write the control with C # yourself (below)

zhaozj2021-02-17  76

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, THIS.WIDTH, THISHEIGHT), 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.

转载请注明原文地址:https://www.9cbs.com/read-31231.html

New Post(0)