C # GDI + programming (i)

Source: Internet
Author: User

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);

Example 1: draw a line in the middle of the window . (Create WindowsForms application)

Graphics g = This.            CreateGraphics (); Pen P=NewPen (Color.Black); //draw a line in the middle of the screenG.drawline (P,0, This. Height/2, This. Width, This. Height/2); //Draw a rectangleG.drawrectangle (P, -, -, $, -); //Draw a circleG.drawellipse (P, Max, Max, -, -); //Manually Releasing ResourcesG.dispose ();        P.dispose (); 

Example 2: A fill rectangle

        Private voidFormpaint (Object sender, PaintEventArgs e) {graphics graphics=E.graphics; //Blue paint BrushSolidBrush brush =NewSolidBrush (Color.FromArgb (0,0,255)); //a rectangleRectangle rect =NewRectangle (0,0, -, -); //fills a rectangleGraphics.        FillRectangle (brush, rect); }

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

        Private voidFormpaint (Object sender, PaintEventArgs e) {graphics graphics=E.graphics; //Loading Pictures            Image img = image.fromfile (application.startuppath+ @ "\111.png"); //The picture shows the starting positionPoint strpoint=NewPoint ( -, -); //do not limit size drawingGraphics.            DrawImage (IMG, strpoint); //narrow down the picture and limit it within a rectangleRectangle rect=NewRectangle ( -, -, -, -); Graphics.        DrawImage (IMG, rect); }

Example 4: 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 classForm1:form { Public StaticString StrText ="";  PublicForm1 () {InitializeComponent ();  This. Paint + =Formpaint;  This. KeyPress + =formkeypress; }        Private voidFormpaint (Object sender, PaintEventArgs e) {graphics graphics=E.graphics; //Create a paint brushSolidBrush brush=NewSolidBrush (Color.FromArgb (0,255,0)); //Creating FontsFont font=NewFont ("Song Body", 20f); //displays the string, within a rectangleGraphics. DrawString (strText, font, brush, This.                 ClientRectangle); }        Private voidFormkeypress (Objectsender, KeyPressEventArgs e) {StrText+=E.keychar; //Refresh Entire window             This.        Invalidate (); }    }

C # GDI + programming (i)

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.