win32 Device Contexts

Device Contexts

device context
是一个结构,该结构定义了graphic objects集合 和他们之间的关联属性,以及graphic modes。
graphic modes 会影响graphic对象的输出。
graphic objects 包括:

图形对象 用途
pen 用来画线
brush 用来painting和填充
bitmap copying or scrolling parts of the screen
palette defining the set of available colors
region clipping and other operations
path painting and drawing operations

About Device Contexts

Microsoft Windows的一个特性是 Device independence(设备无关,独立于设备)。
应用可以在各种各样的设备上绘制或者打印输出。
支持设备无关特性的软件,包含两个动态链接库。
第一个是 Gdi.dll,叫做graphics device interface (GDI) 图形设备接口。
另外一个是device driver 设备驱动。
第二个库的名字和应用要绘制输出的设备有关。
比如,如果应用绘图输出到窗口的客户区,显示在VGA显示器上。库的名字是Vga.dll。
如果应用打印输出到Epson FX-80 打印机上,库的名字是Epson9.dll。

应用工需要通知GDI 去加载特定的device driver,一旦driver加载了,就配置device,去做绘制操作。
比如选择line的颜色和宽度,brush的类型和颜色,字体类型,剪切区域等等。
这些任务是通过创建和管理device context (DC)完成的。
DC 是 是一个结构,该结构定义了graphic objects集合 和他们之间的关联属性,以及graphic modes。
graphic modes 会影响graphic对象的输出。
graphic objects 包括:

图形对象 用途
pen 用来画线
brush 用来painting和填充
bitmap copying or scrolling parts of the screen
palette defining the set of available colors
region clipping and other operations
path painting and drawing operations

不像其他的结构,应用不会直接访问DC,而是通过调用各种函数来间接的访问DC。

下面的内容包含下面的话题:

Graphic Objects
Graphic Modes
Device Context Types
Device Context Operations
ICM-Enabled Device Context Functions

另外一个重要的概念是窗口或者DC的布局。
它定义GDI objects和text 是怎么显示的,从左到右,还是从右到左。
进一步的消息,请看”Window Layout and Mirroring” in Window Features
和the GetLayout and SetLayout 函数

Graphic Objects

device context (DC) 包含的pen, brush, bitmap, palette, region, 和 path。
作为DC的graphic objects
下面是这些objects的属性:

Graphic object Associated attributes
Bitmap Size, in bytes; dimensions, in pixels; color-format; compression scheme; and so on.
Brush Style, color, pattern, and origin.
Palette Colors and size (or number of colors).
Font Typeface name, width, height, weight, character set, and so on.
Path Shape.
Pen Style, width, and color.
Region Location and dimensions.

当应用创建一个DC,系统自动保存一系列的默认objects给这个DC。
DC没有默认的bitmap 和 path。

应用可以通过调用GetCurrentObject 和 GetObject函数来 检查DC的默认objects。
应用可以创建一个新的object,然后将该object选择进入DC。
通过调用SelectObject函数,来选择一个object进入DC。
应用可以调用SetDCBrushColor来设置当前brush的颜色为指定的颜色。
函数GetDCBrushColor 返回DC的brush对象的颜色。
SetDCPenColor设置pen对象的颜色。
GetDCPenColor函数返回DC的pen对象的颜色。

Graphic Modes

Windows supports five graphic modes that allow an application to specify how colors are mixed, where output appears, how the output is scaled, and so on. These modes, which are stored in a DC, are described in the following table.

https://docs.microsoft.com/zh-cn/windows/desktop/gdi/graphic-modes

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.

Display Device Contexts

https://docs.microsoft.com/zh-cn/windows/desktop/gdi/display-device-contexts

Printer Device Contexts

The printer DC can be used when printing on a dot-matrix printer, ink-jet printer, laser printer, or plotter. An application creates a printer DC by calling the CreateDC function and supplying the appropriate arguments (the name of the printer driver, the name of the printer, the file or device name for the physical output medium, and other initialization data). When an application has finished printing, it deletes the printer DC by calling the DeleteDC function. An application must delete (rather than release) a printer DC; the ReleaseDC function fails when an application attempts to use it to release a printer DC.
For more information, see Printer Output.

Memory Device Contexts

https://docs.microsoft.com/zh-cn/windows/desktop/gdi/memory-device-contexts

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.
The memory DC stores bitmap images for a particular device. An application can create a memory DC by calling the CreateCompatibleDC function.
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.
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.
When displaying a DIB or a DDB created from a DIB on a palette device, you can improve the speed at which the image is drawn by arranging the logical palette to match the layout of the system palette. To do this, call GetDeviceCaps with the NUMRESERVED value to get the number of reserved colors in the system. Then call GetSystemPaletteEntries and fill in the first and last NUMRESERVED/2 entries of the logical palette with the corresponding system colors. For example, if NUMRESERVED is 20, you would fill in the first and last 10 entries of the logical palette with the system colors. Then fill in the remaining 256-NUMRESERVED colors of the logical palette (in our example, the remaining 236 colors) with colors from the DIB and set the PC_NOCOLLAPSE flag on each of these colors.
For more information about color and palettes, see Colors. For more information about bitmaps and bitmap operations, see Bitmaps.

Information Device Contexts

The information DC is used to retrieve default device data. For example, an application can call the CreateIC function to create an information DC for a particular model of printer and then call the GetCurrentObject and GetObject functions to retrieve the default pen or brush attributes. Because the system can retrieve device information without creating the structures normally associated with the other types of device contexts, an information DC involves far less overhead and is created significantly faster than any of the other types. After an application finishes retrieving data by using an information DC, it must call the DeleteDC function.

Device Context Operations

An application can perform the following operations on a device context:
Enumerate existing graphic objects.
Select new graphic objects.
Delete existing graphic objects.
Save the current graphic objects, their attributes, and the graphic modes.
Restore previously saved graphic objects, their attributes, and the graphic modes.
In addition, an application can use a device context to:
Determine how graphics output is translated.
Cancel lengthy drawing operations (begun by a thread in a multithreaded application).
Reset a printer to a particular state.
This subsection provides information on the following topics.
Operations on Graphic Objects
Cancellation of Drawing Operations
Retrieving Device Data
Saving, Restoring, and Resetting a Device Context

Using the Device Context Functions

Device Context Reference

猜你喜欢

转载自blog.csdn.net/wowocpp/article/details/80832287