Bitmap Resources

A bitmap is a kind of picture. You can create resources from bitmaps and include them in your programs (or DLLs). Generally you do this to create user interface elements like tool bars with custom graphics. The other way to use bitmaps is to load them directly from a file (surprisingly there is no Win32 API function to do this), but that is a different story.

Creating a bitmap is simply a matter of using some graphics program (the Paint program included with Windows will do) to draw an image and save if in the Windows Bitmap file format (with the extension .bmp generally). Then you include the bitmap in your resource script using the BITMAP keyword. For example:

#include "resources.h"

BITMAP_ID BITMAP "test.bmp"

That command line includes the bitmap file test.bmp as a bitmap resource in the program. In the resources.h header file BITMAP_ID is defined as some integer, e.g.:

#define BITMAP_ID 10

Then, to use the bitmap in your program you use LoadBitmap. The LoadBitmap function does not load a bitmap from a .bmp file as you might expect, but loads a bitmap resource with the given ID from the file associated with the instance handle supplied. For example, to get a bitmap handle for the bitmap resource created by the above resource script you might use code like this:

#include <windows.h>
#include "resources.h"

/* ...other code, including the beginning of the function... */

     /* Get the instance handle for this program */
    HINSTANCE hInstance = (HINSTANCE) GetModuleHandle (NULL);

    HBITMAP hbmp;

    hbmp = LoadBitmap (hInstance, MAKEINTRESOURCE(BITMAP_ID));

/* ...more code using hbmp... */

    DeleteObject (hbmp); /* Delete objects when you're finished with them. */

/* ...more code until the end of the function... */

Once you have a bitmap handle you can select the bitmap into a device context and then copy the bitmap bits onto a window (or do various other things to it). I'll describe how to use bitmaps (and what a device context is for that matter) when I get around to writing the section of the tutorial on drawing graphics with the Win32 GDI (Graphics Device Interface) functions.