Pattern Brushes

From HashVB
Jump to: navigation, search

What is a 'Pattern Brush'?

A pattern brush is a special type of brush in GDI that tiles a pattern when you draw with it. This can be used to draw patterns (as the name implies) or to tile images (which is a lot simpler than manually tiling images).

How to create and use a Pattern Brush

Here's the code:

   Dim p           As StdPicture
   Dim hPatBrush   As Long
   Dim r           As RECT
   Dim pt          As POINTAPI
   
   '-- Load the picture/pattern you want to tile
   Set p = LoadPicture(Your File Name Here)
   '-- Create the pattern brush from the bitmap
   hPatBrush = CreatePatternBrush(p.Handle)
   
   SetRect r, 0, 0, Me.ScaleWidth \ Screen.TwipsPerPixelX, Me.ScaleHeight \ Screen.TwipsPerPixelY
   '-- Must SetBrushOrgEx to (0, 0) to avoid it drawing away from the form's edge (duh!)
   SetBrushOrgEx Me.hdc, 0, 0, pt
   '-- Fill the rect with the brush
   FillRect Me.hdc, r, hPatBrush
   
   '-- Clean up
   DeleteObject hPatBrush
   Set p = Nothing

Pretty much self explanatory...