If you draw graphics onto a form, they may be lost when the form — or sections of the form — refreshes. This program shows one way to retain the graphics on the form background.
The program included here, named GraphicsTest, uses a simple Windows form with three buttons. Two of the buttons display graphics in the window and the third exits the program. This is a modification of a larger listing from my book, Teach Yourself the C# Language in 21 Days".
When you click the different buttons, shapes are drawn on the window's background. Figures 1 to 3 show some results. Because the location and the size of shapes are random, your output will be different. If you click a button again, more shapes will be drawn.
Figure 1 The GraphicsTest form.
Figure 2 - Output from GraphicsTest.
Figure 3 - Output from GraphicsTest.
I'm not providing a complete analysis for the listing; however, a couple comments deserve to be made.
This program uses a set of classes used to create and draw graphics. The graphics are drawn onto a basic graphics object named DrawingArea. This is a simple bitmap image that was declared in Line 14. All of the graphics are drawn onto this bitmap rather than directly to the form. The bitmap is then copied to the form. If you draw directly to the form, the graphics may be lost when the form is refreshed.
Stepping back, you'll see that in Line 4, the System.Drawing namespace is included. This namespace contains many of the graphic routines. Also included are a number of other namespaces, including Windows.Forms, which contains information for doing the Windows form logic.
In Lines 12 to 21, a number of variables are declared that will be used in drawing the graphics and creating controls on the form. Line 20 contains a variable for getting random numbers. In Line 21, myPen is declared as a Pen object. This object will be assigned colors and used to draw shapes later in the listing. In Line 27, you see a new Pen object actually created and the color blue is assigned to it. You can assign any color to the pen.
NOTE: Lots of colors are listed in the Color enumeration. You can assign them in the same way that the color blue was assigned. You can see a list of names for the many available colors at the end of this article.
In Lines 102 to 121, you can see the method btnLine_Click defined. This is actually an event that is executed when the line button is clicked. Because the color of the pen could have been changed, in Line 112, the pen defined earlier is set to the color blue. Before that, in Line 1105, a graphics object was created and assigned the DrawingArea graphic created earlier. This new graphic, oGraphics, will be used to draw upon.
In Lines 109 to 117, there is a loop that executes 50 times. This loop draws a single line each time through the loop by calling the DrawLine method in the graphics object that was created. The parameters being passed are not as complicated as they look in Lines 113 to 116. The first parameter is the pen, myPen. The next four parameters are random numbers. The first and third are numbers between 0 and the width of the form. The second and fourth are between 0 and the height of the form. The first two numbers are the starting point of the line. The last two are the ending point.
After the lines have been drawn, calling the Dispose method cleans up the oGraphics object. This is done in Line 118. Because the graphics object uses a lot of system resources, it is good to force this clean up.
The disposal of the graphics object is followed by a call to this.Invalidate in Line 120. Invalidate makes the current form redraw itself. This causes a paint event to occur. An event handler is created in Lines 185 to 198 for the painting event. In this method, the DrawingArea image is copied to the form's background image.
The other method events in this listing operate in much the same as in the earlier line listing: Random numbers are used to draw shapes. One other difference is in Lines 134 to 1137. In these lines, instead of setting the pen to a specific color, it is set to a random color. The FromArgb method sets red, green, and blue values for a single color. Because random numbers are used, the result is a random color.
This is a fun listing to play with. You should be able to change the colors, modify the shape sizes and locations, and work with these basic graphics. If you check out the listing in my book, you will find that rectangles and a solid color are added to the listing.
Listing GraphicsTest.cs — A Program to Draw Colored Shapes on a form.
About the Author Bradley Jones, CodeGuru Site Manager, is a Microsoft MVP that works for Jupitermedia as an Executive Editor over many of the software development sites and channels. His experience includes development in C, C++, VB, some Java, C#, ASP, COBOL, and more as well as having been a developer, consultant, analyst, lead, and much more. His recent books include Teach Yourself the C# Language in 21 Days.
Tools:
Add www.codeguru.com to your favorites Add www.codeguru.com to your browser search box IE 7 | Firefox 2.0 | Firefox 1.5.xReceive news via our XML/RSS feed
RATE THIS ARTICLE:
Excellent Very Good Average Below Average Poor
(You must be signed in to rank an article. Not a member? Click here to register)