;
; Conversion for PB of a Vegard Fiksdal Technologies code
; http://www.codeguru.com/vb/gen/vb_graphics/transparency/article.php/c5329/
;
Procedure DrawTransparentImage(DC, Bitmap, x, y, Width, Height, TransparentColor)
; First, create some DC's. These are our gateways To associated
; bitmaps in RAM
maskDC = CreateCompatibleDC_(DC)
tempDC = CreateCompatibleDC_(DC)
SourceDC = CreateCompatibleDC_(DC)
SelectObject_(SourceDC, Bitmap)
; Then, we need the bitmaps. Note that we create a monochrome
; bitmap here!
; This is a trick we use For creating a mask fast enough.
hMaskBmp = CreateBitmap_(Width, Height, 1, 1, 0)
hTempBmp = CreateCompatibleBitmap_(DC, Width, Height)
; Then we can assign the bitmaps to the DCs
;
hMaskBmp = SelectObject_(maskDC, hMaskBmp)
hTempBmp = SelectObject_(tempDC, hTempBmp)
; Now we can create a mask. First, we set the background color
; To the transparent color; then we copy the image into the
; monochrome bitmap.
; When we are done, we reset the background color of the
; original source.
TransparentColor = SetBkColor_(SourceDC, TransparentColor)
BitBlt_(maskDC, 0, 0, Width, Height, SourceDC, 0, 0, #SrcCopy)
SetBkColor_(SourceDC, TransparentColor)
; The first we do with the mask is To MergePaint it into the
; destination.
; This will punch a WHITE hole in the background exactly were
; we want the graphics To be painted in.
BitBlt_(tempDC, 0, 0, Width, Height, maskDC, 0, 0, #SrcCopy)
BitBlt_(DC, X, Y, Width, Height, tempDC, 0, 0, #MergePaint)
; Now we delete the transparent part of our source image. To do
; this, we must invert the mask And MergePaint it into the
; source image. The transparent area will now appear as WHITE.
BitBlt_(maskDC, 0, 0, Width, Height, maskDC, 0, 0, #NotSrcCopy)
BitBlt_(tempDC, 0, 0, Width, Height, SourceDC, 0, 0, #SrcCopy)
BitBlt_(tempDC, 0, 0, Width, Height, maskDC, 0, 0, #MergePaint)
; Both target And source are clean. All we have To do is To And
; them together!
BitBlt_(DC, X, Y, Width, Height, tempDC, 0, 0, #SrcAnd)
; Now all we have To do is To clean up after us And free system
; resources..
DeleteObject_(hMaskBmp)
DeleteObject_(hTempBmp)
DeleteDC_(maskDC)
DeleteDC_(tempDC)
DeleteDC_(SourceDC)
EndProcedure
OpenWindow(0, 100, 100, 200, 200, #PB_Window_SystemMenu, "FastTransparency")
LoadImage(0, "BackGround.bmp") ; Background
LoadImage(1, "ForeGround.bmp") ; Image to be drawn as transparent
UseImage(0)
DC = StartDrawing(ImageOutput())
If DC
DrawTransparentimage(DC, UseImage(1), 0, 0, 97, 97, RGB(255, 255, 255))
StopDrawing()
EndIf
CreateGadgetList(WindowID())
ImageGadget(0, 10, 10, 100, 100, UseImage(0))
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow