Monday, August 27, 2007

Drawing a filled rectangle on a windows form

This is quite straight-forward. Override the OnPaint() method with the following code:



And it's done. A filled rectangle will be painted at the top of the form.

The code is in the overridden OnPaint() method so that each time the form is resized/obscurred by another form, the content will be re-painted automatically. Also, it is a good practice to call the Dispose methods of objects that consume system resources heavily (e.g. Brush and Graphics objects).

No comments: