using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.Diagnostics; namespace Graphics2 { /// This is the second graphics example from class on 7/9/07. /// This program makes the graphics persistent by installing a /// handler for the form's Paint event and doing the drawing /// in the handler. The button doesn't do anything--it's there /// just to show that Windows redraws it for us automatically. /// The program also demostrates the use of a gradient brush /// for filling a rectangle. Also, calls of the paint handler /// are logged in the debug output window. (You have to run /// under the debugger to see this.) The IDE was used to create /// the stub of the Form2_Paint method and register it for the /// Paint event. public partial class Form2 : Form { private int paintHandlerCalls = 0; public Form2() { InitializeComponent(); } private void Form2_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; //We don't create this so we won't call Dispose on it Brush br = new LinearGradientBrush(new Point(10, 10), new Point(250,250), Color.Blue, Color.Red); g.FillRectangle(br, 10, 10, 240, 240); br.Dispose(); Debug.WriteLine("MyPaintHandler " + paintHandlerCalls++); } } } /// This line of code was generated by Visual Studio in the file Form2.Designer.cs to /// register for the Paint event: /// /// this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form2_Paint);