Hi,

I have just had to convert DrawTransparentBitmap to Assembler, so i thought i would share and save others the trouble...




DrawTransparentBitmap PROC hdc:DWORD,hBitmap:DWORD,
xStart:DWORD,yStart:DWORD,
cTransparentColor:DWORD

LOCAL bm:BITMAP
LOCAL cColor:COLORREF
LOCAL bmAndBack:HBITMAP,bmAndObject:HBITMAP
LOCAL bmAndMem:HBITMAP,bmSave:HBITMAP
LOCAL bmBackOld:HBITMAP,bmObjectOld:HBITMAP
LOCAL bmMemOld:HBITMAP,bmSaveOld:HBITMAP
LOCAL hdcMem:HDC,hdcBack:HDC,hdcObject:HDC
LOCAL hdcTemp:HDC,hdcSave:HDC
LOCAL ptSize:POINT

INVOKE CreateCompatibleDC,hdc
mov hdcTemp,eax

INVOKE SelectObject,hdcTemp,hBitmap

INVOKE GetObject,hBitmap,SIZEOF BITMAP,ADDR bm

;Get width of bitmap
push bm.bmWidth
pop ptSize.x
;Get height of bitmap
push bm.bmHeight
pop ptSize.y

;Convert from device to logical points
INVOKE DPtoLP,hdcTemp,ADDR ptSize,1

;Create some DCs to hold temporary data
INVOKE CreateCompatibleDC,hdc
mov hdcBack,eax
INVOKE CreateCompatibleDC,hdc
mov hdcObject,eax
INVOKE CreateCompatibleDC,hdc
mov hdcMem,eax
INVOKE CreateCompatibleDC,hdc
mov hdcSave,eax

;Create a bitmap for each DC
INVOKE CreateBitmap,ptSize.x,ptSize.y,1,1,NULL ;Monochrome DC
mov bmAndBack,eax
INVOKE CreateBitmap,ptSize.x,ptSize.y,1,1,NULL ;Monochrome DC
mov bmAndObject,eax
INVOKE CreateCompatibleBitmap,hdc,ptSize.x,ptSize.y
mov bmAndMem,eax
INVOKE CreateCompatibleBitmap,hdc,ptSize.x,ptSize.y
mov bmSave,eax

;Each DC must select a bitmap object to store pixel data
INVOKE SelectObject,hdcBack,bmAndBack
mov bmBackOld,eax
INVOKE SelectObject,hdcObject,bmAndObject
mov bmObjectOld,eax
INVOKE SelectObject,hdcMem,bmAndMem
mov bmMemOld,eax
INVOKE SelectObject,hdcSave,bmSave
mov bmSaveOld,eax

;Set proper mapping mode
INVOKE GetMapMode,hdc
INVOKE SetMapMode,hdcTemp,eax

;Save the bitmap sent here, because it will be overwritten
INVOKE BitBlt,hdcSave,0,0,ptSize.x,ptSize.y,hdcTemp,0,0,SRCCOPY

;Set the background color of the source DC to the color
;contained in the parts of the bitmap that should be transparent
INVOKE SetBkColor,hdcTemp,cTransparentColor
mov cColor,eax

;Create the object mask for the bitmap by performing a BitBlt
;from the source bitmap to a monochrome bitmap
INVOKE BitBlt,hdcObject,0,0,ptSize.x,ptSize.y,hdcTemp,0,0,SRCCOPY

;Set the background color of the source DC back to the original color
INVOKE SetBkColor,hdcTemp,cColor

;Create the inverse of the object mask
INVOKE BitBlt,hdcBack,0,0,ptSize.x,ptSize.y,hdcObject,0,0,NOTSRCCOPY

;Copy the background of the main DC to the destination
INVOKE BitBlt,hdcMem,0,0,ptSize.x,ptSize.y,hdc,xStart,yStart,SRCCOPY

;Mask out the places where the bitmap will be placed
INVOKE BitBlt,hdcMem,0,0,ptSize.x,ptSize.y,hdcObject,0,0,SRCAND

;Mask out the transparent colored pixels on the bitmap
INVOKE BitBlt,hdcTemp,0,0,ptSize.x,ptSize.y,hdcBack,0,0,SRCAND

;XOR the bitmap with the background on the destination DC
INVOKE BitBlt,hdcMem,0,0,ptSize.x,ptSize.y,hdcTemp,0,0,SRCPAINT

;Copy the destination to the screen
INVOKE BitBlt,hdc,xStart,yStart,ptSize.x,ptSize.y,hdcMem,0,0,SRCCOPY

;Place the original bitmap back into the bitmap sent here
INVOKE BitBlt,hdcTemp,0,0,ptSize.x,ptSize.y,hdcSave,0,0,SRCCOPY

;Delete the memory bitmaps
INVOKE SelectObject,hdcBack,bmBackOld
INVOKE DeleteObject,eax
INVOKE SelectObject,hdcObject,bmObjectOld
INVOKE DeleteObject,eax
INVOKE SelectObject,hdcMem,bmMemOld
INVOKE DeleteObject,eax
INVOKE SelectObject,hdcSave,bmSaveOld
INVOKE DeleteObject,eax

;Delete the memory DCs
INVOKE DeleteDC,hdcMem
INVOKE DeleteDC,hdcBack
INVOKE DeleteDC,hdcObject
INVOKE DeleteDC,hdcSave
INVOKE DeleteDC,hdcTemp
ret

DrawTransparentBitmap ENDP

Posted on 2004-04-10 15:06:54 by Lennon