打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
关于memory device context内存设备上下文和位图
关于memory device context内存设备上下文和位图

英文原文来自MSDN,我自己翻译了一下。

关于内存DC:
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.
为了能够让应用程序在内存中而不是在实际设备中绘图,使用一种称作内存设备内容(memory device context)的特殊设备内容来针对位图操作。内存DC允许系统分配一部分内存来作为虚拟DC,内存DC是应用程序可以临时为在普通绘图区域创建的位图保存色彩数据而在内存中保存的一系列的比特。由于位图和设备是兼容的,所以一个内存DC有时也被称作兼容设备内容。

The memory DC stores bitmap images for a particular device. An application can create a memory DC by calling the CreateCompatibleDC function.
内存DC为特定的设备保存位图,一个应用程序可以通过调用CreateCompatibleDC函数创建一个内存DC。

关于CreateCompatibleDC函数:
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.
内存DC只在内存中存在。当一个内存DC被创建时,它的显示区域只有一个像素宽一个像素高。在一个应用程序使用内存DC进行绘图操作之前,必须选入一副正确高度和宽度的位图到内存DC中。为了选入一副位图到内存DC中,可以使用CreateCompatibleBitmap函数。使用CreateCompatibleBitmap函数,指定所需位图的高度,宽度和色彩组织。
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.
当一个内存DC被创建之后,所有的属性都被设置为默认属性。内存DC可以像普通的DC那样使用。你可以设置属性,获得当前的设置或者当前属性,并且可以选入笔,画刷和区域。

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.

内存DC中最初的位图只是简单的一个放置场所。它的只有1 x 1大小,在一个应用程序开始绘图之前,必须通过SelectObject函数选入一个适当高度和宽度大小的位图到内存DC中。如果想要创建一个合适大小的位图,可以使用 CreateBitmap, CreateBitmapIndirect, 或者CreateCompatibleBitmap 函数。当一个位图被选入到内存DC中,系统会使用能够保存特定大小的色彩信息的足够大的数组来替换原先单独位数的数组。

CreateCompatibleBitmap函数:
Note: When a memory device context is created, it initially has a 1-by-1 monochrome bitmap selected into it. If this memory device context is used in CreateCompatibleBitmap, the bitmap that is created is a monochrome bitmap. To create a color bitmap, use the hDC that was used to create the memory device context, as shown in the following code:

注意:当一个内存DC被创建时,它最初只有一个1X1的单色位图在里面,如果这个内存DC被CreateCompatibleBitmap函数使用的话,返回的位图就只能是单色的。所以为了创建一个彩色的位图,就要使用创建内存DC时所使用的hDC来创建。就像以下代码一样:

    HDC memDC = CreateCompatibleDC ( hDC );
    HBITMAP memBM = CreateCompatibleBitmap ( hDC, nWidth, nHeight );//注意这里的hDC就是创建内存DC时使用的DC
    SelectObject ( memDC, memBM );

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.

当应用程序把通过CreateCompatibleDC函数返回的句柄传递到绘图函数中,请求的输出并不是在设备的绘图表面上,相反,系统会把响应的直线,弧线,文本的色彩信息保存到内存中。应用程序可以使用BitBlt函数从内存中把图像复制到一个绘图表面,把内存DC作为源DC,窗口或者屏幕DC作为目标DC。

所有,截取屏幕的代码应为(在WM_PAINT消息的处理过程中添加):
   hdc = BeginPaint(hWnd, &ps);
   HDC hMemDC;
   HDC hScreenDC;
   HBITMAP hBitmap;
   HBITMAP bitmap;

   hMemDC=CreateCompatibleDC(hdc);
   hBitmap=CreateCompatibleBitmap(hdc,1024,768);
   SelectObject(hMemDC,hBitmap);
  
   //获取屏幕DC,并且将屏幕DC的内容复制到内存DC中
   hScreenDC=GetDC(NULL);
   BitBlt(hMemDC,0,0,1024,768,hScreenDC,0,0,SRCCOPY);
   //将内存DC复制到窗体的DC中,在窗体中显示屏幕内容
   BitBlt(hdc,0,0,1024,768,hMemDC,0,0,SRCCOPY);
   //创建一个兼容位图来保存内存DC中的图像
   hBitmap=CreateCompatibleBitmap(hdc,1024,768);
   bitmap=(HBITMAP)SelectObject(hMemDC,hBitmap);
   //将位图放入剪贴板
   OpenClipboard(hWnd);
   EmptyClipboard();
   SetClipboardData(CF_BITMAP,bitmap);
   CloseClipboard();
   //释放DC
   ReleaseDC(NULL,hScreenDC);
   DeleteDC(hMemDC);
   EndPaint(hWnd, &ps);


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
对Windows程序中设备上下文DC(device context)的理解
CreateCompatibleBitmap - fengqing888的日志 - 网易博...
CreateCompatibleDC,CreateCompatibleBitmap,SelectObject详解
GDI编程2
VB与API学习笔记(5) 认识DC(Device Context简介)
设置双缓冲减少窗体闪烁
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服