Results 1 to 1 of 1

Thread: VB - Tile Window (Fast Image Tiler)

  1. #1

    Thread Starter
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    VB - Tile Window (Fast Image Tiler)

    The following code demonstrates how to use various Win32 API to create a very fast and effective tiling routine.

    Functionality includes being able to use an image containing multiple tiles of any dimension.

    Paste the following into a Standard Module:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Type RECT
    4.         Left As Long
    5.         Top As Long
    6.         Right As Long
    7.         Bottom As Long
    8. End Type
    9.  
    10. Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    11.  
    12. Private Declare Function BitBlt Lib "gdi32" _
    13.   (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, _
    14.    ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, _
    15.    ByVal dwRop As Long) As Long
    16.    
    17. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    18.  
    19. Private Declare Function CreateCompatibleBitmap Lib "gdi32" _
    20.   (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    21.  
    22. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    23. Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
    24. Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    25. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    26. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    27.  
    28. Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" _
    29.   (ByVal hInst As Long, ByVal lpsz As String, ByVal iType As Long, ByVal cx As Long, _
    30.    ByVal cy As Long, ByVal fOptions As Long) As Long
    31.  
    32. Private Const IMAGE_BITMAP = 0
    33. Private Const LR_LOADFROMFILE = &H10
    34.  
    35. Private Const SRCCOPY = &HCC0020
    36.  
    37. Public Sub fnTileWindow( _
    38.   ByVal lhWnd As Long, ByVal sTileFile As String, ByVal lTileNo As Long, _
    39.   Optional ByVal lTileWidth As Long = 32, _
    40.   Optional ByVal lTileHeight As Long = 32, _
    41.   Optional lTilesPerRow As Long = 1)
    42.  
    43.   ' lhWnd         = Handle of Window to Tile
    44.   ' sTileFile     = Location of Bitmap File to use which contains 1 or more tiles.
    45.   ' lTileNo       = Tile No. to use, (reading tiles left to right, top to bottom.)
    46.   ' lTileWidth    = Width of Each Tile in the Tile Bitmap
    47.   ' lTileHeight   = Height of Each Tile in the Tile Bitmap
    48.   ' lTilesPerRow  = No. of tiles in each row of the Bitmap
    49.  
    50.   Dim lDC As Long, lTileDC As Long
    51.   Dim lDestDC As Long
    52.   Dim lTileBmp As Long, lBmp As Long
    53.   Dim tRECT As RECT
    54.   Dim lSrcTileX As Long, lSrcTileY As Long
    55.   Dim x As Long, y As Long
    56.   Dim lTileCountX As Long, lTileCountY As Long
    57.   Dim lDestWidth As Long, lDestHeight As Long
    58.  
    59.   ' Determine the X/Y Coords of the Source Tile within the Tiles Image
    60.   lSrcTileX = ((lTileNo - 1) Mod lTilesPerRow) * lTileWidth
    61.   lSrcTileY = ((lTileNo - 1) / lTilesPerRow) * lTileHeight
    62.  
    63.   ' Get the Dimensions of the Client area of the window to tile
    64.   Call GetClientRect(lhWnd, tRECT)
    65.  
    66.   ' Calculate the Window's Width/Height
    67.   lDestWidth = tRECT.Right - tRECT.Left
    68.   lDestHeight = tRECT.Bottom - tRECT.Top
    69.  
    70.   ' Get the Window's DC
    71.   lDestDC = GetDC(lhWnd)
    72.   ' Create 2 Compatible Device Contexts', 1 for the Buffer the
    73.   ' other for the Tiles Image
    74.   lDC = CreateCompatibleDC(lDestDC)
    75.   lTileDC = CreateCompatibleDC(lDestDC)
    76.  
    77.   ' Load the Tiles Bitmap
    78.   lTileBmp = LoadImage(0, sTileFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE)
    79.  
    80.   ' Select the Tiles Bitmap into the Buffer DC
    81.   Call SelectObject(lDC, lTileBmp)
    82.   ' Create a Bitmap Compatible with the Tiles Bitmap which is as
    83.   ' big as the target window's client area in the Buffer DC
    84.   lBmp = CreateCompatibleBitmap(lDC, lDestWidth, lDestHeight)
    85.   ' Select the Buffer BMP into the Buffer DC
    86.   Call SelectObject(lDC, lBmp)
    87.  
    88.   ' Select the Tiles BMP into the Tiles DC
    89.   Call SelectObject(lTileDC, lTileBmp)
    90.  
    91.   ' Calculate how many tiles it'll take to tile the window
    92.   lTileCountX = lDestWidth / lTileWidth
    93.   lTileCountY = lDestHeight / lTileHeight
    94.  
    95.   ' Start drawing the tiles to the Buffer DC
    96.   For y = 0 To lTileCountY
    97.     For x = 0 To lTileCountX
    98.       BitBlt lDC, x * lTileWidth, y * lTileHeight, lTileWidth, lTileHeight, _
    99.        lTileDC, lSrcTileX, lSrcTileY, SRCCOPY
    100.     Next
    101.   Next
    102.   ' Copy the whole Buffer to the Destination Window in one go.
    103.   BitBlt lDestDC, 0, 0, lDestWidth, lDestHeight, lDC, 0, 0, SRCCOPY
    104.  
    105.   ' Clean Up the DC's and BMP's
    106.   Call DeleteDC(lDC)
    107.   Call DeleteDC(lTileDC)
    108.   Call ReleaseDC(lhWnd, lDestDC)
    109.   Call DeleteObject(lBmp)
    110.   Call DeleteObject(lTileBmp)
    111. End Sub
    Example Usage:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.   Static lTileNo As Long
    5.  
    6.   ' Toggle Between the 2 Tiles in the Tiles BMP
    7.   lTileNo = (lTileNo + 1) Mod 2
    8.   ' Tile the Form with this Tile
    9.   fnTileWindow hwnd, "C:\Tiles.bmp", lTileNo + 1
    10. End Sub
    Image for Tiles in this example attached.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width