(c) 2003 Visual Studio Magazine 
Fawcette Technical Publications

VB.NET	Listing 1
Draw an Entire Button. The OnPaint procedure is the heart of the ImageButton, where you draw display images, focus indicators, and control borders. GDI+ lets you do this with a tiny amount of code.

Protected Overrides Sub OnPaint(ByVal e As _
	System.Windows.Forms.PaintEventArgs)
	Dim g As Graphics = e.Graphics
	Try
		Dim CurrentImage As Image
			If Me.DesignMode Then
				CurrentImage = _
					Me.ImageList.Images( _
					Me._DisplayImageIndex)
		Else
			Select Case _MouseButtonState
				Case MouseButtonStates.Down
					CurrentImage = _
						Me.ImageList.Images( _
						_DownImageIndex)
				Case MouseButtonStates.Hover
					CurrentImage = _
						Me.ImageList.Images( _
						_HoverImageIndex)
				Case MouseButtonStates.Up
					CurrentImage = _
						Me.ImageList.Images(_UpImageIndex)
			End Select
			End If
			g.DrawImage(CurrentImage, 1, 1, _
				CurrentImage.Width, CurrentImage.Height)
	Catch ex As Exception
		'An image is missing
	End Try
	Dim BorderColor As Color
	If _MouseButtonState = MouseButtonStates.Up Then
		BorderColor = Color.DarkGray
	Else
		BorderColor = Color.Blue
	End If
	Dim w As Integer
	If _HasFocus And _MouseButtonState = _
		MouseButtonStates.Up Then w = 2 Else w = 1
	Dim p As New Pen(BorderColor, w)
	Dim r As New Rectangle(w - 1, w - 1, Me.Size.Width - w, _
		Me.Size.Height - w)
	g.DrawRectangle(p, r)
	p.Dispose()
	MyBase.OnPaint(e)
End Sub	
