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 application can draw and print ouput 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 includes a pen for 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 operations on the structures indirectly by calling various functions.
This overview provides information on the following topics:
1.Graphic objects
2.Graphic modes
3.Device Context Types
4.Device Context Operations
5.ICM-Enabled Device Context Functions
Topic One: Graphic Objects
The Pen, brush, bitmap, palette, region, and path associated with a DC are referred to as its graphic objects. The following attributes are associated with each of these 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. |
When an application creates a DC, the system automatically stores a set of default objects in it. (There is no default bitmap or path.) An application can examine the attributes of the default objects by calling the GetCurrentObject and GetObject functions. The application can change these defaults by creating a new object and selecting it into the DC. An object is selected into a DC by calling the SelectObject function.
An application can set the current brush color to a specified color value with SetDCBrushColor.
The GetDCBrushColor function returns the DC brush color. The SetDCPenColor function sets the pen color to a specified color value. The GetDCPenColor function returns the DC pen color.
GetCurrentObject
The GetCurrentObject function retrieves a handle to an object of the specified type that has been selected into the specified device context (DC).
HGDIOBJ GetCurrentObject( HDC hdc, // handle to DC UINT uObjectType // object type );
Paraments
- hdc
- [in] Handle to the DC.
- uObjectType
- [in] Specifies the object type to be queried. This parameter can be one of the following values.
Value | Meaning |
OBJ_BITMAP | Returns the current selected bitmap. |
OBJ_BRUSH | Returns the current selected brush. |
OBJ_COLORSPACE | Returns the current color space. |
OBJ_FONT | Returns the current selected font. |
OBJ_PAL | Returns the current selected palette. |
OBJ_PEN | Returns the current selected pen. |
GetObject
The GetObject function retrieves information for the specified graphics object.
int GetObject( HGDIOBJ hgdiobj, // handle to graphics object int cbBuffer, // size of buffer for object information LPVOID lpvObject // buffer for object information );
Parameters
- hgdiobj
- [in] Handle to the graphics object of interest. This can be a handle to one of the following: a logical bitmap, a brush, a font, a palette, a pen, or a device independent bitmap created by calling the CreateDIBSection function.
- cbBuffer
- [in] Specifies the number of bytes of information to be written to the buffer.
- lpvObject
- [out] Pointer to a buffer that receives the information about the specified graphics object.
The following table shows the type of information the buffer receives for each type of graphics object you can specify with hgdiobj.
Object type |
Data written to buffer |
HBITMAP |
BITMAP |
HBITMAP returned from a call to CreateDIBSection | DIBSECTION, if cbBuffer is set to sizeof(DIBSECTION), or BITMAP, if cbBuffer is set to sizeof(BITMAP) |
HPALETTE | A WORD count of the number of entries in the logical palette |
HPEN returned from a call to ExtCreatePen | EXTLOGPEN |
HPEN | LOGPEN |
HBRUSH | LOGBRUSH |
HFONT | LOGFONT |
If the lpvObject parameter is NULL, the function return value is the number of bytes required to store the information it writes to the buffer for the specified graphics object.
Retrieving Graphic-Object Attributes and Selecting New Graphic Objects
An application can retrieve the attributes for a pen, brush, palette, font, or bitmap by calling the GetCurrentObject and GetObject functions. The GetCurrentObject function returns a handle identifying the object currently selected into the DC; the GetObject function returns a structure that describes the attributes of the object.
The following example shows how an application can retrieve the current brush attributes and use the retrieved data to determine whether it is necessary to select a new brush.
HDC hdc; // display DC handle
HBRUSH hbrushNew, hbrushOld; // brush handles
HBRUSH hbrush; // brush handle
LOGBRUSH lb; // logical-brush structure
// Retrieve a handle identifying the current brush.
hbrush = GetCurrentObject(hdc, OBJ_BRUSH);
// Retrieve a LOGBRUSH structure that contains the
// current brush attributes.
GetObject(hbrush, sizeof(LOGBRUSH), &lb);
// If the current brush is not a solid-black brush,
// replace it with the solid-black stock brush.
if ((lb.lbStyle != BS_SOLID)
|| (lb.lbColor != 0x000000))
{
hbrushNew = GetStockObject(BLACK_BRUSH);
hbrushOld = SelectObject(hdc, hbrushNew);
}
// Perform painting operations with the white brush.
// After completing the last painting operation with the new
// brush, the application should select the original brush back
// into the device context and delete the new brush.
// In this example, hbrushNew contains a handle to a stock object.
// It is not necessary (but it is not harmful) to call
// DeleteObject on a stock object. If hbrushNew contained a handle
// to a brush created by a function such as CreateBrushIndirect,
// it would be necessary to call DeleteObject.
SelectObject(hdc, hbrushOld);
DeleteObject(hbrushNew);
Note The application saved the original brush handle when calling the SelectObject function the first time. This handle is saved so that the original brush can be selected back into the DC after the last painting operation has been completed with the new brush. After the original brush is selected back into the DC, the new brush is deleted, freeing memory in the GDI heap.
自己編寫的例子得到默認的GDI OBJECT
BOOL CDeviceContextDlg::OnInitDialog() // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. CMenu* pSysMenu = GetSystemMenu(FALSE); // Set the icon for this dialog. The framework does this automatically // TODO: Add extra initialization here HDC hdc = ::GetDC(this->m_hWnd); g_Trace.on(); // 開啓DEBUG輸出開關
HBRUSH hbrush; // brush handle // Retrieve a handle identifying the current brush. g_Trace.print(L"Default Brush R,G,B color:"); if(lb.lbStyle == BS_SOLID)// default style // If lbStyle is BS_SOLID or BS_HOLLOW, lbHatch is ignored
HFONT hfont; // font handle //default font object
HPEN hpen; g_Trace.print(L"Defalut Pen width:");
HPALETTE hpalette; return TRUE; // return TRUE unless you set the focus to a control |