Click here to Skip to main content
14,172,361 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Simply make a call to GetObjectType().
   
  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190524.3 | Last Updated 1 Aug 2009
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100