如何理解设备上下文

来源:互联网 发布:mac charles 简书 编辑:程序博客网 时间:2024/05/16 23:43

先看msdn上关于设备上下文的解释:

 

 

Device Contexts 

 

A device context is a Windows data structure containing information about the drawing attributes of a device such as a display or a printer. All drawing calls are made through a device-context object, which encapsulates the Windows APIs for drawing lines, shapes, and text. Device contexts allow device-independent drawing in Windows. Device contexts can be used to draw to the screen, to the printer, or to a metafile.

CPaintDC objects encapsulate the common idiom of Windows, calling the BeginPaint function, then drawing in the device context, then calling the EndPaint function. The CPaintDC constructor callsBeginPaint for you, and the destructor calls EndPaint. The simplified process is to create the CDCobject, draw, and destroy the CDC object. In the framework, much of even this process is automated. In particular, your OnDraw function is passed a CPaintDC already prepared (via OnPrepareDC), and you simply draw into it. It is destroyed by the framework and the underlying device context is released to Windows upon return from the call to your OnDraw function.

CClientDC objects encapsulate working with a device context that represents only the client area of a window. The CClientDC constructor calls the GetDC function, and the destructor calls the ReleaseDCfunction. CWindowDC objects encapsulate a device context that represents the whole window, including its frame.

CMetaFileDC objects encapsulate drawing into a Windows metafile. In contrast to the CPaintDC passed to OnDraw, you must in this case call OnPrepareDC yourself.

 

翻译如下:

 

设备上下文是一种包含有关某个设备(如显示器或打印机)的绘制属性信息的 Windows 数据结构。所有绘制调用都通过设备上下文对象进行,这些对象封装了用于绘制线条、形状和文本的 Windows API。设备上下文允许在 Windows 中进行与设备无关的绘制。设备上下文可用于绘制到屏幕、打印机或者图元文件。  CPaintDC 对象将 Windows 的常见固定用语进行封装,调用 BeginPaint 函数,然后在设备上下文中绘制,最后调用 EndPaint 函数。CPaintDC 构造函数为您调用 BeginPaint,析构函数则调用 EndPaint。该简化过程将创建 CDC 对象、绘制和销毁 CDC 对象。在框架中,甚至连这个过程的大部分也是自动的。具体说来,框架给 OnDraw 函数传递(通过 OnPrepareDC)准备好的 CPaintDC,您只需绘制到 CPaintDC 中。根据调用 OnDraw 函数的返回,CPaintDC 被框架销毁并且将基础设备上下文释放给 Windows。  CClientDC 对象封装对一个只表示窗口工作区的设备上下文的处理。CClientDC 构造函数调用 GetDC 函数,析构函数调用 ReleaseDC 函数。CWindowDC 对象封装表示整个窗口(包括其框架)的设备上下文。

  CMetaFileDC 对象将绘制封装到 Windows 图元文件中。与传递给 OnDraw 的 CPaintDC 相反,在这种情况下您必须自己调用 OnPrepareDC。

 

至此,我们应该对dc有了一个大致的了解。再看下面转来的文章:

 

转载  什么是设备上下文 

关于设备场景,叫法颇多,有些书上说为设备环境、显示场景,更常见的叫做设备描述表或设备描述体。当然你爱怎么叫随你的便,我还是喜欢说为设备场景。
那 么究竟什么是设备场景呢? 设备场景是一种windows对象,而windows则是一种图形环境,其图形系统令人难以自信地灵活和强大。而实质上,widnows下的所有绘图都是通过设备场景进行的,而不是直接对窗口和设备本身进行。为了说明设备场景,很多书都拿一些现实生活中的现象来进行对照说明。其中,最常见的是把它比喻为一位画家在作画。我想大家都看过画家是如何画画的,最起码是在电影里或者是在道旁的广告牌上作画的画家。我们可以想象一下∶有个风景秀丽的白云山(是我瞎起的名)上,有位画家一只手拿着调色板,另一只手则拿着画笔,面对一个画板正在写风景画。有些书认为画家的调色板相当于设备场景,有些书则认为画板相当于设备场景,说法不一。鉴于这种情况,我认为还是直接去说明设备场景比较好。


作为windows的对象,设备场景实际上是一种windows内部的数据结构。就象pointapi数据结构具有x和y两个属性一样,设备场景同样具有着它自身的属性,只是属性比较多而已,如下表∶



设备场景属性


属 性 默 认 值
背景色(background color) 白色(white)
背景模式(background mode) 不透明(opaque)
位图(bitmap)无(none)
刷子(brush)白色刷子(white brush)
刷子起点(brush origin) 0,0
剪切区(clipping region) 整个窗口或设备表面(entire window or device surface)
调色板(color palette) 默认调色板(default palette)
画笔位置(pen position) 0,0
绘图模式(drawing mode) r2_copypen
字体(font) 系统字体
字间距(intercharater spacing) 0
影射模式(mapping mode) mm_text
画笔(pen) 黑色(black)
多边形填充模式(mapping mode) alternate
伸缩模式(stretching mode) blackonwhite
文本色(text color) 黑色(black)
视口起点(viewport origin) 0,0
视口范围(viewport extents) 1,1
窗口起点(window origin) 0,0
窗口范围(window extents) 1,1
请你多看看这张表,对设备场景都有哪些属性,脑子里应当有个印象。事实上,设备场景的很多属性对应于vb中的form、picturebox、text等窗体或控件的属性。比如,字体、背景色、绘图模式等等。可想而知,很多学vb的朋友尽管并不知道什么叫设备场景,但实质上都不知不觉地使用了设备场景。可以说,设备场景是windows编程中最重要的概念之一。
对于设备场景,有些朋友可能一时不大好理解,这很自然,不用担心谁都是一样。不知对你能否作为一个帮助,我是把设备场景想象成一种配套的(包括画板、调色板、画笔、刷子等)的绘画工具。其中画板是最重要的,其他的东西都是为这个画板服务的。如过你创建了一个设备场景,就等于是你从百货商店买来了这一套绘画工具,从而具备了绘画的条件。但,你的房间总不是那么宽敞的。为了继续绘出别的画、继续购买新的绘画工具,无用的工具应当及时清理掉。因为设备场景本身是占用内存的。不要担心这会降低运行速度,对计算机来说创建一个设备场景,再删掉一个设备场景,那都是瞬息之间的事情,根本谈不上什么浪费时间,绝对不像跑一趟百货商店那么麻烦、费时。对于绘图,你应当认识的一点是,绘图并不是简单地指绘画,输出文本也是一种绘图过程。尽管如此,api函数中图形函数与文本函数大体都是各自各的。绘画和写文本都是在同样的设备场景中进行,这一点很重要。
我想,你大概还是没有理解好,不过没有关系,继续往下看好了。本节中请记住一点∶
widnows下的所有绘图都是通过设备场景进行的。