?? ch15.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD> <META NAME="GENERATOR" Content="Symantec Visual Page Mac 1.1"> <TITLE>Teach Yourself Visual C++® 5 in 24 Hours -- Hour 15 -- Using Bitmaps</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><H1 ALIGN="CENTER"><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM"BORDER="0"><BR><FONT COLOR="#000077">Teach Yourself Visual C++® 5 in 24 Hours</FONT></H1><CENTER><P><A HREF="../ch14/ch14.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch16/ch16.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <HR></CENTER><H1 ALIGN="CENTER"><FONT COLOR="#000077">- Hour 15 -<BR>Using Bitmaps</FONT></H1><P>Bitmaps are one of the most important GDI objects offered by Windows. In thishour, you will learn<UL> <LI>The structures used by Windows bitmaps<BR> <BR> <LI>How to use the MFC <TT>CBitmap</TT> class to simplify bitmap handling<BR> <BR> <LI>How to use color palettes<BR> <BR> <LI>How to solve problems that occur when using 256-color bitmaps in Windows</UL><P>Bitmaps can be a complex topic--this hour presents a C++ class that greatly simplifiesthe use of bitmaps that can be used in your projects. You will use this class ina sample program that demonstrates how to handle 256-color bitmaps.<H2><FONT COLOR="#000077"><B>What Is a Bitmap?</B></FONT></H2><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>bitmap</I> is a graphicalobject that can be used to represent an image in programs written for Windows. Usinga bitmap, an image can be easily stored, loaded, and displayed. A bitmap is onlyone of many graphical objects available when writing Windows programs. The followingare some other types of graphical objects:<UL> <LI>Pens and brushes, used to draw lines and fill areas. Pens and brushes are covered in detail in Hour 12, "Using Pens and Brushes."<BR> <BR> <LI>Fonts, used to provide the characteristics for displayed text in a Windows program. Fonts are discussed in Hour 13, "Fonts."<BR> <BR> <LI>Icons and cursors, small bitmap images that are used for special purposes in an application. Icons and cursors are discussed in Hour 14, "Icons and Cursors."</UL><P>Bitmaps provide a flexible way to store image data in a Windows program. The datastructure used for a bitmap is straightforward and enables a wide variety of bitmaptypes to be stored.<BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>Although image lists (described in Hour 17, "Using Image Lists and Bitmaps") provide extra features, usually a simple bitmap is the easiest way to display an image on the screen. <HR></BLOCKQUOTE><H3><FONT COLOR="#000077"><B>Visual C++ Support for Bitmaps</B></FONT></H3><P>The easiest way to create a bitmap is to use an image editor like the one thatis integrated into the Developer Studio. The image editor is used in this chapterto create a bitmap that is displayed in a dialog box-based program's main dialogbox.</P><P>After you've created the bitmap using the image editor, you can manipulate itby using the MFC <TT>CBitmap</TT> class. You can load the bitmap into your programby calling the <TT>LoadBitmap</TT> member function:</P><PRE><FONT COLOR="#0066FF"><TT>bmpHello.LoadBitmap( IDB_HELLO );</TT></FONT></PRE><P>After the bitmap has been loaded, it can be displayed to any output device byusing a device context.<H3><FONT COLOR="#000077"><B>Adding a Bitmap to a Sample Project</B></FONT></H3><P>You can display a bitmap in any project that handles the <TT>WM_PAINT</TT> message.As an example, create an SDI program named Bitmap, following the steps used in Hour1, "Introducing Visual C++ 5." After you have created the project, openthe resource tree by clicking the ResourceView tab in the project workspace window.</P><P>Insert a new bitmap resource into the project by right-clicking the resource treeand selecting Insert from the shortcut menu. An Insert Resource dialog box is displayed;select Bitmap and click the New button. A new bitmap will be inserted into the projectand loaded for editing.</P><P>The image editor displays a large grid that represents the bitmap surface, aswell as two dockable toolbars:<UL> <LI>The Graphics toolbar consists of tools you can use to draw different shapes and text.<BR> <BR> <LI>The Colors palette contains the colors that are available for drawing the bitmap.</UL><P>Both toolbars can be used as either floating palettes or docked toolbars.</P><P>You can change the properties for a bitmap resource by double-clicking the edgeof the bitmap grid or by right-clicking the bitmap's edge and selecting Propertiesfrom the pop-up menu. Change the name of the bitmap resource to <TT>IDB_HELLO</TT>.Select the text tool from the Graphics toolbar, and choose your favorite color fromthe Colors palette. Type a hello message, as shown in Figure 15.1.</P><P><A NAME="01"></A><A HREF="01.htm"><B>Figure 15.1.</B></A> <BR><I>The <TT>IDB_HELLO</TT> bitmap used in the Bitmap sample program.</I><BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>The font shown in Figure 15.1 is 18-point Times New Roman Italic. <HR></BLOCKQUOTE><P>As with most resources, you can adjust the size of the bitmap by dragging theedges of the grid in any direction. Change the size of the bitmap so that the textfits inside the bitmap without any clipping. You can select a different font by pressingthe Font button on the text tool. Feel free to add other effects by selecting othertools from the Graphics toolbar.<BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>Using names that begin with <TT>IDB_</TT> for bitmaps is a standard naming convention in Windows programming. <HR></BLOCKQUOTE><H3><FONT COLOR="#000077"><B>Loading and Displaying a Bitmap</B></FONT></H3><P>Open the <TT>BitmapView.cpp</TT> source file, and locate the member function <TT>CBitmapView::OnDraw</TT>.The function should already contain several lines of source code, which you can replacewith the function provided in Listing 15.1.<H4><FONT COLOR="#000077">TYPE: Listing 15.1. The CBitmapView::OnDraw function, usedto display a bitmap.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>void CBitmapView::OnDraw(CDC* pDC)</TT><TT>{</TT><TT> CBitmap bmpHello;</TT><TT> bmpHello.LoadBitmap( IDB_HELLO );</TT><TT> // Calculate bitmap size using a BITMAP structure.</TT><TT> BITMAP bm;</TT><TT> bmpHello.GetObject( sizeof(BITMAP), &bm );</TT><TT> // Create a memory DC, select the bitmap into the</TT><TT> // memory DC, and BitBlt it into the view.</TT><TT> CDC dcMem;</TT><TT> dcMem.CreateCompatibleDC( pDC );</TT><TT> CBitmap* pbmpOld = dcMem.SelectObject( &bmpHello );</TT><TT> pDC->BitBlt( 10,10, bm.bmWidth, bm.bmHeight,</TT><TT> &dcMem, 0,0, SRCCOPY );</TT><TT> // Reselect the original bitmap into the memory DC.</TT><TT> dcMem.SelectObject( pbmpOld );</TT><TT>}</TT></FONT></PRE><P>Before the bitmap is displayed in Listing 15.1, information about the bitmap iscollected using the <TT>SelectObject</TT> member function, which fills a <TT>BITMAP</TT>structure with information. Two pieces of useful information the <TT>BITMAP</TT>structure can provide are the width and height of the bitmap.</P><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>memory device context</I>,or <I>memory DC</I>, is a memory location that allows for images to be drawn off-screen,which improves performance.</P><P>When displaying a bitmap, the bitmap is first selected into a memory DC. The <TT>BitBlt</TT>function is used to transfer the image from the memory DC to the view's device context,passed as a parameter to <TT>OnDraw</TT>. <TT>BitBlt</TT> is an abbreviation forBit-Block Transfer, which is the process used to move the image from the memory DCto the actual device context. The first two parameters passed to <TT>BitBlt</TT>are the coordinates of the destination rectangle.</P><P>When you compile and run the Bitmap project, the <TT>IDB_HELLO</TT> bitmap isdisplayed in the upper-right corner of the view window. Experiment by changing thesize and color of the bitmap or by combining the source code provided in Listing15.1 with the <TT>DrawText</TT> function.</P><P>If you experiment with the Bitmap program long enough, you might discover a problemthat occurs when using bitmaps. Although the background of the <TT>IDB_HELLO</TT>bitmap is white, it isn't transparent. If the color of the view background is grayor another color, the bitmap will look like a white square containing text. It ispossible to display a bitmap with a transparent background, although it takes a greatdeal of advanced graphics work. Fortunately, the image list, first introduced forWindows 95, has the capability to draw a transparent bitmap. Image lists are thoroughlydiscussed in Hour 17, "Using Image Lists and Bitmaps."<H2><FONT COLOR="#000077"><B>DDBs Versus DIBs</B></FONT></H2><P>Bitmaps come in two basic flavors: Device-Independent Bitmaps (DIB) and Device-Dependent Bitmaps (DDB). In earlier versions of 16-bit Windows, only DDBs were supported.Beginning with Windows 3.0, and on all versions of Windows NT, DIBs are supported.<H3><FONT COLOR="#000077"><B>The DDB Problem</B></FONT></H3><P>A DDB is tightly coupled to the device on which it is intended to be displayed.The memory that is used to store the bitmap is actually allocated by the device driver,and an application that must change the contents of the bitmap must do so indirectly,a slow and inefficient process. Figure 15.2 shows how a DDB is controlled by a devicedriver and that the application has only indirect access to the bitmap.</P><P>One of the problems with DDBs is that an application must supply bitmaps in aformat supported by the driver. The application must either store bitmaps in multipleformats or it must be capable of converting a bitmap from one format into another.Either way, dealing with a DDB can be difficult and time consuming.</P><P><A NAME="02"></A><A HREF="02.htm"><B>Figure 15.2.</B></A> <I><BR>A device-dependent bitmap is controlled by the device driver.</I><H3><FONT COLOR="#000077"><B>The DIB Solution</B></FONT></H3><P>To work around these problems, all versions of Windows since the Jurassic era(Windows 3.0) support DIBs. A DIB has a known structure that can be converted easilyinto a DDB whenever necessary. A DIB can exist in two formats: the Windows formatand the OS/2 format. Because the OS/2 format is rarely used, the examples in thishour assume the DIB is in the Windows format. A DIB bitmap stored in a file consistsof four structures, as shown in Figure 15.3.</P><P><A NAME="03"></A><A HREF="03.htm"><B>Figure 15.3.</B></A> <I><BR>DIBs contain four data structures.</I><H3><FONT COLOR="#000077"><B>The <TT>BITMAPFILEHEADER</TT> Structure</B></FONT></H3><P>The <TT>BITMAPFILEHEADER</TT> structure is used only when the bitmap is read orstored to disk. When a DIB is manipulated in memory, the <TT>BITMAPFILEHEADER</TT>structure is often discarded. The remaining parts of the DIB structure follow thesame format whether they're located in memory or in a disk file.<H3><FONT COLOR="#000077"><B>The <TT>BITMAPINFO</TT> Structure</B></FONT></H3><P>The <TT>BITMAPINFO</TT> structure contains a <TT>BITMAPINFOHEADER</TT> and zeroor more palette values for pixels stored in the bitmap. <TT>BITMAPINFOHEADER</TT>contains information about the dimensions, color format, and compression for thebitmap.</P><P>After the <TT>BITMAPINFOHEADER</TT> structure, the <TT>bmiColors</TT> variablemarks the beginning of the color table. This table is used if the bitmap isn't a16-, 24-, or 32-bits-per-pixel bitmap. The color table is an array of <TT>RGBQUAD</TT>structures, with each entry storing one of the colors used by the bitmap. The membersof the <TT>RGBQUAD</TT> structure are</P><PRE><FONT COLOR="#0066FF"><TT>BYTE rgbBlue;</TT><TT>BYTE rgbGreen;</TT><TT>BYTE rgbRed;</TT><TT>BYTE rgbReserved; // Always set to zero</TT></FONT></PRE><P>The members of the <TT>RGBQUAD</TT> structure represent the red, green, and bluecolor intensity for a color stored in the color table. Each structure member hasa range of 0-255. If all members have a value of 0, the color is black; if all membershave a value of 255, the color is white.<H3><FONT COLOR="#000077"><B>The DIB Image Array</B></FONT></H3><P>An array of pixel information follows the color table. Every pixel in the bitmapis represented by one element of this array. Each element contains a value that representsone of the color map entries. If the first element in the array has a value of 32,the first pixel in the bitmap will use the color found in color table entry number32, as shown in Figure 15.4.</P><P><A NAME="04"></A><A HREF="04.htm"><B>Figure 15.4.</B></A> <I><BR>Every entry in the image array refers to a pixel in the displayed image.</I></P><P>If this is a 16-, 24-, or 32-bits-per-pixel bitmap, there was no color table andeach element of the array contains the RGB color for a single pixel. In effect, thepalette has been moved out to the pixel array for these types of bitmaps.<H3><FONT COLOR="#000077"><B>256-Color DIBs</B></FONT></H3><P>You might think that manipulating a 256-color bitmap is just as easy as loadinga 16-color bitmap using the MFC <TT>CBitmap</TT> class. Unfortunately, that's notthe case. When a 256-color DIB is displayed on a 256-color device, the colors arealmost never correct because of how Windows handles the color palette.<H3><FONT COLOR="#000077"><B>An Overview of the Windows Color Palette</B></FONT></H3><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>Windows uses <I>color palettes</I>to track the colors that are displayed for a particular window.</P><P>Before examining the C++ class used to display 256-color bitmaps, it's importantto discuss how Windows determines the colors available to your application. Unfortunately,when a bitmap is loaded, Windows makes no special effort to make sure that colorentries in the bitmap's color table are added to the system's color palette. Theresult is an ugly-looking bitmap.<BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Time Saver:</B></FONT><B> </B>To display a 256-color bitmap, you must always create and manage a logical palette for your application. <HR></BLOCKQUOTE><P>The Windows GDI uses palettes to manage color selection for 256-color devices.There are actually several different types of palettes, as shown in Figure 15.5.</P><P><A NAME="05"></A><A HREF="05.htm"><B>Figure 15.5.</B> </A><BR><I>The different types of color palettes used in Windows.</I></P><P>Three different types of palettes are shown in Figure 15.5:<UL> <LI>Device drivers that use palettes have an internal palette that stores the current set of colors available for display. This means that 256-color devices have 256 entries in the hardware palette.<BR> <BR> <LI>The Windows NT palette maintains a system palette that matches the hardware palette. When this palette is updated, the operating system will also take the necessary steps to have the device driver update its internal palette.<BR> <BR> <LI>Every application can have one or more logical palettes. An application interacts with the system palette in order to control the colors that are currently available for display.</UL><BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>To maintain some level of consistency, Windows reserves the first 10 and last 10 palette entries for its own use, leaving 236 palette entries for application use, as shown in Figure 15.6.
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -