Adding a Paint Event Handler to a Form and Controls in GDI+


This article has been excerpted from book "Graphics Programming with GDI+".

Adding a Paint Event Handler to a Form

Adding a paint event handler for any Control-derived class is pretty simple. We write an event handler that has two parameters, of types object and PaintEventArgs:

        private void MyPaintEventHandler(object sender, System.Windows.Forms.PaintEventArgs args)
        {
        }

We can give the event handler whatever name we want. After implementing this event handler, we use the parameter args (which is a PaintEventArgs object) to get the Graphics object for the control. The following code delegates the vent handler for the Paint event:

this.Paint +=
new System.Windows.Forms.PaintEventHandler
(this.MyPaintEventHandler);

The following code gives the paint event handler for a form:

        private void MyPaintEventHandler(object sender, System.Windows.Forms.PaintEventArgs args)
        {
            //Write your code here
        }

Now we can use the PrintEventArgs object to get the Graphics object associated with the form and use the Graphics object's methods and properties to draw and fill lines, curves, shapes, text, and images. Let's draw a rectangle, an ellipse, and some text on the form, as shown in Listing 13.1.

LISTING 13.1: Using the paint event handler to draw

private void MyPaintEventHandler (object sender, System.Windows.Forms.PaintEventArgs args)
{
//Drawing a rectangle
args.Graphics.DrawRectangle (
new Pen (Color.Blue, 3),
new Rectangle (10, 10, 50, 50));
//Drawing an ellipse
args.Graphics.FillEllipse (Brushes.Red,
newRectangle (60, 60, 100, 100));
//Drawing text
args.Graphics.DrawString ("Text",new Font ("Verdana", 14),new SolidBrush (Color.Green), 200, 200);
}

Figure 13.2 shows the output from Listing 13.1. Now if the form is covered by another window and the focus returns to the form, the code on the paint event handler will repaint the form.

Adding a Paint Event Handler to Windows Controls

As mentioned earlier, the paint event handler can be added to any Windows control that is inherited from the Control class, such as Button, ListBox, or DataGrid. In other words, each Windows control can have a paint event handler and a Graphics object, which represent the control as a drawing canvas. That means we can use a button or a list as a drawing canvas.

Figure 13.2.jpg

FIGURE 13.2: Drawing on a form

Let's add DataGrid and Button controls to a form. We will use the button and the data grid as our drawing canvases. Listing 13.2 adds the paint event methods of our Button1 and DataGrid1 controls.

LISTING 13.2: Adding a paint event handler for Windows control

//Adding a button's Paint event handler
this.button1.Paint + =
new System.Windows.Form.PaintEventHandler
(this.TheButtonPaintEventHadler);
//Adding a data grid's Paint event handler
this.dataGrid1.Paint +=
new System.Windows.Forms.PaintEventHandler
(this.TheDataGridPaintEventHandler);

Listing 13.3 gives the code for the Button and DataGrid paint event handlers. This code is useful when we need to draw graphics shapes on a control itself. For example, a column of a data grid can be used to display images or graphics shapes. In our example we draw an ellipse on these controls, instead of drawing on a form. The PaintEventArgs.Graphics object represents the Graphics object associated with a particular control. Once you have the Graphics object of a control, you are free to call its draw and fill methods.

LISTING 13.3: Drawing on Windows controls

        private void TheButtonPaintEventHandler(object sender, System.Windows.Forms.PaintEventArgs btnArgs)
        {
            btnArgs.Graphics.FillEllipse(
            Brushes.Blue,
            10, 10, 100, 100);
        }
 
        private void TheDataGridPaintEventHandler(object sender, System.Windows.Forms.PaintEventArgs dtGridArgs)
{
dtGridArgs.Graphics.FillEllipse (
Brushes.Blue,
10, 10, 100, 100;);
}


Figure 13.3 shows the output of Listing 13.3. As you can see, a button or a data grid can function as a drawing canvas. The top left-hand corner of a control is the (0,0) coordinate of the canvas associated with that control.

Figure13.3.jpg

FIGURE 13.3: Drawing on Windows controls

As this stage it is worth point big out another big advantage that GDI+ has over GDI: the flexibility to have a Graphics object associated with a control.

book.gif

Next Recommended Readings