Maintaining the Bounding Rectangle

The area of a window in which you can draw is referred to as the window’s client area. Within this area, a bounding rectangle defines the invisible rectangular region in which the Graphics object draws, and can include the window’s entire client area.

When a window loses, and then regains focus, the part of the bounding rectangle covered by some other object does not automatically re-display the items that were previously rendered to it.

To ensure correct display, you must manage the repainting of your form or control. The paint event handler is the place in your Form class code in which such management typically takes place. Within this handler, you restore the bounding rectangle to its proper state.

The following example creates a Bitmap object at the class level, then it uses the paint event handler to redraw the Bitmap. Each time the client area of the form becomes invalid, Windows invokes this handler, and the image is repainted to the form:

Bitmap bmp = new Bitmap("c:\\MyImage.bmp");

protected void onPaint(PaintEvent e){
    e.graphics.drawImage(bmp, new Point(0, 0));
}

When your form is initially displayed, and each time it regains focus, the paint event handler is automatically invoked. However, if your form supports resizing, a change in the form’s dimensions does not automatically trigger a repaint. Instead, you must add a resize handler to the Form-derived class and then call the object’s invalidate method from within this handler. A call to invalidate trigger’s the form’s paint event handler:

protected void onResize(Event e){
    this.invalidate();
}