使用 创建了内存DC之后,要再调用SelectObject选择一张位图放入此DC,然后才可以使用此DC进行绘制,之后绘制的数据会保存在内存中,
详细说明看后文。
在MFC中使用内存DC例子:
// 创建内存DC
CDC mMemDc;
mMemDc.CreateCompatibleDC( &dc ); // 创建兼容位图
CBitmap bmpMemBmp;
bmpMemBmp.CreateCompatibleBitmap( &mMemDc, , );
CBitmap* pOldBmp = mMemDc.SelectObject( &bmpMemBmp ); // 在内存DC上绘图
BOOL bRet = mMemDc.Ellipse( , , , ); // 从内存DC上拷贝至显示DC
dc.BitBlt( , , , , &mMemDc, , , SRCCOPY ); // 恢复
// When you finish with the CBitmap object created with the
// CreateCompatibleBitmap function, first select the bitmap out of the device context, then delete the CBitmap object.
mMemDc.SelectObject( pOldBmp );
以下内容来自msdn:
CreateCompatibleDC
The CreateCompatibleDC function creates a memory device context (DC) compatible with the specified device.
HDC CreateCompatibleDC(
HDC // handle to DC
);
Remarks
A memory DC exists only in memory. When the memory DC is created, its display surface is exactly one monochrome pixel wide and one monochrome pixel high. Before an application can use a memory DC for drawing operations, it must select a bitmap of the correct width and height into the DC. To select a bitmap into a DC, use the function, specifying the height, width, and color organization required.
When a memory DC is created, all attributes are set to normal default values. The memory DC can be used as a normal DC. You can set the attributes; obtain the current settings of its attributes; and select pens, brushes, and regions.
Memory Device Contexts
To enable applications to place output in memory rather than sending it to an actual device, use a special device context for bitmap operations called a memory device context. A memory DC enables the system to treat a portion of memory as a virtual device. It is an array of bits in memory that an application can use temporarily to store the color data for bitmaps created on a normal drawing surface. Because the bitmap is compatible with the device, a memory DC is also sometimes referred to as a compatible device context.
The memory DC stores bitmap images for a particular device. An application can create a memory DC by calling the function.
The original bitmap in a memory DC is simply a placeholder. Its dimensions are one pixel by one pixel. Before an application can begin drawing, it must select a bitmap with the appropriate width and height into the DC by calling the function. To create a bitmap of the appropriate dimensions, use the , , or function. After the bitmap is selected into the memory DC, the system replaces the single-bit array with an array large enough to store color information for the specified rectangle of pixels.
When an application passes the handle returned by CreateCompatibleDC to one of the drawing functions, the requested output does not appear on a device's drawing surface. Instead, the system stores the color information for the resultant line, curve, text, or region in the array of bits. The application can copy the image stored in memory back onto a drawing surface by calling the function, identifying the memory DC as the source device context and a window or screen DC as the target device context.
When displaying a DIB or a DDB created from a DIB on a palette device, you can improve the speed at which the image is drawn by arranging the logical palette to match the layout of the system palette. To do this, call with the NUMRESERVED value to get the number of reserved colors in the system. Then call and fill in the first and last NUMRESERVED/2 entries of the logical palette with the corresponding system colors. For example, if NUMRESERVED is 20, you would fill in the first and last 10 entries of the logical palette with the system colors. Then fill in the remaining 256-NUMRESERVED colors of the logical palette (in our example, the remaining 236 colors) with colors from the DIB and set the PC_NOCOLLAPSE flag on each of these colors.
For more information about color and palettes, see . For more information about bitmaps and bitmap operations, see .