Syntax
HGDIOBJ SelectObject(
[in] HDC hdc,
[in] HGDIOBJ hgdiobj
);
Parameters
Parameter | Type | Description |
---|---|---|
hdc |
[in] HDC |
A handle to the device context. |
hgdiobj |
[in] HGDIOBJ |
A handle to the GDI object to be selected. This can be a logical pen, brush, or font. |
Return Value
Type | Description |
---|---|
HGDIOBJ |
If the function succeeds, the return value is a handle to the object previously selected into the specified device context.
If the function fails, the return value is NULL .
|
Remarks
An application can use the SelectObject
function to select a
logical pen,
brush,
font,
bitmap,
region, or
color transform into a specified device context.
The application can select only one of each type of GDI object (pen, brush, font, bitmap, region, color transform) into a device context at a time.
When you no longer need a GDI object, you should delete it by calling the DeleteObject
function.
If a font is selected into a device context, the font metrics can be retrieved by calling the GetTextMetrics
function.
To retrieve the handle of the object of a specific type that is currently selected into a device context, call GetCurrentObject
.
Examples
Example 1: Selecting a Pen
This example demonstrates how to select a red pen into a device context and then restore the original pen.
// Assume hdc is a valid device context handle
HGDIOBJ hOldPen = SelectObject(hdc, hNewRedPen); // Select the new pen
if (hOldPen == NULL) {
// Handle error
return;
}
// Use the new pen for drawing...
// Restore the original pen
SelectObject(hdc, hOldPen);