A device context is a device independent means of specifying graphics. A device context may be associated with a video display or a printer or other device. The functions that may be used for creating a new device context are shown in the table below.
|BeginPaint||Creates a device context suitable for painting a window.|
|GetDeviceContext||Creates a display device context.|
|GetWindowDeviceContext||Creates a display device context for the entire window.|
|GetDeviceContext - Extended||Creates a display device context with clipping control.|
|CreateDeviceContext||Creates a device context suitable for printing.|
|CreateMemoryDeviceContext||Creates a memory device context.|
|CreateInformationalDeviceContext||Creates an information only device context.|
|CreateMetafile||Creates a metafile device context.|
The first of these functions: BeginPaint is usually called in response to the message Message::Paint. When the application is finished with such a device context, it should call EndPaint to release the device context. This sequence creates a device context with attributes suitable for immediate drawing and validates the entire window (the invalid region of a window is the area marked as requiring painting). Drawing in a device context of this type implies that such drawing is clipped to the invalid region of the window. The rectangle found in the paint structure indicates the area to which drawing is restricted. Such a mechanism creates and destroys the device context within the body of the Paint Message.
The function GetDeviceContext also creates a type of device context that is suitable for drawing in a window. Drawing is not clipped to the invalid region of the window, nor is any portion of the window validated for device contexts of this type. This type of device context is useful for drawing in a window in situations other than receiving a paint message. When finished with a device context of this type, the function ReleaseDeviceContext should be called.
The function GetDeviceContext - Extended creates a device context type that allows for clipping control. An application should use ReleaseDeviceContext to destroy device contexts of this type.
The function GetWindowDeviceContext creates a device context type that allows for drawing in the entire window - non-client areas included. Applications generally use this type of device context rarely. Such a device context may be released via the function ReleaseDeviceContext.
The function CreateDeviceContext is the most general function that may be used to create a device context. It allows for the creation of device contexts associated with any device type, including screens and printers. For example, this function may be used to create a screen device context as shown below.
Handle DeviceContext = Gdi::CreateDeviceContext("DISPLAY",0,0,0);
Device contexts of this type are destroyed via the function DeleteDeviceContext.
The function CreateInformationalDeviceContext creates a device context whose sole function is to query attributes of the associated device. Device contexts of this type should be destroyed via a call to the function DeleteDeviceContext. An information device for the display may be created via the following call.
Handle DeviceContext = Gdi::CreateInformationalDeviceContext("DISPLAY",0,0,0);
A device context of type created via the function CreateMemoryDeviceContext is used for storing bitmaps. When an application no longer requires a device context of this type, it should call the function DeleteDeviceContext.
A device context created via the function CreateMetafile is a metafile device context. This type of device context is destroyed via a call to the function CloseMetafile. Upon closing such a device context, a handle of the newly created metafile is returned.
Rather than use the above-mentioned API directly, it is possible to use the class DeviceContext. Note that this class is missing from the .Net windows subsystem, so there is no way of accessing driver graphics (GDI) from .Net. I++ remedies this situation by supplying the DeviceContext class. The DeviceContext class is built over the Win+ interface which is based upon exceptions. This yields a very solid native graphics class that can be used with C# .Net programs.
The program of this section displays device capabilities. The output of the program is shown below.
The program consists of a C# source code file. All examples will be expressed in C# from here on. The program resembles the programs of the previous chapter (a 3 column textual display) but the information is provided to display the capabilities of a device context.
The function GetDeviceCapabilities is used to query the actual value of an index.
Device contexts have associated attributes. When a device context is first created, default values are applied for attributes. The following table documents device context attributes.
|Attribute||Default Value||Function to Set||Function to Query
||Unit Mode ||Unit::Pixel ||SetMappingMode ||GetMappingMode
||Window Origin ||(0,0) ||SetWindowOrigin|
||Viewport Origin ||(0,0) ||SetViewportOrigin|
||Window Extents ||(1,1) ||SetWindowExtent|
||Viewport Extents ||(1,1) ||SetViewportExtent|
||Pen ||StandardPen::Black ||SelectObject ||GetCurrentObject
||Brush ||StandardBrush::White ||SelectObject ||GetCurrentObject
||Font ||StandardFont::System ||SelectObject ||GetCurrentObject
||Bitmap ||none ||SelectObject ||GetCurrentObject
||Position ||(0,0) ||MoveTo|
||Background Mode ||Background::Opaque ||SetBackgroundMode ||GetBackgroundMode
||Background Color ||white ||SetBackgroundColor ||GetBackgroundColor
||Text Color ||black ||SetTextColor ||GetTextColor
||Drawing Mode ||Mix::CopyPen ||SetForegroundMix ||GetForegroundMix
||Stretch Mode ||StretchBlockMode::And ||SetStretchBitBlockMode ||GetStretchBitBlockMode
||Filling Mode ||FillMode::Alternate ||SetFillMode ||GetFillMode
||Extra Spacing ||0 ||SetTextCharacterExtra ||GetTextCharacterExtra
||Brush Origin ||(0,0) ||SetBrushOrigin ||GetBrushOrigin
||Clipping Region ||none
All of the attributes of a device context may be saved using the function SaveDeviceContext. Saved attributes may be later restored using the function RestoreDeviceContext.
Apart from this, when registering a window class, the class style ClassStyle::OwnDeviceContext may be applied. When this style is applied, each window of the class has its own device context set aside. In this case, when a device context is created, destroyed and later recreated, it retains the most recent device context attributes.