Help C # rookie enter GDI + development

zhaozj2021-02-16  96

Seeing a lot of netizens asked questions about designing some style unique buttons, or to make some icons on some controls, the answer is that the answer is a "use GDI " that is what is GDI ? How to apply GDI ? Today, I will give you a small example, I hope to help everyone:

1 GDI definition: GraphicDevice Interface Plus is a graphical device interface, and we can design some of the controls through this excuse.

2 a small example: How to use GDI to modify a button style:

The first step, we have to build a class --button_paint, this class is what we need to use, we design the button itself.

In the second step, we need to quote two components come in:

One is system.drawing.dll, this is the components required for GDI .

The other is System.Windows.Forms.dll, we need this component to rewrite the button object inside.

The third step, now start to enter the write code, first to do it to transfer the components just referenced to our program:

Using system.drawing; using system.windows.forms;

Then we have to inherit the original Button object in here, so that this object is rewritten, the code is as follows:

Public class button_paint: system.windows.forms.button

Then add a proxy in the entry point public button_paint () in this function, just when the class is called, when performing image generated part of this.paint, automatically activate the method of our own design Button_Paint:

This.paint = new system.windows.forms.painteventhandler (Button_Paint);

Then we write the method of budton_paint, bring the agent of this painting into this method, and then starting the regular GDI programming.

Private void button_paint (Object Sende, System.Windows.Forms.PainteventArgse)

In the method of Button_Paint, we can add some simple graphical modifications to this button, the method is as follows:

First we define a brush, we need to use it to draw pictures, this brush we use red:

Pen Pen = New Pen (Color.RED);

Then define the width of the brush:

Pen.width = 8;

Ok, we started to paint now, the object of operation is naturally the object we transferred in need to modify the object E

Let's draw first line:

E.Graphics.drawline (Pen, 7, 4, 7, this.height-4);

This is not necessary to explain.

Then draw an ellipse

E.Graphics.drawellipse (Pen, this.width-16, 6, 8, 8);

Then recompile this class, then reference to our application, you can see the effect of this object we involved.

The code I do this is as follows:

Using system.drawing; using system.windows.forms ;. USING SYSTEM.WINDOWING;

namespace Button_paint {public class Button_paint: System.Windows.Forms.Button {public Button_paint () {this.Paint = new System.Windows.Forms.PaintEventHandler (button_paint);} private void button_paint (object sende, System.Windows.Forms. Painteventargse) {Pen Pen = New Pen (Color.red); Pen.Width = 8; E.Graphics.drawline (Pen, 7, 4, 7, this.Height-4); E.Graphics.Drawellipse (Pen, this .Width-16, 6, 8, 8);}}}

I hope that the above instructions are helpful to everyone. If you think this article is helpful to you, I hope you can reply, I will decide whether to continue.

-

__________

My personal website:

Www.yt-e.com www.cha8.com www.1liao.com

Old K

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

New Post(0)