Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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?
Posted

1 solution

Simply make a call to GetObjectType().
 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900