How to find the GdiObject type from handle.

8 06 2008


Gdi Objects are represented by handles. Basically all handles are just an integer inside. For instance, if you’re writing a function with accepts HFONT as parameter, its quite possible to pass a typecasted handle of other Gdi object. So how can you determine the real Gdi object type from the handle?


You can use the function – GetObjectType(). See the following code snippet.

// Get the Black Brush.
HGDIOBJ hGdiObj = GetStockObject( BLACK_BRUSH );

// Check the object type with handle.
if( OBJ_BRUSH == GetObjectType( hGdiObj ))
{
    // Yes! The handle points to Gdi Brush.
}

So now add better error checking in your gdi routines. 😉


The values that GetObjectType() returns are,

  • OBJ_BITMAP – Bitmap
  • OBJ_BRUSH – Brush
  • OBJ_COLORSPACE – Color space
  • OBJ_DC Device – Context
  • OBJ_ENHMETADC – Enhanced metafile DC
  • OBJ_ENHMETAFILE – Enhanced metafile
  • OBJ_EXTPEN – Extended pen
  • OBJ_FONT – Font
  • OBJ_MEMDC – Memory DC
  • OBJ_METAFILE – Metafile
  • OBJ_METADC – Metafile DC
  • OBJ_PAL – Palette
  • OBJ_PEN – Pen
  • OBJ_REGION – Region


Targeted Audience – Beginners.

Advertisements