4 out of 7 rated this helpful - Rate this topic

SelectObject function

Applies to: desktop apps only

The SelectObject function selects an object into the specified device context (DC). The new object replaces the previous object of the same type.

Syntax

HGDIOBJ SelectObject(
  __in  HDC hdc,
  __in  HGDIOBJ hgdiobj
);

Parameters

hdc [in]

A handle to the DC.

hgdiobj [in]

A handle to the object to be selected. The specified object must have been created by using one of the following functions.

ObjectFunctions
Bitmap

CreateBitmap, CreateBitmapIndirect, CreateCompatibleBitmap, CreateDIBitmap, CreateDIBSection

Bitmaps can only be selected into memory DC's. A single bitmap cannot be selected into more than one DC at the same time.

Brush

CreateBrushIndirect, CreateDIBPatternBrush, CreateDIBPatternBrushPt, CreateHatchBrush, CreatePatternBrush, CreateSolidBrush

Font

CreateFont, CreateFontIndirect

Pen

CreatePen, CreatePenIndirect

Region

CombineRgn, CreateEllipticRgn, CreateEllipticRgnIndirect, CreatePolygonRgn, CreateRectRgn, CreateRectRgnIndirect

 

Return value

If the selected object is not a region and the function succeeds, the return value is a handle to the object being replaced. If the selected object is a region and the function succeeds, the return value is one of the following values.

ValueMeaning
SIMPLEREGIONRegion consists of a single rectangle.
COMPLEXREGIONRegion consists of more than one rectangle.
NULLREGIONRegion is empty.

 

If an error occurs and the selected object is not a region, the return value is NULL. Otherwise, it is HGDI_ERROR.

Remarks

This function returns the previously selected object of the specified type. An application should always replace a new object with the original, default object after it has finished drawing with the new object.

An application cannot select a single bitmap into more than one DC at a time.

ICM: If the object being selected is a brush or a pen, color management is performed.

Examples

For an example, see Setting the Pen or Brush Color.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Wingdi.h (include Windows.h)

Library

Gdi32.lib

DLL

Gdi32.dll

See also

Device Contexts Overview
Device Context Functions
CombineRgn
CreateBitmap
CreateBitmapIndirect
CreateBrushIndirect
CreateCompatibleBitmap
CreateDIBitmap
CreateDIBPatternBrush
CreateEllipticRgn
CreateEllipticRgnIndirect
CreateFont
CreateFontIndirect
CreateHatchBrush
CreatePatternBrush
CreatePen
CreatePenIndirect
CreatePolygonRgn
CreateRectRgn
CreateRectRgnIndirect
CreateSolidBrush
SelectClipRgn
SelectPalette

 

 

Send comments about this topic to Microsoft

Build date: 3/7/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
To DeleteObject or not to DeleteObject ,that is the question...
looking at
CGdiObject::~CGdiObject()
in c:\Program Files\Microsoft Visual Studio 9.0\VC\atlmfc\include\afxwin1.inl,Line 107,It is quite clear that gdi objects need to be deleted. That because you are supposed NOT to leave any of your own created objects selected in one given DC. However,there is an window style that forces windows not to change the DC associated with a window as long as it exists: CS_OWNDC....
So the basic rule must be:
If it is selected in an DC,YOU MUST NOT DeleteObject, if it is not selected,then YOU MUST DeleteObject.

Consequently, if you want to deallocate an MFC CGdiObject-derived object such as an CBitmap you must first deselect it by selecting another CGdiObject or an saved HGDIOBJ(from an previous SelectObject()). Keep in mind that MFC CGdiObject* objects returned by SelectObject() are automatically deleted by mfc in its OnIdle(), so it would be safer to save an handle (HGDIOBJ) rather than an CGdiObject if you use CS_OWNDC.
If you do not have CS_OWNDC added to your window, then you must not preserve(save in member variables) in your class any of the returned GDI handles or objects passed the end of your message handler function(like OnDraw,OnPaint) , because on the next call of the message handler (OnDraw,OnPaint) it is quite possible to get another HDC(or CDC*),totally different than in the one in the previous call of the handler.
Re: SelectObject() my notes
On the contrary - you must not call "DelectObject(savedObject)" at all because savedObject is selected into the DC and may continue to be used.

Additionally, since your code did not create that specific object yourself, another code fragment is responsible for deselecting and deleting it and deleting the same resource twice is usually an error.
SelectObject() my notes

When I use SelectObject, I normally restore the original object to the selection

savedObject = SelectObject(...)
...
SelectObject(savedObject)

Do I need to call DeleteObject() on savedObject in this case? As far as I could find out from the web the answer is no. SelectObject() doesn't increment the reference count.