Windows Forms programming in C # Reading Notes-Chapter 4 Drawing Basics

Source: Internet
Author: User
1. Drawing to the screen

Bool drawellipse = false;


Void drawellipsebutton_click (Object sender, eventargs e ){

Drawellipse =! Drawellipse;


Graphics G = This. creategraphics ();

Try {

If (drawellipse ){

// Draw the ellipse

G. fillellipse (brushes. darkblue, this. clientrectangle );

}

Else {

// Erase the previusly drawn Ellipse

G. fillellipse (systembrushes. Control, this. clientrectangle );

}

}

Finally {

G. Dispose ();

}

}

Note: The reason for adding a try-Catch Block is: "The graphic class's implementation of idisposable dispose can release the underlying graphics object that it's maintaining. the graphics object must be released. Here, you can also use the C # unique using block (dispose is automatically called after completion ):
Void drawellipsebutton_click (Object sender, eventargs e ){

Using (Graphics G = This. creategraphics ()){

If (drawellipse)

G. fillellipse (brushes. darkblue, this. clientrectangle );

Else

G. fillellipse (systembrushes. Control, this. clientrectangle );

} // G. Dispose called automatically here

}


Note: If you want to enable automatic re-painting of an ellipse during window resize. This must be added to the constructor of the Form class. setstyle (controlstyles. resizeredraw, true); add this to the button click event. refresh (); (equivalent to this. invalidate (true) plus this. update ();) but it is inefficient to automatically redraw all windows during resize, which is time-consuming.

2. Saving and restoring graphics settings

Use the graphicsstate object in system. Drawing. drawing2d namespace, and add the SAVE () and restore () Methods of the graphics class.
Void drawsomething (Graphics g ){

// Save the old

Graphicsstate oldstate = G. Save ();


// Change the smooth Mode

G. smoothingmode = smoothingmode. antialias;


// Start painting

// Restore the old

G. Restore (oldstate );

}


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.