天下

记录修行的印记

Win32设备上下文(Device Contexts)

设备上下文(设备内容)
Device Contexts
A device context is a structure that defines a set of graphic objects and their associated attributes, as well as the graphic modes that affect output. The graphic objects include a pen for line drawing, a brush for painting and filling, a bitmap for copying or scrolling parts of the screen, a palette for defining the set of available colors, a region for clipping and other operations, and a path for painting and drawing operations. The remainder of this section is divided into the following three areas. 

About Device Contexts
Device independence is one of the chief features of Microsoft Windows. Applications can draw and print output on a variety of devices. The software that supports this device independence is contained in two dynamic-link libraries. The first, Gdi.dll, is referred to as the graphics device interface (GDI); the second is referred to as a device driver. The name of the second depends on the device where the application draws output. For example, if the application draws output in the client area of its window on a VGA display, this library is Vga.dll; if the application prints output on an Epson FX-80 printer, this library is Epson9.dll. 

An application must inform GDI to load a particular device driver and, once the driver is loaded, to prepare the device for drawing operations (such as selecting a line color and width, a brush pattern and color, a font typeface, a clipping region, and so on). These tasks are accomplished by creating and maintaining a device context (DC). A DC is a structure that defines a set of graphic objects and their associated attributes, and the graphic modes that affect output. The graphic objects include a pen for line drawing, a brush for painting and filling, a bitmap for copying or scrolling parts of the screen, a palette for defining the set of available colors, a region for clipping and other operations, and a path for painting and drawing operations. Unlike most of the structures, an application never has direct access to the DC; instead, it operates on the structure indirectly by calling various functions. 

This overview provides information on the following topics: 

Graphic Objects 
Graphic Modes 
Device Context Types 
Device Context Operations 
ICM-Enabled Device Context Functions 
An important concept is the layout of a DC or a window, which describes the order in which GDI objects and text are revealed (either left-to-right or right-to-left). For more information, see "Window Layout and Mirroring" in Window Features and the GetLayout and SetLayout functions. 

Device Context Types
There are four types of DCs: display, printer, memory (or compatible), and information. Each type serves a specific purpose, as described in the following table. 

Device context Description 
Display Supports drawing operations on a video display. 
Printer Supports drawing operations on a printer or plotter. 
Memory Supports drawing operations on a bitmap. 
Information Supports the retrieval of device data. 

设备上下文是windows编程中最重要的概念之一。widnows下的所有绘图都是通过设备上下文进行的。 
设备上下文是一种包含有关某个设备(如显示器或打印机)的绘制属性信息的 Windows 数据结构。所有绘制调用都通过设备上下文对象进行,这些对象封装了用于绘制线条、形状和文本的 Windows API。设备上下文允许在 Windows 中进行与设备无关的绘制。设备上下文可用于绘制到屏幕、打印机或者图元文件。

设备上下文(Device Context)DC 
DC实际上是GDI内部保存的数据结构。
DC与特定的显示设备(如显示器或打印机)相关。
对于显示器,DC总是与显示器上的特定视窗相关。
DC中的有些值是图形「属性」,这些属性定义了GDI绘图函数工作的细节。
例如,对於TextOut,DC的属性确定了文字的颜色、文字的背景色、x坐标和y坐标映射到视窗的显示区域的方式,以及显示文字时Windows使用的字体。
MSDN的解释: 一个DC是一个结构,它定义了一系列图形对象的集合以及它们相关的属性,以及影响输出效果的一些图形模式。这些图形对象包括一个画线的笔,一个填充和painting的画刷,一个用来向屏幕拷贝的位图,一个定义了一系列颜色集合的调色板,一个用来剪裁等操作的区域,一个做painting和drawing操作的路径。

设备上下文
当您想在一个图形输出设备(诸如屏幕或者打印机)上绘图时,您首先必须获得一个设备上下文(或者DC)的句柄。将句柄传回给程序时,Windows就给了您使用设备的权限。然后您在GDI函数中将这个句柄作为一个参数,向Windows标识您想在其上进行绘图的设备。

设备上下文中包含许多确定GDI函数如何在设备上工作的目前「属性」,这些属性允许传递给GDI函数的参数只包含起始坐标或者尺寸信息,而不必包含Windows在设备上显示对象时需要的所有其它信息。例如,呼叫TextOut时,您只需要在函数中给出设备上下文句柄、起始坐标、文字和文字的长度。您不必指定字体、文字颜色、文字后面的背景色彩以及字符间距,因为这些属性都是设备上下文的一部分。当您想改变这些属性之一时,您呼叫一个可以改变设备上下文中属性的函数,以后针对该设备上下文的TextOut呼叫来使用改变后的属性。
取得设备上下文句柄

Windows提供了几种取得设备上下文句柄的方法。如果在处理一个消息时取得了设备上下文句柄,应该在退出窗口函数之前释放它(或者删除它)。一旦释放了句柄,它就不再有效了。对于打印机设备上下文句柄,规则就没有这么严格。在第十三章会讨论打印。
最常用的取得并释放设备上下文句柄的方法是,在处理WM_PAINT消息时,使用BeginPaint和EndPaint呼叫:
hdc = BeginPaint (hwnd, &ps) ;
//do something
EndPaint (hwnd, &ps) ;

Windows程序还可以在处理非WM_PAINT消息时取得设备上下文句柄:
hdc = GetDC (hwnd) ;
//do something
ReleaseDC (hwnd, hdc) ;
        
Windows程序还可以取得适用于整个窗口(而不仅限于窗口的显示区域)的设备上下文句柄:
hdc = GetWindowDC (hwnd) ;
//do something
ReleaseDC (hwnd, hdc) ;
        
这个设备上下文除了显示区域之外,还包括窗口的标题列、菜单、滚动条和框架(frame)。GetWindowDC函数很少使用,如果想尝试用一用它,则必须拦截处理WM_NCPAINT消息,Windows使用该消息在窗口的非显示区域上绘图。
BeginPaint、GetDC和GetWindowDC获得的设备上下文都与视讯显示器上的某个特定窗口相关。取得设备上下文句柄的另一个更通用的函数是CreateDC:
hdc = CreateDC (pszDriver, pszDevice, pszOutput, pData) ;
//do something
DeleteDC (hdc) ;


设备上下文就是一个windows对象,即DC的句柄,而windows则是一种图形环境,其图形系统令人难以自信地灵活和强大。而实质上,widnows下的所有绘图都是通过设备上下文进行的,而不是直接对窗口和设备本身进行。

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

设备上下文属性 属性默认值
背景色(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

posted on 2013-06-04 09:08 天下 阅读(1100) 评论(0)  编辑 收藏 引用 所属分类: Win32


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2013年6月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

导航

统计

常用链接

留言簿(4)

随笔分类(378)

随笔档案(329)

链接

最新随笔

搜索

最新评论