GDI相关内容

来源:互联网 发布:动画设计软件flash 编辑:程序博客网 时间:2024/05/16 09:40

1.       什么是GDI

GDIGraphics Device Interface的缩写,含义是图形设备接口。windows绘图的实质就是利用windows提供的图形设备接口GDI将图形绘制在显示器上,利用GDI所提供的众多函数就可以方便的在屏幕、打印机及其它输出设备上输出图形,文本等操作。GDI的出现使程序员无需要关心硬件设备及设备驱动,就可以将应用程序的输出转化为硬件设备上的输出,实现了程序开发者与硬件设备的隔离,大大方便了开发工作。

2.       什么是DC

设备环境DCDevice Context)也称为设备描述表或设备上下文。

设备环境存储了一些绘图操作中需要共同设置的信息,如画笔,画刷,字体和位图等图形对象及属性,以及坐标映射,颜色,背景等影响图形输出的绘图模式。形象地说,一个绘图环境提供了一张画布和一些绘画工具,我们可以使用不同颜色,格式的绘画工具在上面涂鸦。设备环境中的设备是指任何类型的显示器和打印机等输出设备,所有绘画的操作必须通过DC进行间接的处理。

设备描述表是Windows内在的一种数据结构,它包含GDI需要的所有关于显示平面情况的描述字段,包括相连的物理设备和各种各样的状态信息。

3.       CDCDC的关系

CDC即是DC的封装,CClass

4.       GDIDC关系

MFCGDI绘图主要通过CDC类实现。

在单任务环境如MS-DOS中,应用程序可以自己的做它想做的事情,无论是在屏幕上画一条线,重新编写适配器的调色板,还是转换到另一种图像模式。而在窗口化多任务环境如Windows中,程序则失去了这种自由,因为程序A的输出必须与程序B的输出格开,首先这意味着各程序的输出必须限制在自己的窗口中。GDI使用单一的机制保证在窗口中画图的程序遵循这一规则,这种机制我们称之为设备描述表(或者设备环境)。

Windows程序在屏幕、打印机或其他输出设备上画图时,它并不是将像素直接输出到设备上,而是将图绘制到由设备描述表(DC)表示的逻辑意义上的“显示平面”上去。在屏幕上画图之前,Windows程序从GDI获取设备描述表句柄(Device Context Handle),每一次调用一个GDI输出函数时它就会把这个句柄传回给GDI。如果没有有效的设备句柄,则GDI不会做任何的绘图动作。通过设备描述表,GDI可确保程序所画的任何图形都能剪贴到屏幕的特定区域。设备描述表在使GDI摆脱设备限制的过程中发挥了重要的作用。获得设备描述表的句柄之后,同一GDI函数可以向多种输出设备上画图。

5.       获取一张图片(来自MSDN

可以使用位图获取图片,并且可以将得到的图片存储在内存中,在自身应用程序窗口的任意位置或其它窗口中进行显示。

有时,你可能只需暂时存储获取的图片。例如,在绘图程序中,当你移动一张图片时,该程序必须先暂时保存图片的原始视图并且显示移动后的视图。然后,当用户选择原始视图时,程序必须以保存的原始视图代替现在的视图。

为了暂时保存图片,程序必须首先调用CreateCompatibleDC来创建一个与当前窗口DC兼容的DC。然后,通过调用CreateCompatibleBitmap创建一个大小合适的位图(bitmap)。接着,调用SelectObject将这该位图选入DC

以上工作完成之后,就可以利用BitBlt来获取图像了。这个函数执行的是位块转换,即将源位图的数据拷贝到目标位图。但是,这个函数的两个参数却不是位图句柄,而是两个dc的句柄,其将被选入源dc的位图数据拷贝到另一个目标dc内的位图中。这种情况下,目标DC是兼容DC,因此当BitBlt完成转换后,图像就被存储到内存中了。为了重新显示该图像,再次调用BitBlt,指定兼容DC作为源DC,窗口(或打印机)DC作为目标DC

下面的代码,功能是从桌面获取图像,首先创建兼容DC以及合适大小的位图,将位图选入兼容DC,然后使用BitBlt函数拷贝图像。

//     创建普通DC及兼容DC,普通DC提供屏幕快照,兼容DC在关联的位图中保存//  此快照的副本

hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL);

hdcCompatible = CreateCompatibleDC(hdcScreen);

 

// hdcScreen创建兼容位图

hbmScreen = CreateCompatibleBitmap(hdcScreen,

                     GetDeviceCaps(hdcScreen, HORZRES),

                     GetDeviceCaps(hdcScreen, VERTRES));

 

if (hbmScreen == 0)

    errhandler("hbmScreen", hwnd);

 

// 将位图选入兼容DC

if (!SelectObject(hdcCompatible, hbmScreen))

    errhandler("Compatible Bitmap Selection", hwnd);

 

// 隐藏程序窗口

ShowWindow(hwnd, SW_HIDE);

 

// 将颜色数据拷贝到选入到兼容DC的位图中

if (!BitBlt(hdcCompatible,

               0,0,

               bmp.bmWidth, bmp.bmHeight,

               hdcScreen,

               0,0,

               SRCCOPY))

 

        errhandler("Screen to Compat Blt Failed", hwnd);

 

// 重画程序窗口

ShowWindow(hwnd, SW_SHOW);

由于翻译水平实在不怎么着,特附上英文原文。

Capturing an Image

You can use a bitmap to capture an image, and you can store the captured image in memory, display it at a different location in your application's window, or display it in another window.

 

In some cases, you may want your application to capture images and store them only temporarily. For example, when you scale or zoom a picture created in a drawing application, the application must temporarily save the normal view of the image and display the zoomed view. Later, when the user selects the normal view, the application must replace the zoomed image with a copy of the normal view that it temporarily saved.

 

To store an image temporarily, your application must call CreateCompatibleDC to create a DC that is compatible with the current window DC. After you create a compatible DC, you create a bitmap with the appropriate dimensions by calling the CreateCompatibleBitmap function and then select it into this device context by calling the SelectObject function.

 

After the compatible device context is created and the appropriate bitmap has been selected into it, you can capture the image. The BitBlt function captures images. This function performs a bit block transfer that is, it copies data from a source bitmap into a destination bitmap. However, the two arguments to this function are not bitmap handles. Instead, BitBlt receives handles that identify two device contexts and copies the bitmap data from a bitmap selected into the source DC into a bitmap selected into the target DC. In this case, the target DC is the compatible DC, so when BitBlt completes the transfer, the image has been stored in memory. To redisplay the image, call BitBlt a second time, specifying the compatible DC as the source DC and a window (or printer) DC as the target DC.

 

The following example code, from an application that captures an image of the entire desktop, creates a compatible device context and a bitmap with the appropriate dimensions, selects the bitmap into the compatible DC, and then copies the image using the BitBlt function.

 

// Create a normal DC and a memory DC for the entire screen. The

// normal DC provides a "snapshot" of the screen contents. The

// memory DC keeps a copy of this "snapshot" in the associated

// bitmap.

 

hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL);

hdcCompatible = CreateCompatibleDC(hdcScreen);

 

// Create a compatible bitmap for hdcScreen.

 

hbmScreen = CreateCompatibleBitmap(hdcScreen,

                     GetDeviceCaps(hdcScreen, HORZRES),

                     GetDeviceCaps(hdcScreen, VERTRES));

 

if (hbmScreen == 0)

    errhandler("hbmScreen", hwnd);

 

// Select the bitmaps into the compatible DC.

 

if (!SelectObject(hdcCompatible, hbmScreen))

    errhandler("Compatible Bitmap Selection", hwnd);

 

        // Hide the application window.

 

        ShowWindow(hwnd, SW_HIDE);

 

         //Copy color data for the entire display into a

         //bitmap that is selected into a compatible DC.

 

        if (!BitBlt(hdcCompatible,

               0,0,

               bmp.bmWidth, bmp.bmHeight,

               hdcScreen,

               0,0,

               SRCCOPY))

 

        errhandler("Screen to Compat Blt Failed", hwnd);

 

        // Redraw the application window.

 

        ShowWindow(hwnd, SW_SHOW);

6.       一个形象的比喻

如果你喜欢画画,你得先准备了画布,画笔,颜料……

画画的环境搭建好了,你就可以画画了。这个画画的环境,就是DC

在图形环境下,一切都是画出来的,所以,你要准备好一个DC,才能在屏幕上画画。——写字也是画画。

在画画的环境中,有哪些对象呢?

画布——GDI对象之一:区域

画笔——GDI对象之一:画笔

颜料盒——GDI对象之一:调色板

如果要在画笔上写字的话,写什么样的字体呢?方正字体?徐静蕾字体?——字体也是GDI对象之一。

有的画笔比较粗,专用来刷大面积背景色的,这是刷子——GDI对象之一:刷子

如果你不想画了,只想把别人画好的画,贴到你的画布上,这也是可以的。——GDI对象之一:位图。

所以,这里就有6GDI对象可以用于DC

现在开始画画了,你拿起了一只笔。——在Windows环境里,这叫选择了一个画笔对象:使用SelectOBject函数。当然,如果你没带笔也没关系,Windows为你准备了几只画笔,你可以这样申请系统提供的缺省画笔:hPen = GetStockObject(WHITE_PEN);

如果你画着画着,觉得手中的笔用着不爽,可以换一只啊,没关系的。——依旧是SelectObject()换笔。

当然,如果你走出了画室,别完了把你的画笔清除掉,要不画室里全是笔啊,刷子啊,太乱了。——DeleteObject()

 

 

原创粉丝点击