Share via


Obtaining a Display Device Context (Windows CE 5.0)

Send Feedback

To get a handle to a display device context, call the BeginPaint or GetDC function 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 GetCurrentObject and GetObject functions. The GetCurrentObject function returns a handle identifying the current pen, brush, palette, bitmap, or font, and the GetObject function 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 function
Bitmap CreateBitmap, CreateCompatibleBitmap, CreateDIBSection
Brush CreateDIBPatternBrushPt, CreatePatternBrush, CreateSolidBrush
Palette CreatePalette
Font CreateFontIndirect
Pen CreatePen, CreatePenIndirect

Each of these functions returns a handle identifying the new object. After you retrieve a handle, you can call the SelectObject function to select the new object into the device context. However, you should save the SelectObject return value because it is the handle to the default object. When you finish using the new object, use SelectObject to restore the default object, and delete the new object with the DeleteObject function.

When you have finished drawing in the display area, you must release the device context by calling the EndPaint or ReleaseDC function. If you called BeginPaint to create the device context, then call EndPaint to release it. If you called GetDC to create the device context, then call ReleaseDC to release it.

Note   Call BeginPaint and EndPaint while processing WM_PAINT messages in your window procedure. Otherwise, call GetDC and ReleaseDC to obtain and release a device context.

The following code example shows how to call GetDC and ReleaseDC to obtain and release a device context and how to call SelectObject to get a new object.

Note   To make the following code example easier to read, error checking is not included. Do not use this code example in a release configuration unless you have modified it 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);

See Also

Getting a Handle to a Device Context

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.