How to to tile a picture on a form
Applies to: Visual Basic 5.0 and 4.0
The following steps and code will
demonstrate how to tile a picture, wallpaper-style, on a form, and maintain it if the form
is resized or repainted. Similar code could be use to place multiple pictures in
varying patterns and sizes, by way of creating a simple screensaver.
1 - Start a new Visual Basic project.
2 - Add a BAS module and in it place the following code:
Public Sub TileForm(ByVal FormName As Form, ByVal PicBox As Control)
numdown As Integer
x, y As Integer
'Determine the number of rows and columns required
'to cover the form
numacross = Int(FormName.Width / PicBox.Width) + 1
numdown = Int(FormName.Height / PicBox.Width) + 1
'Loop through each row down
= 0 To numdown - 1
y = PicBox.Height * d
For i = 0 To numacross - 1
each down iteration, loop and populate the
x = i * PicBox.Width
where we put the picture at each consecutive position!
PicBox.Picture, x, y
3 - On Form1, draw a picturebox, Picture1,
and set its Visible property to false and its AutoSize property to true.
4 - Set the Picture property of Picture1 to a small bitmap or an icon; not something so
large that tiling would be pointless.
5 - Leave the AutoRedraw property of Form1 set to False.
6 - Place the following code in both the Paint and Resize events of Form1:
'Pass the form to be affected and the
'picture object to the tiling routine
7 - Test what we have so far. Watch
the behavior when the form is resized or covered by another window and then uncovered
again. Now stop the project and let's take it further.
8 - Add another form, Form2. In the click event of Form1, code to show Form2.
9 - On Form2, draw a picturebox, Picture1, and set its Visible property to False and its
AutoSize property to True. Note that an image control may also be used in this
10 - Set the AutoRedraw property of Form2 to True.
11 - In only the Resize event of Form2, place the following code:
12 - Test the project. When Form1
comes up, click on it to show Form2. Put both forms through their paces, and note
the difference. Either method is viable for tiling and keeping things updated, but
the flicker that can occur with tiling implemented in the Paint event may make use of
AutoRedraw more pleasing.
With the generic routine in a module, it's
now ready to plug into any project and be applied with minimal effort.