Is there an easy way to find out what type of GDI object is referenced by HGDIOBJ handle i.e. is it a pen, brush, bitmap, font or region? I can't find WINAPI that does the job. Of course, I can always go the tricky way to determine the type of HGDIOBJ hTestedObj: create a dummy hDc, hPen, hBrush, hBitmap, hFont, hRegion; select these dummy objects into hDc, then compare the handle returned by SelectObject(hDc,hTestedObj) with hPen, hBrush, etc. The following code illustrates the idea.
const CHAR* GdiType(HGDIOBJ hTestedObj){
HDC hDc=CreateCompatibleDC(GetDC(NULL));
assert(hDc!=NULL);
HGDIOBJ hPen=GetStockObject(DC_PEN);
HGDIOBJ hBrush=GetStockObject(DC_BRUSH);
HGDIOBJ hFont=GetStockObject(ANSI_FIXED_FONT);
HGDIOBJ hBitmap=CreateCompatibleBitmap(GetDC(NULL),1,1);
assert(hBitmap!=NULL);
HRGN hRegion=CreateRectRgn(0,0,1,1);
assert(hRegion);
HGDIOBJ hTemp=NULL;
hTemp=SelectObject(hDc,hPen);assert(hTemp!=NULL);
hTemp=SelectObject(hDc,hBrush);assert(hTemp!=NULL);
hTemp=SelectObject(hDc,hFont);assert(hTemp!=NULL);
hTemp=SelectObject(hDc,hBitmap);assert(hTemp!=NULL);
hTemp=SelectObject(hDc,hRegion);assert(hTemp!=HGDI_ERROR);
const CHAR *szRet="?";
hTemp=SelectObject(hDc,hTestedObj);
assert(hTemp!=NULL && hTemp!=HGDI_ERROR);
if(hTemp==hPen)szRet="PEN";
else if(hTemp==hBrush)szRet="BRUSH";
else if(hTemp==hFont)szRet="FONT";
else if(hTemp==hBitmap)szRet="BITMAP";
else if(hTemp==hRegion || hTemp==(HGDIOBJ)SIMPLEREGION ||
hTemp==(HGDIOBJ)COMPLEXREGION ||
hTemp==(HGDIOBJ)NULLREGION)szRet="REGION";
DeleteObject(hBitmap);
DeleteObject(hRegion);
DeleteDC(hDc);
return szRet;
}
But this technique seems somewhat awkward (though it works). Perhaps, there is an elegant way to do the same thing?