|This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.|
To get a handle to a display device context, call the BeginPaintor GetDCfunction and supply a handle to a window. Windows CE returns a handle to a display device context with default objects, attributes, and graphic modes. Newly created device contexts start with default brush, palette, font, pen, and region objects. You can begin drawing using these defaults, or you can choose a new object, change the attributes of an existing object, or choose a new mode.
You can examine a default object's attributes by calling the GetCurrentObjectand GetObjectfunctions. The GetCurrentObjectfunction returns a handle identifying the current pen, brush, palette, bitmap, or font, and the GetObjectfunction initializes a structure containing the object attributes.
The following table shows the object-specific creation functions you can call to replace a default object.
|Graphics object||Creation functions|
|Bitmap||CreateBitmap, CreateCompatibleBitmap, CreateDIBSection|
|Brush||CreateDIBPatternBrushPt, CreatePatternBrush, CreateSolidBrush|
Each of these functions returns a handle identifying the new object. After you retrieve a handle, you can call the SelectObjectfunction to select the new object into the device context. However, you should save the SelectObjectreturn value because it is the handle to the default object. When you finish using the new object, use SelectObjectto restore the default object, and delete the new object with the DeleteObjectfunction.
When you have finished drawing in the display area, you must release the device context by calling the EndPaintor ReleaseDCfunction. If you called BeginPaintto create the device context, then call EndPaintto release it. If you called GetDCto create the device context, then call ReleaseDCto release it.
Note Call BeginPaintand EndPaintwhile processing WM_PAINT messages in your window procedure. Otherwise, call GetDCand ReleaseDCto obtain and release a device context.
The following code example shows how to call GetDCand ReleaseDCto obtain and release a device context and how to call SelectObjectto get a new object.
Note To make the following code example easier to read, error checking is not included. This code example should not be used in a release configuration unless it has been modified to include secure error handling.
HDC hDC; // Handle to a display device context HBRUSH hBrush, // Handle to the new brush object hOldBrush; // Handle to the old brush object // Retrieve the handle to the display device context. if (!(hDC = GetDC (hwnd))) return; // Create a solid brush and select it into the device context. hBrush = CreateSolidBrush (RGB(0, 255, 255)); hOldBrush = SelectObject (hDC, hBrush); // Draw a rectangle. Rectangle (hDC, 0, 0, 100, 200); // Select the old brush back into the device context. SelectObject (hDC, hOldBrush); // Delete the new brush object. DeleteObject (hBrush); // Release the device context. ReleaseDC (hwnd, hDC);
Last updated on Tuesday, May 18, 2004