怎么样画透明背景的位图?
//该函数摘自MSDN.
voidDrawTransparentBitmap(HDChdc,HBITMAPhBitmap,shortxStart,
shortyStart,COLORREFcTransparentColor)
{
BITMAPbm;
COLORREFcColor;
HBITMAPbmAndBack,bmAndObject,bmAndMem,bmSave;
HBITMAPbmBackOld,bmObjectOld,bmMemOld,bmSaveOld;
HDChdcMem,hdcBack,hdcObject,hdcTemp,hdcSave;
POINTptSize;
hdcTemp=CreateCompatibleDC(hdc);
SelectObject(hdcTemp,hBitmap);//Selectthebitmap
GetObject(hBitmap,sizeof(BITMAP),(LPSTR)&bm);
ptSize.x=bm.bmWidth;//Getwidthofbitmap
ptSize.y=bm.bmHeight;//Getheightofbitmap
DPtoLP(hdcTemp,&ptSize,1);//Convertfromdevice
//tologicalpoints
//CreatesomeDCstoholdtemporarydata.
hdcBack=CreateCompatibleDC(hdc);
hdcObject=CreateCompatibleDC(hdc);
hdcMem=CreateCompatibleDC(hdc);
hdcSave=CreateCompatibleDC(hdc);
//CreateabitmapforeachDC.DCsarerequiredforanumberof
//GDIfunctions.
//MonochromeDC
bmAndBack=CreateBitmap(ptSize.x,ptSize.y,1,1,NULL);
//MonochromeDC
bmAndObject=CreateBitmap(ptSize.x,ptSize.y,1,1,NULL);
bmAndMem=CreateCompatibleBitmap(hdc,ptSize.x,ptSize.y);
bmSave=CreateCompatibleBitmap(hdc,ptSize.x,ptSize.y);
//EachDCmustselectabitmapobjecttostorepixeldata.
bmBackOld=(HBITMAP)::SelectObject(hdcBack,bmAndBack);
bmObjectOld=(HBITMAP)::SelectObject(hdcObject,bmAndObject);
bmMemOld=(HBITMAP)::SelectObject(hdcMem,bmAndMem);
bmSaveOld=(HBITMAP)::SelectObject(hdcSave,bmSave);
//Setpropermappingmode.
SetMapMode(hdcTemp,GetMapMode(hdc));
//Savethebitmapsenthere,becauseitwillbeoverwritten.
BitBlt(hdcSave,0,0,ptSize.x,ptSize.y,hdcTemp,0,0,SRCCOPY);
//SetthebackgroundcolorofthesourceDCtothecolor.
//containedinthepartsofthebitmapthatshouldbetransparent
cColor=SetBkColor(hdcTemp,cTransparentColor);
//CreatetheobjectmaskforthebitmapbyperformingaBitBlt
//fromthesourcebitmaptoamonochromebitmap.
BitBlt(hdcObject,0,0,ptSize.x,ptSize.y,hdcTemp,0,0,
SRCCOPY);
//SetthebackgroundcolorofthesourceDCbacktotheoriginal
//color.
SetBkColor(hdcTemp,cColor);
//Createtheinverseoftheobjectmask.
BitBlt(hdcBack,0,0,ptSize.x,ptSize.y,hdcObject,0,0,
NOTSRCCOPY);
//CopythebackgroundofthemainDCtothedestination.
BitBlt(hdcMem,0,0,ptSize.x,ptSize.y,hdc,xStart,yStart,
SRCCOPY);
//Maskouttheplaceswherethebitmapwillbeplaced.
BitBlt(hdcMem,0,0,ptSize.x,ptSize.y,hdcObject,0,0,SRCAND);
//Maskoutthetransparentcoloredpixelsonthebitmap.
BitBlt(hdcTemp,0,0,ptSize.x,ptSize.y,hdcBack,0,0,SRCAND);
//XORthebitmapwiththebackgroundonthedestinationDC.
BitBlt(hdcMem,0,0,ptSize.x,ptSize.y,hdcTemp,0,0,SRCPAINT);
//Copythedestinationtothescreen.
BitBlt(hdc,xStart,yStart,ptSize.x,ptSize.y,hdcMem,0,0,
SRCCOPY);
//Placetheoriginalbitmapbackintothebitmapsenthere.
BitBlt(hdcTemp,0,0,ptSize.x,ptSize.y,hdcSave,0,0,SRCCOPY);
//Deletethememorybitmaps.
DeleteObject(SelectObject(hdcBack,bmBackOld));
DeleteObject(SelectObject(hdcObject,bmObjectOld));
DeleteObject(SelectObject(hdcMem,bmMemOld));
DeleteObject(SelectObject(hdcSave,bmSaveOld));
//DeletethememoryDCs.
DeleteDC(hdcMem);
DeleteDC(hdcBack);
DeleteDC(hdcObject);
DeleteDC(hdcSave);
DeleteDC(hdcTemp);
}
voidDrawTransparentBitmap(HDChdc,HBITMAPhBitmap,shortxStart,
shortyStart,COLORREFcTransparentColor)
{
BITMAPbm;
COLORREFcColor;
HBITMAPbmAndBack,bmAndObject,bmAndMem,bmSave;
HBITMAPbmBackOld,bmObjectOld,bmMemOld,bmSaveOld;
HDChdcMem,hdcBack,hdcObject,hdcTemp,hdcSave;
POINTptSize;
hdcTemp=CreateCompatibleDC(hdc);
SelectObject(hdcTemp,hBitmap);//Selectthebitmap
GetObject(hBitmap,sizeof(BITMAP),(LPSTR)&bm);
ptSize.x=bm.bmWidth;//Getwidthofbitmap
ptSize.y=bm.bmHeight;//Getheightofbitmap
DPtoLP(hdcTemp,&ptSize,1);//Convertfromdevice
//tologicalpoints
//CreatesomeDCstoholdtemporarydata.
hdcBack=CreateCompatibleDC(hdc);
hdcObject=CreateCompatibleDC(hdc);
hdcMem=CreateCompatibleDC(hdc);
hdcSave=CreateCompatibleDC(hdc);
//CreateabitmapforeachDC.DCsarerequiredforanumberof
//GDIfunctions.
//MonochromeDC
bmAndBack=CreateBitmap(ptSize.x,ptSize.y,1,1,NULL);
//MonochromeDC
bmAndObject=CreateBitmap(ptSize.x,ptSize.y,1,1,NULL);
bmAndMem=CreateCompatibleBitmap(hdc,ptSize.x,ptSize.y);
bmSave=CreateCompatibleBitmap(hdc,ptSize.x,ptSize.y);
//EachDCmustselectabitmapobjecttostorepixeldata.
bmBackOld=(HBITMAP)::SelectObject(hdcBack,bmAndBack);
bmObjectOld=(HBITMAP)::SelectObject(hdcObject,bmAndObject);
bmMemOld=(HBITMAP)::SelectObject(hdcMem,bmAndMem);
bmSaveOld=(HBITMAP)::SelectObject(hdcSave,bmSave);
//Setpropermappingmode.
SetMapMode(hdcTemp,GetMapMode(hdc));
//Savethebitmapsenthere,becauseitwillbeoverwritten.
BitBlt(hdcSave,0,0,ptSize.x,ptSize.y,hdcTemp,0,0,SRCCOPY);
//SetthebackgroundcolorofthesourceDCtothecolor.
//containedinthepartsofthebitmapthatshouldbetransparent
cColor=SetBkColor(hdcTemp,cTransparentColor);
//CreatetheobjectmaskforthebitmapbyperformingaBitBlt
//fromthesourcebitmaptoamonochromebitmap.
BitBlt(hdcObject,0,0,ptSize.x,ptSize.y,hdcTemp,0,0,
SRCCOPY);
//SetthebackgroundcolorofthesourceDCbacktotheoriginal
//color.
SetBkColor(hdcTemp,cColor);
//Createtheinverseoftheobjectmask.
BitBlt(hdcBack,0,0,ptSize.x,ptSize.y,hdcObject,0,0,
NOTSRCCOPY);
//CopythebackgroundofthemainDCtothedestination.
BitBlt(hdcMem,0,0,ptSize.x,ptSize.y,hdc,xStart,yStart,
SRCCOPY);
//Maskouttheplaceswherethebitmapwillbeplaced.
BitBlt(hdcMem,0,0,ptSize.x,ptSize.y,hdcObject,0,0,SRCAND);
//Maskoutthetransparentcoloredpixelsonthebitmap.
BitBlt(hdcTemp,0,0,ptSize.x,ptSize.y,hdcBack,0,0,SRCAND);
//XORthebitmapwiththebackgroundonthedestinationDC.
BitBlt(hdcMem,0,0,ptSize.x,ptSize.y,hdcTemp,0,0,SRCPAINT);
//Copythedestinationtothescreen.
BitBlt(hdc,xStart,yStart,ptSize.x,ptSize.y,hdcMem,0,0,
SRCCOPY);
//Placetheoriginalbitmapbackintothebitmapsenthere.
BitBlt(hdcTemp,0,0,ptSize.x,ptSize.y,hdcSave,0,0,SRCCOPY);
//Deletethememorybitmaps.
DeleteObject(SelectObject(hdcBack,bmBackOld));
DeleteObject(SelectObject(hdcObject,bmObjectOld));
DeleteObject(SelectObject(hdcMem,bmMemOld));
DeleteObject(SelectObject(hdcSave,bmSaveOld));
//DeletethememoryDCs.
DeleteDC(hdcMem);
DeleteDC(hdcBack);
DeleteDC(hdcObject);
DeleteDC(hdcSave);
DeleteDC(hdcTemp);
}
本文地址:http://www.45fan.com/dnjc/69826.html