- Table of Contents
- Copyright
- About the Authors
- About the Contributors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- How to Use This Book
- What You Need to Use This Book
- What's New in Visual C++ 6.0
- Contacting the Main Author
- Part I: Introduction
- Chapter 1. The Visual C++ 6.0 Environment
- Part II: MFC Programming
- Chapter 2. MFC Class Library Overview
- Chapter 3. MFC Message Handling Mechanism
- Chapter 4. The Document View Architecture
- Chapter 5. Creating and Using Dialog Boxes
- Chapter 6. Working with Device Contexts and GDI Objects
- Chapter 7. Creating and Using Property Sheets
- Chapter 8. Working with the File System
- Chapter 9. Using Serialization with File and Archive Objects
- Part III: Internet Programming with MFC
- Chapter 10. MFC and the Internet Server API (ISAPI)
- Chapter 11. The WinInet API
- Chapter 12. MFC HTML Support
- Part IV: Advanced Programming Topics
- Chapter 13. Using the Standard C++ Library
- Chapter 14. Error Detection and Exception Handling Techniques
- Chapter 15. Debugging and Profiling Strategies
- Chapter 16. Multithreading
- Chapter 17. Using Scripting and Other Tools to Automate the Visual C++ IDE
- Part V: Database Programming
- Chapter 18. Creating Custom AppWizards
- Chapter 19. Database Overview
- Chapter 20. ODBC Programming
- Chapter 21. MFC Database Classes
- Chapter 22. Using OLE DB
- Chapter 23. Programming with ADO
- Part VI: MFC Support for COM and ActiveX
- Chapter 24. Overview of COM and Active Technologies
- Chapter 25. Active Documents
- Chapter 26. Active Containers
- Chapter 27. Active Servers
- Chapter 28. ActiveX Controls
- Part VII: Using the Active Template Library
- Chapter 29. ATL Architecture
- Chapter 30. Creating COM Objects Using ATL
- Chapter 31. Creating ActiveX Controls Using ATL
- Chapter 32. Using ATL to Create MTS and COM+ Components
- Part VIII: Finishing Touches
- Chapter 33. Adding Windows Help
- Part IX: Appendix
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.
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.
Drawing with Bitmaps | Next Section

Account Sign In
View your cart