关于内存DC

使用 CreateCompatibleDC 创建了内存DC之后,要再调用SelectObject选择一张位图放入此DC,然后才可以使用此DC进行绘制,之后绘制的数据会保存在内存中,

详细说明看后文。

 

在MFC中使用内存DC例子:

          // 创建内存DC
                CDC mMemDc;
                mMemDc.CreateCompatibleDC( &dc );

                // 创建兼容位图
                CBitmap bmpMemBmp;
                bmpMemBmp.CreateCompatibleBitmap( &mMemDc, 100, 100 );
                CBitmap* pOldBmp = mMemDc.SelectObject( &bmpMemBmp );

                // 在内存DC上绘图
                BOOL bRet = mMemDc.Ellipse( 0, 0, 100, 100 );

                // 从内存DC上拷贝至显示DC
                dc.BitBlt( 0, 0, 100, 100, &mMemDc, 0, 0, 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 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 CreateCompatibleBitmap 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 CreateCompatibleDC 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 SelectObject function. To create a bitmap of the appropriate dimensions, use the CreateBitmap, CreateBitmapIndirect, or CreateCompatibleBitmap 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 BitBlt 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 GetDeviceCaps with the NUMRESERVED value to get the number of reserved colors in the system. Then call GetSystemPaletteEntries 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 Colors. For more information about bitmaps and bitmap operations, see Bitmaps. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值