- 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
Drawing with Bitmaps
After you create a GDI bitmap object you can draw with it using a memory device context and then copy it to a destination device context. A bitmap is copied to a device using the Bit Block Transfer functions. These functions are commonly known as BitBlt (pronounced bit-blit) functions. These functions, such as BitBlt() and StretchBlt() are discussed in the next section.
As you read earlier in this chapter, you can create a compatible memory device context by using the CreateCompatibleDC() function.
When a memory device context is created, it is initialized with a monochrome bitmap. You then should select your custom bitmap into the device context (saving the pointer returned to the old bitmap) and perform the copy into the screen device context. After you finish, you must select the original monochrome bitmap back into the device context (thus deselecting your custom bitmap) before deleting the CBitmap object or calling DeleteObject() to destroy the GDI bitmap object.
Bitmap Copying
After you select a valid bitmap into a memory-based device context, you can use the BitBlt() device-context function to transfer all or part of the image onto a display-based (or printer-based) device context. BitBlt() lets you specify the source device context (your memory DC), the source coordinates to copy from (relative to the memory DC), the destination coordinates (relative to the destination DC), and the width and height describing the area to copy.
You also can specify a raster operation flag that can be used to invert the image during copying, merge the source with the destination image, just copy the source to the destination, or perform a number of other logical operations between the source and destination during the copy.
The StretchBlt() function lets you specify a source width and height, as well as a destination width and height. The image then expands or shrinks to fit the destination width and height. This method provides a fast and easy way to perform zoom operations. You can change the technique used to copy the image area by passing a flag value to the SetStretchBltMode() function. This lets you perform color-averaging stretch copies that give a better image representation (at the expense of speed) or allow a number of different stretching techniques.
You can use the MaskBlt() function to perform an image copy from a source device context through the holes in a mask provided by a monochrome bitmap into the destination device context.
The following code fragment shows how to create a memory device context, load a re source bitmap, and select it into the memory device context. Then you use StretchBlt() to stretch it so that it fills the view.
void CBitmapdemoView::OnDraw(CDC* pDC)
{
// Create a compatible memory device context
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);
// Load and select the bitmap resource
CBitmap bmImage;
bmImage.LoadBitmap(IDB_TSTBITMAP);
CBitmap *pbmOriginal = dcMemory.SelectObject(&bmImage);
CRect rcClient;
GetClientRect(rcClient);
pDC->StretchBlt(0,0,rcClient.Width(),rcClient.Height(),
&dcMemory,0,0,48,48,SRCCOPY);
pDC->SelectObject(pbmOriginal);
}
Creating a Device-Independent Bitmap Class | Next Section

Account Sign In
View your cart