SelectObject

2.x

  HGDIOBJ SelectObject(hdc, hgdiobj)    
  HDC hdc; /* handle of device context */
  HGDIOBJ hgdiobj; /* handle of object, */  

The SelectObject function selects an object into the given device context. The new object replaces the previous object of the same type.

Parameters

hdc

Identifies the device context.

hgdiobj

Identifies the object to be selected. The object can be one of the following and must have been created by using one of the listed functions:

Object Functions

Bitmap CreateBitmap, CreateBitmapIndirect, CreateCompatibleBitmap, CreateDIBitmap
Brush CreateBrushIndirect, CreateDIBPatternBrush, CreateHatchBrush, CreatePatternBrush, CreateSolidBrush
Font CreateFont, CreateFontIndirect
Pen CreatePen, CreatePenIndirect
Region CreateEllipticRgn, CreateEllipticRgnIndirect, CreatePolygonRgn, CreateRoundRectRgn, CreateRectRgn, CreateRectRgnIndirect

Return Value

The return value is the handle of the object being replaced, if the function is successful. Otherwise, it is NULL.

If the hgdiobj parameter identifies a region, this function performs the same task as the SelectClipRgn function and the return value is SIMPLEREGION (region has no overlapping borders), COMPLEXREGION (region has overlapping borders), or NULLREGION (region is empty). If an error occurs, the return value is ERROR and the previously selected object of the specified type remains selected in the device context.

Comments

When an application uses the SelectObject function to select a font, pen, or brush, the system allocates space for that object in its data segment. Because data-segment space is limited, an application should use the DeleteObject function to remove each drawing object that it no longer requires. Before removing the object, the application should select it out of the device context. To do this, the application can select a different object of the same type back into the device context; typically, this different object is the original object for the device context.

When the hdc parameter identifies a metafile device context, the SelectObject function does not return the handle of the previously selected object. When the device context is a metafile, calling SelectObject with the hgdiobj parameter set to a value returned by a previous call to SelectObject can cause unpredictable results. Because metafiles perform their own object cleanup, an application need not reselect default objects when recording a metafile.

Memory device contexts are the only device contexts into which an application can select a bitmap. A bitmap can be selected into only one memory device context at a time. The format of the bitmap must either be monochrome or be compatible with the given device; if it is not, SelectObject returns an error.

Example

The following example creates a pen, uses the SelectObject function to select it into a device context, uses the pen to draw a rectangle, selects the previous pen back into the device context, and uses the DeleteObject function to remove the pen that was just created:

HPEN hpen, hpenOld;

hpen = CreatePen(PS_SOLID, 6, RGB(0, 0, 255));
hpenOld = SelectObject(hdc, hpen);

Rectangle(hdc, 10, 10, 100, 100);

SelectObject(hdc, hpenOld);
DeleteObject(hpen);

See Also

DeleteObject, SelectClipRgn, SelectPalette