How to Heavy WINDOWS controls

zhaozj2021-02-16  86

How to Heavy WINDOWS controls

This article:

Overview Platform Demand Example 1: Changing Control Attributes Example 2: Heavy Pictures Simple Control

Overview:

When you are in the new experience of Windows XP, you must think "if my program interface can be so beautiful, how much better." Then let's see how you should override the Windows standard control through the .NET class library.

This article uses two examples to illustrate how to effectively change the appearance of the Windows standard control through program coding.

Platform demand:

Visual Studio .NET Visual C # .NET

Example 1: Change the control attribute

When using Visual Studio .NET to develop software, you can use its powerful IDE environment to manually set the properties of the interface control. But if you want to make your control style on your entire program, the best way is your own overload control class.

Let us start attempting from the simplest system.windows.forms.button class, buttons have the following common properties:

Backcolor - Background color of the button

Cursor - mouse status

Enabled - Whether the button is enabled

FlatStyle - button's flat style

Font - button on the button

Forecolor - the foreground color of the button

HEIGHT - the height of the button

Image - Picture of Button

Location - the position on the button

Text - the text on the button

Visible - button is visible

Width - the width of the button

In this example below, we get a blue bottom white word of flat buttons:

1. Create a Windows Application project in Visual C # .NET, named oondrawbutton1;

2. Add a MyButton class to the project to inherit system.windows.Forms.Button and add the code to modify the attribute in the constructor;

like this:

public class MyButton: System.Windows.Forms.Button {public MyButton () {// set the back color to blue BackColor = System.Drawing.Color.Blue; // set the appearance style to flat FlatStyle = System.Windows.Forms .FlatStyle.Flat; // use system default font font = System.Windows.Forms.Control.DefaultFont; // use white color to write text and draw the border ForeColor = System.Drawing.Color.White;}}

3. Drag a button to Form1, right click on the mouse, click "View Code", replace "System.Windows.Forms.Button" in the code in the code.

4, compile execution.

Now, you can see custom controls on your interface (Figure 1).

Figure 1 Modify the button of the control attribute via the code

Example 2: Heavy painting simple control

Only by changing the attribute value of the control comes from the definition control, in many cases, it is not satisfactory. For example, if you want to put the style of Windows XP in Windows 98/2000, you have to draw those buttons.

In this case, you can redraw buttons by overpaint () and other events. You can also let the control respond to more events via EventHandler, for example, we can add a mouse hover for the button, add EEventHandler to MouseEnter and MouseEleave, which, the code you write in the event response function will be called . Here is an example, this example has written a control of the button style in Windows XP:

1. Create a Windows Application project in Visual C # .NET, named oowdrawbutton2;

2, add a MyButton class to the project, change it to inherit system.windows.forms.button, add the code and event response association code that modifies the property in the constructor, and write each function code, as follows:

using System; using System.Drawing; using System.Windows; using System.Windows.Forms; namespace OwnDrawButton2 {public class MyButton: System.Windows.Forms.Button {// state variable for mouse down private bool mouseDown = false; // state variable for mouse hover private bool mouseHover = false; public MyButton () {// set the control UserPaint SetStyle (ControlStyles.UserPaint, true); // EventHandler for MouseDown MouseDown = new MouseEventHandler (OnMouseDown); // EventHandler for MouseUp MouseUp = new MouseEventHandler (OnMouseUp); // EventHandler for MouseEnter MouseEnter = new EventHandler (OnMouseEnter); // EventHandler for MouseLeave MouseLeave = new EventHandler (OnMouseLeave); Height = 23; // Default Height Width = 75; // Default Width} protected override void OnPaint (PaintEventArgs pe) {// first, paint the control with parent form's background color pe.Graphics.FillRectangle (new SolidBrush (Parent.BackColor), pe.ClipRectangle); // if the button is disabled, draw the Disable style if (enabled == false) { DrawDisableButton (pe.Graphics);} else if (mouseDown) {// when mouse down, draw the mouse down style DrawMouseDownButton (pe.Graphics);} else if (mouseHover) {// when mouse hover, draw the mouse hover style DrawMouseHoverButton (pe.Graphics);} else if (Focused) {// when mouse is focused but not mouse hover, // draw the focus style DrawContainFocusButton (pe.Graphics);} else // else, draw the normal style {DrawNormalButton (pe.Graphics);} WriteText (pe.Graphics); // write text} private void OnMouseDown (object sender, MouseEventArgs e) {mouseDown = true; // mouse is down now} private void OnMouseUp (object sender, MouseEventArgs e ) {mousedown = false;

// mouse is up now // call paint action PaintEventArgs pe = new PaintEventArgs (CreateGraphics (), ClientRectangle); OnPaint (pe);} private void OnMouseEnter (object sender, EventArgs e) {mouseHover = true; // mouse hover on // call paint action PaintEventArgs pe = new PaintEventArgs (CreateGraphics (), ClientRectangle); OnPaint (pe);} private void OnMouseLeave (object sender, EventArgs e) {mouseHover = false; // mouse is not hover on // call paint action PaintEventArgs pe = new PaintEventArgs (CreateGraphics (), ClientRectangle); OnPaint (pe);} private void DrawBorder (Graphics g, int state) {if (state == 1) // draw normal style broder {Pen p = new Pen (SystemColors.ControlLightlight, 2); g.drawline (p, 1, 1, 1, height-2); g.drawline (p, 1, 1, width-2, 1); g.drawline (p, width- 1, 2, width-1, height-2); g.drawline (p, 2, height-1, width-2, height-1);} else if (state == 2) // Draw Hover Style Border { Pen P = New Pen (Color.Yellow, 2); g.drawline (p, 1, 1, 1, height-2); g.drawline (p, 1, 1, width-2, 1); g.drawline (p, width-1, 2, width-1, heiGHT-2); g. Drawline (P, 2, Height-1, Width-2, Height-1);} else if (state == 3) // Draw pressed style border {pen p = new pen (systemcolors.controlDark, 2); g. Drawline (p, 1, 1, 1, height-2); g.drawline (p, 1, 1, width-2, 1); g.drawline (p, width-1, 2, width-1, height- 2); g.drawline (p, 2, height-1, width-2, height-1);} else if (state == 4) // Draw Disabled style border {Pen P = new Pen (SystemColors.Controllight, 2); g.drawline (p, 1, 1, 1, height-2); g.drawline (p, 1, 1, width-2, 1); g.drawline (p, width-1, 2, width -1, Height-2; g.drawline (p, 2, height-1, width-2, height-1);} else if (state == 5) // Draw default style border {Pen P = New Pen (Color.skyBlue, 2); g.drawline (p, 1, 1, 1, height-2); g.drawline (p, 1, 1, width-2, 1);

g.drawline (p, width-1, 2); g.drawline (p, 2, height-1, width-2, height-1);} if (state == 4) // Draw disable style border {Pen P = new Pen (Color.Fromargb (161, 161, 146), 1); g.drawline (p, 0, 2, 0, height-3); g.drawline (p, 2, 0, Width-3,0); g.drawline (p, width-1, 2, width-1, height-3); g.drawline (p, 2, height-1, width-3, height-1); g .Drawline (p, 0, 2, 2, 0); g.drawline (p, 0, height-3, 2, height-1); g.drawline (p, width-3, 0, width-1, 2 ); g.drawline (p, width-3, height-1, width-1, height-3);} else // Draw Normal style border {g.drawline (Pens.black, 0, 2, 0, Height- 3); g.drawline (Pens.Black, 2, 0, Width-3, 0); g.drawline (Pens.Black, Width-1, 2, Width-1, Height-3); g.drawline (Pens .Black, 2, height-1, width-3, height-1); g.drawline (Pens.Black, 0, 2, 2, 0); g.drawline (Pens.Black, 0, Height-3, 2 Height-1); g.drawline (Pens.Black, Width-3, Width-1, 2); g.drawline (Pens.black, Width-3, Height-1, Width-1, Height-3 }}} private void DrawnormalButton (Graphics g) // Draw Normal Style Button {// Draw Normal Style Border DrawBorder (g, 1); // Paint Background P aintBack (g, SystemColors.ControlLightLight);} private void DrawMouseHoverButton (Graphics g) {// draw mouse hover style border DrawBorder (g, 2); // paint background PaintBack (g, SystemColors.ControlLightLight);} private void DrawMouseDownButton ( Graphics g) {// draw mouse down style border DrawBorder (g, 3); // paint background PaintBack (g, SystemColors.ControlLight);} private void DrawDisableButton (Graphics g) {// draw disable style border DrawBorder (g, 4); // paint background PaintBack (g, SystemColors.ControlLight);} private void DrawContainFocusButton (Graphics g) {// draw contain focuse style border DrawBorder (g, 5); // paint background PaintBack (g, SystemColors.ControlLightLight );

} // Paint Background Area of ​​The Button Private Void Paintback (Graphics G, Color C) {G.FillRectangle (New SolidBrush (C), 3, 3, Width-6, Height-6);} Private Void WriteText (Graphics G ) {// Calculate the text position int x = 0, y = 0; size s = g.measureString (text, font) .tosize (); x = (width-s.width) / 2; y = (Height- S.HEIGHT / 2; // Draw Text IF (Enabled) // IS Enabled, Draw Black Text G.drawstring (Text, Font, Brushes.black, X, Y); Else // Not Enabled, Draw Gray Text G .DrawString (Text, font, brushes.gray, x, y);}}} 3, add: Drag 3 buttons to FORM1, set one of the "enable" to "false" Right click on the mouse, click "View Code", replace "System.Windows.Forms.Button" with "MyButton" in the code;

4, compile execution.

In the end, we get the button control as shown in Figure 2. It maintains a look on any version of the Windows operating system.

Figure 2 A button control similar to Windows XP style

Author: Han set a

Related resources: C # window automatic adjustment control redraw method

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

New Post(0)