If you are implementing a control with user-drawn features instead of a control
made up of constituent controls, you'll use the
UserControl's
Paint event to manage your control's appearance.
The Paint event occurs at design time and at runtime
whenever the operating system determines that your control needs to redisplay graphics. This
would include such events as control siting and resizing or a control being uncovered after
another window had obscured it.
You will put in the Paint event's procedure whatever
graphics commands and methods you need to display the control's graphics. You will probably
need to recompute the sizes and shapes of graphics objects to allow for resizing of the control.
In the example in Listing 13.3, we use the Paint event
procedure to draw an ellipse with a particular color and shading style on the surface of the
control after first re-computing its size and coordinates.
You could also output text with the Print method,
call other graphics methods, and adjust persistent graphics objects such as
Images, PictureBoxes, Shapes, and Lines.
LISTING 13.3
REDRAWING A UserControl'S GRAPHICS IN ITS Paint EVENT PROCEDURE
Private Sub UserControl_Paint()
'Compute radius of ellipse (longest side)
Dim lRadius As Long
If UserControl.ScaleHeight < _
UserControl.ScaleWidth Then
lRadius = UserControl.ScaleWidth / 2
Else
lRadius = UserControl.ScaleHeight / 2
End If
'Save UserControl's existing fill style
Dim lOldFillStyle As Long
lOldFillStyle = FillStyle
'and set it to be diagonal lines
FillStyle = vbUpwardDiagonal
'save UserControl's existing FillColor
Dim lOldFillColor As Long
'and set it to red
lOldFillColor = FillColor
FillColor = vbRed
'draw an ellipse whose shape is
'proportional to the UserControl's shape
Circle (ScaleWidth / 2, ScaleHeight / 2), _
lRadius, vbRed, , , _
UserControl.ScaleHeight
/ _
UserControl.ScaleWidth
'restore previous FillStyle and Color
'settings of the UserControl
FillStyle = lOldFillStyle
FillColor = lOldFillColor
End Sub