Apr 012008
 

Felt the need to retrieve current brush or pen or palette selected into a DC? Sometime back a friend of mine asked me this question, as an answer I gave him a better solution, but the question was a valid one!

So are there any windows api’s that helps us in the regard? Well the answer is yes, use GetCurrentObject and GetObject in tandem to retrieve details of graphics object selected into a dc.

GetCurrentObject signature

HGDIOBJ GetCurrentObject( HDC hdc, // handle to DC
                          UINT uObjectType // object type
                        );

Lookup MSDN for possible values of uObjectType.

GetObject signature

int GetObject( HGDIOBJ hgdiobj,  // handle to graphics object
               int cbBuffer,     // size of buffer for object information
               LPVOID lpvObject  // buffer for object information
             );

Lookup MSDN for possible values of lpvObject in GetObject. Here is an example of how to use these functions!

// Use GetCurrentObject to get current brush selected in to a DC
HBRUSH hCurrentDCBrush = GetCurrentObject( hDC, OBJ_BRUSH );

// Get brush details
LOGBRUSH lCurrentBrushDetails = { 0 };
GetObject( hCurrentDCBrush, sizeof( lCurrentBrushDetails ), &lCurrentBrushDetails );
Mar 272008
 

To retrieve type of a HGDIOBJ object call GetObjectType.

E.g.

GetObjectType( hGdiObj );

Possible return values takenĀ from MSDN…

OBJ_BITMAP
OBJ_BRUSH
OBJ_COLORSPACE
OBJ_DC
OBJ_ENHMETADC
OBJ_ENHMETAFILE
OBJ_EXTPEN
OBJ_FONT
OBJ_MEMDC
OBJ_METAFILE
OBJ_METADC
OBJ_PAL
OBJ_PEN
OBJ_REGION