本文介绍了HGDIOBJ的确切类型(笔,画笔,字体等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简便的方法来查找HGDIOBJ句柄引用的GDI对象类型,即它是钢笔,画笔,位图,字体还是区域?我找不到能胜任这项工作的WINAPI.当然,我总是可以采用棘手的方法来确定HGDIOBJ hTestedObj的类型:创建虚拟hDc,hPen,hBrush,hBitmap,hFont,hRegion;将这些虚拟对象选择为hDc,然后将SelectObject(hDc,hTestedObj)返回的句柄与hPen,hBrush等进行比较.以下代码说明了这一想法.

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);声明(hTemp!= HGDI_ERROR);
     const CHAR * szRet =?";
     hTemp = SelectObject(hDc,hTestedObj);
assert(hTemp!= NULL&&hTemp!= HGDI_ERROR);
     if(hTemp == hPen)szRet ="PEN";
    否则if(hTemp == hBrush)szRet ="BRUSH";
    否则if(hTemp == hFont)szRet ="FONT";
    否则if(hTemp == hBitmap)szRet ="BITMAP";
    否则if(hTemp == hRegion || hTemp ==(HGDIOBJ)SIMPLEREGION ||
        hTemp ==(HGDIOBJ)COMPLEXREGION ||
  ;      hTemp ==(HGDIOBJ)NULLREGION)szRet ="REGION";
        DeleteObject(hBitmap);
     DeleteObject(hRegion);
    DeleteDC(hDc);
    return szRet;
}

但是此技术似乎有些尴尬(尽管它可以工作).

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?

推荐答案


这篇关于HGDIOBJ的确切类型(笔,画笔,字体等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 10:15