使用CreateCompatibleDC 创建了内存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
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.

最新文章

  1. vim环境变量配置、背景色配置
  2. 【腾讯GAD暑期训练营游戏程序班】游戏中的设计模式作业说明文档
  3. [MySQL]使用Begin...End语句的一个坑
  4. ngrok内网穿透利器在windws下的使用
  5. Scala 中 构造函数,重载函数的执行顺序
  6. php curl详细说明
  7. Linux命令(18)查看当前用户who、whoami、who am i
  8. Javascript获取浏览器窗口大小 获取屏幕,浏览器,网页高度宽度
  9. SGU 385 Highlander(期望)
  10. 关于ajax解析
  11. Android应用程序框架层和系统运行库层日志系统源代码分析
  12. 背景图片等比缩放的写法background-size简写法
  13. 深度学习框架-caffe安装-环境[Mac OSX 10.12]
  14. 高性能 Java 缓存库 — Caffeine
  15. 使用redis构建文章投票系统
  16. B2B、B2C、C2C、O2O
  17. BZOJ 5475: [WC 2019] 数树
  18. git使用遇到的坑
  19. 分离vue组件内部css
  20. linux 查找匹配文件中包含指定字符的 前五行,这里是指所有匹配的前五行

热门文章

  1. 深入理解vue路由的使用
  2. PS 如何制作柔和的边框
  3. 专訪阿里陶辉:大规模分布式系统、高性能server设计经验分享
  4. python(14)- 简单练习:登录账户失败三次,账户自动锁定
  5. Android SQLite性能分析
  6. EasyUI基础入门之Droppable(可投掷)
  7. NumPy事例练习
  8. PS 魔法棒
  9. React系列-ES6
  10. android mvp高速开发框架介绍(dileber的简单介绍)