Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

Creating and Loading Bitmaps

Images are used extensively in Windows; a bitmap instance is another low-level GDI object represented by the HBITMAP handle.

Before you can use bitmaps in your application, you generally create a number of bitmap resources that are bound with your .EXE or .DLL when linked. These bitmaps then are loaded from the current module ready for drawing.

Creating a Bitmap Resource with the Resource Editor

You can create a new bitmap resource from the Insert Resource dialog box in the Visual Studio Resource Editor. You can change the size, the resource ID, and the source bitmap filename from the Bitmap Properties dialog box.

You then can draw the bitmap using the Resource Editor drawing tools, as Figure 6.5 shows.

06fig05.jpg

Figure 6.5 Editing a bitmap resource.

You also can open BMP, JPEG, GIF, and many other common formats and copy and paste the image into a new bitmap.

When you compile the program, the bitmap is bundled into the resulting executable file.

Loading a Bitmap

Before you can use a bitmap, you must load the bitmap resource from the executable file. The CBitmap class is another CgdiObject-derived class, and it wraps the HBITMAP GDI object handle. You can construct a CBitmap object using its default constructor and then load a specific bitmap by calling its LoadBitmap() function, passing a resource ID (or resource name) that identifies the specific bitmap.

If the bitmap is found and loaded from the executable module, LoadBitmap() returns a TRUE value, and the HBITMAP handle is valid. You also can use the LoadMappedBitmap() function to load color-mapped bitmaps and specify a set of COLORMAP structures to define the remapped COLORREF values.

You can load a system stock bitmap with the LoadOEMBitmap() function passing a flag value that loads a few standard bitmaps.

Creating Bitmaps

You can create a bitmap image without an associated resource by using one of the bitmap-creation functions. The CreateBitmap() function lets you create a bitmap in memory and specify its width, height, number of bit planes (color planes), number of color bits per pixel, and (optionally) an array of short values to initialize the bitmap image.

The CreateBitmapIndirect() function lets you specify these values in a BITMAP structure instead of through parameter settings.

You should use the CreateCompatibleBitmap() function to create bitmaps that are compatible with specific devices (such as the current display configuration). To do this, you must pass a pointer to a valid device context along with a width and height. This creates a bitmap that has a compatible color depth with a device attached to the device context.

Share ThisShare This

Informit Network