Wednesday, December 17, 2008

[vb6] When to do Refresh in response to Resize

When a form or picture box is resized, it is automatically invalidated. This means that the Paint event handler is called immediately after the Resize event handler. However, this is only true if either the width or height (or both) were increased by the resize operation. This may be fine, if that is what you want, but in many situations you need to repaint the form / picturebox also when it is made smaller. You cannot simply do a Refresh in the Resize event handler, cause that would entail a duplicate repainting when the form or picturebox is enlarged in one or both dimensions. Instead, you must cheque the new size and compare it to the previous size, refreshing only when appropiate. This example is for a picture box named picCar:
    Private Sub picCar_Resize()
Static OldPictureSizeX As Long
Static OldPictureSizeY As Long
If picCar.Width <= OldPictureSizeX And picCar.Height <= OldPictureSizeY Then
picCar.Refresh
End If
OldPictureSizeX = picCar.Width
OldPictureSizeY = picCar.Height
End Sub

No comments:

Post a Comment