設備上下文(設備內容)
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