Option Explicit
Private 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
Private 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
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function ExtFloodFill Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long, ByVal wFillType As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetObject Lib "gdi32.dll" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, ByRef lpObject As Any) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Private Type BITMAP
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type
Private Const filename As String = "C:\doggie2.gif"
Private Const picBackground As Long = &H000100
Private Const backColour As Long = vbBlue
Private backDC As Long, picDC As Long, maskDC As Long, picInfo As BITMAP
Private Sub Form_Load()
'Damn twips
Me.ScaleMode = vbPixels
'Load the picture into the device context and retrieve info
picDC = CreateCompatibleDC(GetDC(0))
SelectObject picDC, LoadPicture(filename)
GetObject LoadPicture(filename), Len(picInfo), picInfo
'Picture background colour
SetBkColor picDC, picBackground
'Create mask
Dim maskBitmap As Long
maskDC = CreateCompatibleDC(GetDC(0))
maskBitmap = CreateBitmap(picInfo.bmWidth, picInfo.bmHeight, 1, 1, ByVal 0)
SelectObject maskDC, maskBitmap
BitBlt maskDC, 0, 0, picInfo.bmWidth, picInfo.bmHeight, picDC, 0, 0, vbSrcCopy
BitBlt picDC, 0, 0, picInfo.bmWidth, picInfo.bmHeight, maskDC, 0, 0, vbSrcInvert
DeleteObject maskBitmap
'Create device context for the background
backDC = CreateCompatibleDC(GetDC(0))
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
'Draw picture on background
Form_MouseMove Button, Shift, x, y
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button > 0 Then
'Draw picture on background
BitBlt backDC, x - Int(picInfo.bmWidth / 2), y - Int(picInfo.bmHeight / 2), picInfo.bmWidth, picInfo.bmHeight, maskDC, 0, 0, vbSrcAnd
BitBlt backDC, x - Int(picInfo.bmWidth / 2), y - Int(picInfo.bmHeight / 2), picInfo.bmWidth, picInfo.bmHeight, picDC, 0, 0, vbSrcPaint
Form_Paint
End If
End Sub
Private Sub Form_Paint()
'Draw background on form
BitBlt Me.hdc, 0, 0, Me.ScaleWidth, Me.ScaleHeight, backDC, 0, 0, vbSrcCopy
End Sub
Private Sub Form_Resize()
'Create bitmap of proper size for the background
Dim backBitmap As Long
backBitmap = CreateCompatibleBitmap(GetDC(0), Me.ScaleWidth, Me.ScaleHeight)
SelectObject backDC, backBitmap
'Create background colour brush
Dim backBrush As Long
backBrush = CreateSolidBrush(backColour)
SelectObject backDC, backBrush
'Fill black area with brush colour
ExtFloodFill backDC, 0, 0, vbBlack, 1
'Draw picture in the middle of background
BitBlt backDC, Int((Me.ScaleWidth - picInfo.bmWidth) / 2), Int((Me.ScaleHeight - picInfo.bmHeight) / 2), picInfo.bmWidth, picInfo.bmHeight, maskDC, 0, 0, vbSrcAnd
BitBlt backDC, Int((Me.ScaleWidth - picInfo.bmWidth) / 2), Int((Me.ScaleHeight - picInfo.bmHeight) / 2), picInfo.bmWidth, picInfo.bmHeight, picDC, 0, 0, vbSrcPaint
Form_Paint
'Cleanup memory
DeleteObject backBitmap
DeleteObject backBrush
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Cleanup memory
DeleteDC picDC
DeleteDC maskDC
DeleteDC backDC
End Sub |