'Déclaration des constantes Public Const SRCCOPY = &HCC0020 Public Const SRCINVERT = &H660046 Public Const SRCAND = &H8800C6'Déclaration des fonctions Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long Declare Function GetBkColor Lib "gdi32" (ByVal hdc As Long) As Long Declare Function GetTextColor Lib "gdi32" (ByVal hdc As Long) As Long Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Any) As Long Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As LongPublic Function TransBitBlt(ByVal hDCD As Long, ByVal x As Long, ByVal y As Long, ByVal DX As Long, ByVal DY As Long, ByVal hDCS As Long, ByVal X0 As Long, ByVal Y0 As Long, ByVal TransColor As Long) As Boolean Dim RGBBK As Long Dim RetL As Long Dim RetI As Long Dim RGBBKS As Long Dim RGBFG As Long Dim HbmMask As Long Dim HbmT As Long Dim hDCMask As Long Dim F As Boolean F = FalseRetL = SetBkColor(hDCD, TransColor)RGBBK = GetBkColor(hDCD) RGBFG = GetTextColor(hDCD) RGBBKS = GetBkColor(hDCS)RetL = SetTextColor(hDCD, RGB(0, 0, 0))hDCMask = CreateCompatibleDC(hDCS)If IsNull(hDCMask) Then TransBitBlt = F Exit FunctionEnd If HbmMask = CreateBitmap(DX, DY, 1, 1, ByVal 0&)If IsNull(HbmMask) Then RetI = DeleteDC(hDCMask) TransBitBlt = F Exit FunctionEnd If HbmT = SelectObject(hDCMask, HbmMask)RetL = SetBkColor(hDCS, RGBBK)RetI = BitBlt(hDCMask, 0, 0, DX, DY, hDCS, X0, Y0, SRCCOPY)RetL = SetBkColor(hDCD, RGB(255, 255, 255))RetI = BitBlt(hDCD, x, y, DX, DY, hDCS, X0, Y0, SRCINVERT) RetI = BitBlt(hDCD, x, y, DX, DY, hDCMask, 0, 0, SRCAND) RetI = BitBlt(hDCD, x, y, DX, DY, hDCS, X0, Y0, SRCINVERT)F = TrueRetI = SelectObject(hDCMask, HbmT)RetI = DeleteObject(HbmMask)RetI = DeleteDC(hDCMask)RetL = SetBkColor(hDCD, RGBBK) RetL = SetTextColor(hDCD, RGBFG) RetL = SetBkColor(hDCS, RGBBKS)TransBitBlt = F End Function
当然,如果你是用DirectX实现的DirectDraw有它的去背景方法你不必担心。
你可以参阅:
http://www.csdn.net/cnshare/soft/12/12207.shtm
http://www.csdn.net/cnshare/soft/12/12208.shtm
用BitBlt加掩图实现的Super Mario和Tankwar。
--------------------------------------------------------------------
Made by Thirdapple's Studio(http://3rdapple.51.net/)
Public Const SRCCOPY = &HCC0020
Public Const SRCINVERT = &H660046
Public Const SRCAND = &H8800C6'Déclaration des fonctions
Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Declare Function GetBkColor Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function GetTextColor Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Any) As Long
Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As LongPublic Function TransBitBlt(ByVal hDCD As Long, ByVal x As Long, ByVal y As Long, ByVal DX As Long, ByVal DY As Long, ByVal hDCS As Long, ByVal X0 As Long, ByVal Y0 As Long, ByVal TransColor As Long) As Boolean
Dim RGBBK As Long
Dim RetL As Long
Dim RetI As Long
Dim RGBBKS As Long
Dim RGBFG As Long
Dim HbmMask As Long
Dim HbmT As Long
Dim hDCMask As Long
Dim F As Boolean
F = FalseRetL = SetBkColor(hDCD, TransColor)RGBBK = GetBkColor(hDCD)
RGBFG = GetTextColor(hDCD)
RGBBKS = GetBkColor(hDCS)RetL = SetTextColor(hDCD, RGB(0, 0, 0))hDCMask = CreateCompatibleDC(hDCS)If IsNull(hDCMask) Then
TransBitBlt = F
Exit FunctionEnd If
HbmMask = CreateBitmap(DX, DY, 1, 1, ByVal 0&)If IsNull(HbmMask) Then
RetI = DeleteDC(hDCMask)
TransBitBlt = F
Exit FunctionEnd If
HbmT = SelectObject(hDCMask, HbmMask)RetL = SetBkColor(hDCS, RGBBK)RetI = BitBlt(hDCMask, 0, 0, DX, DY, hDCS, X0, Y0, SRCCOPY)RetL = SetBkColor(hDCD, RGB(255, 255, 255))RetI = BitBlt(hDCD, x, y, DX, DY, hDCS, X0, Y0, SRCINVERT)
RetI = BitBlt(hDCD, x, y, DX, DY, hDCMask, 0, 0, SRCAND)
RetI = BitBlt(hDCD, x, y, DX, DY, hDCS, X0, Y0, SRCINVERT)F = TrueRetI = SelectObject(hDCMask, HbmT)RetI = DeleteObject(HbmMask)RetI = DeleteDC(hDCMask)RetL = SetBkColor(hDCD, RGBBK)
RetL = SetTextColor(hDCD, RGBFG)
RetL = SetBkColor(hDCS, RGBBKS)TransBitBlt = F
End Function