C # GDI + programming (i)

Source: Internet
Author: User
Although it is C + +, there are always some similarities.

When the window refreshes, it generates a paint event, so we add a handler function to the event. Then draw in this function. will ensure that the drawing is not refreshed,

It can always be displayed. The delegate that corresponds to the Paint event is: public delegate void Painteventhandler (object sender, PaintEventArgs e);

Let's start with the simplest drawing and draw a line on the window. (Create WindowsForms application)

public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();

Add the Paint event handler function
This. Paint + = Formpaint;
}
private void Formpaint (Object sender, PaintEventArgs e)
{
Graphics graphics = E.graphics;
Brush, green, 2 pixels wide
Pen pen=new Pen (Color.FromArgb (0,255,0), 2);
Draw a line, two points is 0,0 and 100,100
Graphics. DrawLine (pen,new Point (0,0), new Point (100,100));
}
}

Like drawing lines, rectangles, circles, and pictures, all can be done by using the graphics class.

Example 2: A fill rectangle

Graphics graphics = E.graphics;
Blue Paint Brush
SolidBrush brush = new SolidBrush (Color.FromArgb (0, 0, 255));
a rectangle
Rectangle rect = new Rectangle (0, 0, 100, 100);
Fills a rectangle
Graphics. FillRectangle (brush, rect);

Example 3: Draw a PNG image (PNG is because you can display transparent pictures, GIF pictures also have this effect)

private void Formpaint (Object sender, PaintEventArgs e)
{
Graphics graphics = E.graphics;
Loading pictures
Image img=image.fromfile ("D:\\image\\win.png");
The picture shows the starting position
Point Strpoint=new Point (50,50);
Do not limit size drawing
Graphics. DrawImage (IMG, strpoint);
Narrow down the picture and limit it within a rectangle
Rectangle rect=new Rectangle (50,50,100,100);
Graphics. DrawImage (IMG, rect);
}

Display text with drawstring

DrawString has several overloads in the Grahpics class, some of which allow strings to be displayed within a rectangle, and some can use a specific display format. This is not a detailed introduction.

Only the more commonly used.

Look at the example, handle keyboard input character events, and display the input characters in the window. As follows:

public partial class Form1:form
{
public static String StrText = "";
Public Form1 ()
{
InitializeComponent ();
This. Paint + = Formpaint;
This. KeyPress + = formkeypress;

}
private void Formpaint (Object sender, PaintEventArgs e)
{
Graphics graphics = E.graphics;
Create a paint brush
SolidBrush brush=new SolidBrush (Color.FromArgb (0,255,0));
Creating fonts
Font font=new font ("Arial", 20f);
Displays the string, within a rectangle
Graphics. DrawString (strText, font, brush, this.) ClientRectangle);

}
private void Formkeypress (object sender, KeyPressEventArgs e)
{
StrText + = E.keychar;
Refresh Entire window
This. Invalidate ();
}
}

and a graphics. DrawString (strText, font, brush, new Point (100, 100)), display mode, this just specifies the starting position of the text display.

For the display format, see the following example:

Displays the string, within a rectangle
StringFormat Strformat = new StringFormat (stringformatflags.directionrighttoleft);
Graphics. DrawString (strText, font, brush, this.) Clientrectangle,strformat);

StringFormatFlags is an enumeration type, try it yourself, and see what format each enumeration member represents.

Next, let's take a look. The default method for handling events in the form class, and how you add an event-handling method, have a relationship.

One example: Handling the left mouse button event

public partial class Form1:form
{
Private Color Prbackcolor;
Public Form1 ()
{
InitializeComponent ();
Window default background color
Prbackcolor = this. BackColor;
Add Event Handling
This. MouseDown + = Formmousedown;
This. MouseUp + = Formmouseup;
}
mouse button press Event handling method
private void Formmousedown (object sender, MouseEventArgs e)
{
If the right mouse button is pressed
if (E.button = = mousebuttons.right)
{
Form1 Form1 = (Form1) sender;
Change the background color
Form1. BackColor = Color.FromArgb (0, 255, 0);
}
}
mouse button Bounce (release) event handling method
private void Formmouseup (object sender, MouseEventArgs e)
{
if (e.button==mousebuttons.right)
{
Form1 Form1 = (Form1) sender;
Form1. BackColor = Prbackcolor;
}

}

}

Like mouse button press MouseDown and mouse button release, the form has a default event handling method, these methods are virtual methods, you can rewrite them, such as rewrite

protected virtual void OnMouseDown (MouseEventArgs e); protected virtual void OnMouseUp (MouseEventArgs e);

This allows you to implement the above example by overriding the default event handling method of the parent class, without having to add event handlers or handle mouse events.

In the case of MSDN, you can see that, like these default event-handling methods, calls can raise their corresponding events. For example, I call the onmousedown can trigger the left mouse button down event, in fact, the execution of our added event processing method (delegate). And we're still multicast-commissioned because we're adding a delegate method with "+ =".

In that case, if you rewrite the onmousedown, be sure to call the base class's OnMouseDown method inside,

Otherwise the MouseDown method we add will not be executed (if any)

So knowing the above, I'll do a practical example. Overrides the method that OnPaintBackground draws the background.

public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}
Overriding the OnPaintBackground method
protected override void OnPaintBackground (PaintEventArgs e)
{
Prohibit base class processing, we draw the background ourselves
Base. OnPaintBackground (e);
Transparent Background paint brush
SolidBrush brush=new SolidBrush (color.transparent);
Fill the entire window
E.graphics.fillrectangle (brush,this. ClientRectangle);
Draw a circle again.
Pen pen = new Pen (Color.FromArgb (0, 255, 0), 3);
E.graphics.drawellipse (pen, this.) ClientRectangle);
}
}

Texturebursh Picture Painting Brush

You can use a picture to fill a shape, such as a rectangle, a circle. The picture is not large enough to be tiled.

Example:

private void Formpaint (Object sender, PaintEventArgs e)
{
Graphics graphics = E.graphics;
Create a picture paint brush
Rectangle rect = new Rectangle (10, 10, 70, 70);
TextureBrush brush = new TextureBrush (Image.FromFile ("d:\\image\\345.jpg"), rect);
Graphics. FillEllipse (brush, 0, 0, 200, 200);
}

Constructor last argument, rect indicates which part of the picture is to be populated with, 10,10 represents the starting position of the picture (upper-left corner), 70,70 represents width and height, and attention cannot exceed the original extent of the picture. If you fill the entire picture, you do not need to specify a rect. Just fill in the constructor with a parameter.

Lineargradientbursh Linear Gradient Brush (this class exists in the System.Drawing.Drawing2D namespace)

The Lineargradientbursh class has a constructor overload, which has four parameters, two points, and two colors.

These four parameters specify the color of the starting point, and the color of the end point. There is also a location.

Look at the following example:

public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
This. Paint + = Formpaint;

}
private void Formpaint (Object sender, PaintEventArgs e)
{
Graphics graphics = E.graphics;
LinearGradientBrush Linebrush = new LinearGradientBrush (new point (0, 0), New Point (50, 0),
Color.FromArgb (255, 255, 255), Color.FromArgb (0, 0, 0));
Fill the entire window
Graphics. FillRectangle (linebrush,this. ClientRectangle);
}
}

The starting point is the 0,0 end point is 0,50. A gradient color segment from white to black. More than 50 of the parts, and began to re-gradient.

Just like when using PS, the gradient color is white to black, then pull a line. The starting point is 0, 0, and the end point is 0, 50.

So if I use this property of the gradient brush in the window to draw a rectangle is what kind of, see above can know.

Like graphics. FillRectangle (linebrush, 0, 0, 100, 100);

This rectangle's brush fill is the same as the corresponding rectangular area of the first effect.

If you change the values of the start and end points, you can also see them. That's how it's filled in.

For example, the starting point is changed to 0, 0, and the end point changes 50,50.

private void Formpaint (Object sender, PaintEventArgs e)
{
Graphics graphics = E.graphics;
LinearGradientBrush Linebrush = new LinearGradientBrush (new point (0, 0), New Point (50, 50),
Color.FromArgb (255, 255, 255), Color.FromArgb (0, 0, 0));
Fill the entire window
Graphics. FillRectangle (Linebrush, this. ClientRectangle);
Pen pen = new Pen (Color.FromArgb (0, 255,0));
Graphics. DrawRectangle (pen, 0, 0, 50, 50);
}

Draw the fill shape inside, which is shown by the rules above. This window does not disable the Maximize function, you can change the window size for further observation.

Multiple color gradients

The LinearGradientBrush class has a interpolationcolors attribute member that can specify a variety of color gradients, which are a colorblend type, like previous gradients, that can be limited to gradients of two colors. With Interpolationcolors, you can use a variety of, such as red to green gradients, and then green to blue.

See Example:

private void Formpaint (Object sender, PaintEventArgs e)
{
Create a ColorBlend object that specifies multiple color gradient information
ColorBlend color_blend=new colorblend ();
Specify several colors
Color_blend. Colors=new Color[]{color.red,color.green,color.blue};
Specify the range of colors
Color_blend. Positions=new float[]{0/3f,2/3f,3/3f};
Rectangle rect=new Rectangle (0,0,200,100);
Create a gradient paint brush
LinearGradientBrush brush=
New LinearGradientBrush (new Point (0,0), new Point (200,0), color.white,color.white);

Brush. Interpolationcolors=color_blend;
E.graphics.fillrectangle (Brush,rect);
}

Color_blend. The colors array is how many color gradients are specified, such as the red and green blue above, then the gradient should look like this, from red to green, then from green to blue.

Color_blend. positions specifies the range of colors, such as the width of the rectangle above as the whole 1, then the red to green gradient, from 0/3f to 2/3f completed, that is, in this

Within the range of the red to green gradient, the green to blue gradient range is 2/3f to 3/3f.

If you want to account for half of each, that is color_blend. Positions=new float[]{0/2f,1/2f,2/2f};



Related Article

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.