vc创建位图:根据位图创建区域Rgn--VC代码



这是段根据位图创建区域Rgn--VC代码例子大家可以看www.

//根据位图文件创建区域

HRGNBitmapToRegion(HBITMAPhBmp,COLORREFcTransparentColor=0,COLORREFcTolerance=0x101010)
...{
HRGNhRgn=NULL;

(hBmp)
...{
//CreateamemoryDCinsidewhichwewillscanthebitmapcontent
HDChMemDC=CreateCompatibleDC(NULL);
(hMemDC)
...{
//Getbitmapsize
BITMAPbm;
GetObject(hBmp,(bm),&bm);

//Createa32bitsdepthbitmapandselectitothememoryDC
BITMAPINFOHEADERRGB32BITSBITMAPINFO=...{
(BITMAPINFOHEADER),//biSize
bm.bmWidth,//biWidth;
bm.bmHeight,//biHeight;
1,//biPlanes; [Page]
32,//biBitCount
BI_RGB,//biCompression;
0,//biSizeImage;
0,//biXPelsPerMeter;
0,//biYPelsPerMeter;
0,//biClrUsed;
0//biClrImportant;
};
VOID*pbits32;
HBITMAPhbm32=CreateDIBSection(hMemDC,(BITMAPINFO*)&RGB32BITSBITMAPINFO,DIB_RGB_COLORS,&pbits32,NULL,0); [Page]


(hbm32)
...{
HBITMAPholdBmp=(HBITMAP)SelectObject(hMemDC,hbm32);

//CreateaDCjusttocopythebitmapothememoryDC
HDChDC=CreateCompatibleDC(hMemDC);
(hDC)
...{
//Gethowmanysperrowwehaveforthebitmapbits(roundedupto32bits)
BITMAPbm32;
GetObject(hbm32,(bm32),&bm32);
while(bm32.bmWidthBytes%4)
bm32.bmWidthBytes;

//CopythebitmapothememoryDC
HBITMAPholdBmp=(HBITMAP)SelectObject(hDC,hBmp);
BitBlt(hMemDC,0,0,bm.bmWidth,bm.bmHeight,hDC,0,0,SRCCOPY);

//Forbetterperformances,wewilluheExtCreateRegionfunctiontocreatethe [Page]
//region.ThisfunctiontakeaRGNDATAstructureonentry.Wewilladdrectanglesby
//amountofALLOC_UNITnumberhisstructure.
#ALLOC_UNIT100
DWORDmaxRects=ALLOC_UNIT;
HANDLEhData=GlobalAlloc(GMEM_MOVEABLE,(RGNDATAHEADER)+((RECT)*maxRects));
RGNDATA*pData=(RGNDATA*)GlobalLock(hData);
pData->rdh.dwSize=(RGNDATAHEADER);
pData->rdh.iType=RDH_RECTANGLES;
pData->rdh.nCount=pData->rdh.nRgnSize=0;
SetRect(&pData->rdh.rcBound,MAXLONG,MAXLONG,0,0);

//Keeponhandhighestandlowestvaluesforthe\"transparent\"pixels
BYTElr=GetRValue(cTransparentColor);


BYTElg=GetGValue(cTransparentColor); [Page]
BYTElb=GetBValue(cTransparentColor);
BYTEhr=min(0xff,lr+GetRValue(cTolerance));
BYTEhg=min(0xff,lg+GetGValue(cTolerance));
BYTEhb=min(0xff,lb+GetBValue(cTolerance));

//Scaneachbitmaprowfrombottomtotop(thebitmapisinvertedvertically)
BYTE*p32=(BYTE*)bm32.bmBits+(bm32.bmHeight-1)*bm32.bmWidthBytes;
for(y=0;y<bm.bmHeight;y)
...{
//Scaneachbitmappixelfromlefttoright
for(x=0;x<bm.bmWidth;x)
...{
//Searchforacontinuousrangeof\"nontransparentpixels\"
x0=x; [Page]
LONG*p=(LONG*)p32+x;
while(x<bm.bmWidth)
...{
BYTEb=GetRValue(*p);
(b>=lr&&b<=hr)
...{
b=GetGValue(*p);
(b>=lg&&b<=hg)
...{
b=GetBValue(*p);


(b>=lb&&b<=hb) [Page]
//Thispixelis\"transparent\"
;
}
}
p;
x;
}

(x>x0)
...{
//Addthepixels(x0,y)to(x,y+1)asarectangleheregion
(pData->rdh.nCount>=maxRects) [Page]
...{
GlobalUnlock(hData);
maxRectsALLOC_UNIT;
hData=GlobalReAlloc(hData,(RGNDATAHEADER)+((RECT)*maxRects),GMEM_MOVEABLE);
pData=(RGNDATA*)GlobalLock(hData);
}
RECT*pr=(RECT*)&pData->Buffer;
SetRect(&pr[pData->rdh.nCount],x0,y,x,y+1);
(x0<pData->rdh.rcBound.left)


pData->rdh.rcBound.left=x0; [Page]
(y<pData->rdh.rcBound.top)
pData->rdh.rcBound.top=y;
(x>pData->rdh.rcBound.right)
pData->rdh.rcBound.right=x;
(y+1>pData->rdh.rcBound.bottom)
pData->rdh.rcBound.bottom=y+1;
pData->rdh.nCount;

//OnWindows98,ExtCreateRegionmayfailthenumberofrectanglesistoo
//large(ie:>4000).Therefore,wehavetocreatetheregionbymultiplesteps.
(pData->rdh.nCount2000) [Page]
...{
HRGNh=ExtCreateRegion(NULL,(RGNDATAHEADER)+((RECT)*maxRects),pData);
(hRgn)
...{
CombineRgn(hRgn,hRgn,h,RGN_OR);
DeleteObject(h);
}

hRgn=h;


pData->rdh.nCount=0;
SetRect(&pData->rdh.rcBound,MAXLONG,MAXLONG,0,0); [Page]
}
}
}

//Gotonextrow(remember,thebitmapisinvertedvertically)
p32-=bm32.bmWidthBytes;
}

//Createorextendtheregionwiththereingrectangles
HRGNh=ExtCreateRegion(NULL,(RGNDATAHEADER)+((RECT)*maxRects),pData);
(hRgn)
...{
CombineRgn(hRgn,hRgn,h,RGN_OR);
DeleteObject(h);
}

hRgn=h; [Page]

//Cleanup
GlobalFree(hData);
SelectObject(hDC,holdBmp);
DeleteDC(hDC);
}

DeleteObject(SelectObject(hMemDC,holdBmp));
}

DeleteDC(hMemDC);
}
}

hRgn;
}

BitmapToRegion(m_bmMusicListMask,RGB(255,255,255));





Tags:  vc显示位图 vc位图 vcrgn vc创建位图

延伸阅读

最新评论

发表评论