Monday, July 28, 2008

Draw Focus Rectangle using ControlPaint Class

Level: Beginner

Knowledge Required:
GDI+

Description:
Sometimes while creating a User Control from scratch. It is required to display the Focus Rectangle (the dotted border). This can be easily achieved by using

System.Windows.Forms.ControlPaint

E.g.:
Private Sub Form1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Click
    Dim g As Graphics
    g = Me.CreateGraphics
    ControlPaint.DrawFocusRectangle(g, New Rectangle(10, 10, 100, 100))
End Sub


ControlPaint class contains other useful routines for control drawing you can explore the list using intellisense.

2 comments:

Cheri said...

Now do the example for a control that already exists on a form, say a radio button or checkbox.

Jonathan Wood said...

Anyone else find that ControlPaint.DrawButton() seems to draw old gray buttons on XP? Haven't yet tried it on Vista or Win7. But why doesn't the button drawn look the same as regular buttons? What am I missing?