VBfx / Tile tutorial / Step 2

Bitmap manager

We're going a little deeper into the API here, loading bitmaps isn't that easy. We have to hold the objects loaded with the bitmap for successfully releasing it later. Of course we could just exit the application but this may cause huge memory leaks and that's just crap. You may or may not know how the API calls work, I'll try to explain how it works as good as possible but it doesn't matter if you don't get it all.

This is the internal bitmap info Type that'll hold any information for the API calls:

Public Type tBitmapInfo
    DC as Long
    MemoryDC as Boolean
    OriginalObject as Long
    PictureDisp as IPictureDisp
    CompatibleBitmap as Long
End Type

Too complicated? Well I didn't create that Type, it's the API that says what we need. Just don't care about this and read on, at the end you'll get the point. Even I don't know this code by hard ;)

Another bitmap type

Since the bitmap info mentioned above is for internal purpose only we need to make our own Type that has some data we can use. I'm talking about the file name and bitmap size for now but we'll extend this Type later I suppose. Note that the tBitmapInfo from above is included in this Type so it can hold this data, too. I won't explain this Type since the properties should be quite clear.

Public Type tBitmap
    FileName as String
    'Bitmap info
    Picture as tBitmapInfo
    w as Long
    h as Long
End Type


Here again we just declared the Types and no variables yet. Besides the array of bitmaps we also need a window reference here, this will be needed later when converting the size into pixels and such. Note that this window reference is declared Private, that's because we'll only set it once when initializing the Module and because we don't need to access it from outside the Module.

Public BitmapCount as Long
Public Bitmap() as tBitmap

Private MainWindow as Form

API calls

Not to forget the API calls we need, however I'll explain these later in the tutorial.

Private Declare Function CreateCompatibleDC Lib "gdi32" ( ByVal iDC as Long ) as Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" ( ByVal iDC as Long, _
    ByVal iWidth as Long, ByVal iHeight as Long ) as Long

Private Declare Function SelectObject Lib "gdi32" ( ByVal iDC as Long, ByVal iObject as Long ) as Long
Private Declare Function DeleteObject Lib "gdi32" ( ByVal iObject as Long ) as Long
Private Declare Function DeleteDC Lib "gdi32" ( ByVal iDC as Long ) as Long