Visual Basic Tip: 03/04/96


Tile a Bitmap to a Form Background.

For the best effect, use a seamless bitmap image (ex: Windows wallpaper
bitmap).  Create a form1.frm and a module1.bas.  Add a picture box to the
form.  Rename the picture box to "p".  Load a bitmap into the picture box.
The loaded bitmap will be tiled onto the form background.  Copy the code 
listed below into the appropriate places (see intruction comments below).

Let me know if you find an optimization that performs significantly faster!


  'module declaration
  Declare Function BitBlt Lib "GDI" (ByVal hDestDC As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As Integer, ByVal XSrc As Integer, ByVal YSrc As Integer, ByVal dwRop As Long) As Integer


  '**form with a bitmap named "p" containing the bitmap to be tiled:**

  'form level general declarations
  Dim maxhgt As Long, maxwid As Long
  Dim pwid As Integer, phgt As Integer

  Sub Form_Load ()
   p.ScaleMode = 3
   p.Visible = False
   p.AutoSize = True
   p.AutoRedraw = True
   pwid = p.ScaleWidth
   phgt = p.ScaleHeight 
  End Sub

  Sub Form_Paint ()
   phDC& = p.hDC
   frmhdc& = hdc

   For j% = 0 To maxhgt Step phgt
    For i% = 0 To maxwid Step pwid
     X% = BitBlt(frmhdc&, i%, j%, pwid, phgt, phDC&, 0, 0, &HCC0020)
    Next
   Next
  End Sub

  Sub Form_Resize ()
   maxhgt = Height \ screen.TwipsPerPixelY
   maxwid = Width \ screen.TwipsPerPixelX
  End Sub

Return to Phil's Web Page

Created by: Philip