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 );