Older Version Newer Version

RodBird RodBird Nov 25, 2015

This is Demo 2 to Accompany Drawing a Tiled Background with CreateCompatibleBitmap

 ' Demo2: Transferring the Image with BitBlt 
Nomainwin
WindowWidth = 600
WindowHeight = 400

Graphicbox #w.g1, 10, 10, 102, 102
Statictext #w.st1a, "Image Source", 8, 120, 110, 32
Graphicbox #w.g2, 140, 10, 440, 119
Statictext #w.st2a, "Image transferred from memory using", 138, 140, 200, 16
Statictext #w.st2b, "CallDLL #gdi32, 'BitBlt'", 138, 156, 200, 16

' Open the Window
Open "Demo" For Window as #w
#w "Trapclose EndProgram"
#w.g2 "Horizscrollbar On 0 600"

' Display Solid Backgrounds
Call hueBackground "#w.g2", "Darkgreen"

' Get handles and dc of second graphicbox with GetDC
' Place handle numbers in array to make global
hWG1 = hWnd(#w.g1)
hDC1 = GetDC(hWG1)
hWG2 = hWnd(#w.g2)
hDC2 = GetDC(hWG2)

' Create two compatible memory DC with CreateCompatibleDC
dcMem1 = CreateCompatibleDC(hDC1) ' Compatible with #w.g1 'memDC
MemDC(1) = dcMem1
dcMem2 = CreateCompatibleDC(hDC2) ' Compatible with #w.g2
MemDC(2) = dcMem2

' Get a simple bitmap for appending
#w.g1 "Getbmp AppendBitmap 0 0 1200 100"

' Obtain the handle of this simple bitmap
hAppendBitmap = hBmp("AppendBitmap")

' Create a large bitmap with CreateCompatibleBmp
' Use ScreenDC and not MemoryDC
hMemBitmap = CreateCompatibleBitmap(hDC2, 1200, 100)

' Select the appending bitmap into the second memory device context
hAppendBitmap = SelectObject(dcMem2, hAppendBitmap)

' Draw the first bitmap and get its handle
Call SimpleHouse "#w.g1", 0, 0
#w.g1 "Getbmp SimpleHouse 0 0 100 100"
#w.g1 "Flush"
hSimpleHouse = hBmp("SimpleHouse")

' Draw the second bitmap and get its handle
Call hueBackground "#w.g1", "Darkblue"
Call SimpleTrees "#w.g1", 0, 0
#w.g1 "Getbmp SimpleTrees 0 0 100 100"
#w.g1 "Flush"
hSimpleTrees = hBmp("SimpleTrees")

' Draw the third bitmap and get its handle
Call hueBackground "#w.g1", "Darkblue"
Call SimpleFence "#w.g1", 0, 0
#w.g1 "Getbmp SimpleFence 0 0 100 100"
#w.g1 "Flush"
hSimpleFence = hBmp("SimpleFence")

' Select the bitmap into the first memory device context
hSimpleHouse = SelectObject(dcMem1, hSimpleHouse)

' BitBlt the 1st memory device context to the 2nd memory device context
null =BitBlt(dcMem2, 0, 0, 100, 100, dcMem1, 0, 0, _SRCCOPY)



' Select the bitmap into the first memory device context
hSimpleTrees = SelectObject(dcMem1, hSimpleTrees)

' BitBlt the 1st memory device bitmap to the 2nd memory device context
null =BitBlt(dcMem2, 100, 0, 100, 100, dcMem1, 0, 0, _SRCCOPY)

' Select the bitmap into the first memory device context
hSimpleFence = SelectObject(dcMem1, hSimpleFence)

' BitBlt the 1st memory device context to the 2nd memory device context
null =BitBlt(dcMem2, 200, 0, 200, 100, dcMem1, 0, 0, _SRCCOPY)

' Deselect the bitmap from memory
hMemBitmap = SelectObject(dcMem2, hMemBitmap)

' ReleaseDC on Screen DC
null = ReleaseDC(hWG1, hDC1)
null = ReleaseDC(hWG2, hDC2)

' DeleteDC on both memory DC's
null = DeleteDC(dcMem1)
null = DeleteDC(dcMem2)

'Loadbmp from handle
Loadbmp "SimpleScene", hMemBitmap
#w.g2 "Drawbmp SimpleScene 0 0"
#w.g2 "Flush"

Wait

Sub EndProgram handle$
Unloadbmp "SimpleHouse"
Unloadbmp "SimpleTrees"
Unloadbmp "SimpleScene"
Close #w
End
End Sub

Function GetDC(handle)
CallDLL #user32, "GetDC", _
handle As Ulong, _
GetDC as Ulong
End Function

Function ReleaseDC(handle, hDC)
CallDLL #user32, "ReleaseDC", _
handle As Ulong, _
hDC As Ulong, _
ReleaseDC As Long
End Function

Function DeleteDC(hDC)
CallDLL #gdi32, "DeleteDC", _
hDC as Ulong, _
result as Boolean long
End Function

Function CreateCompatibleDC(hDC)
CallDLL #gdi32, "CreateCompatibleDC", _
hDC as Ulong, _
CreateCompatibleDC as Ulong
End Function

Function CreateCompatibleBitmap(hDC, wMem, hMem)
CallDLL #gdi32, "CreateCompatibleBitmap", _
hDC as Ulong, _
wMem as Long, _
hMem as Long, _
CreateCompatibleBitmap as Ulong
End Function

Function SelectObject(hDC, hPic)
CallDLL #gdi32, "SelectObject", _
hDC as Ulong, _
hPic as Ulong, _
SelectObject as Ulong
End Function

Function BitBlt(hdcDest, xDest, yDest, wDest, hDest, hdcSource, xSource, ySource, ROP)
CallDLL #gdi32, "BitBlt", _
hdcDest as Ulong, _
xDest as Long, _
yDest as Long, _
wDest as Long, _
hDest as Long, _
hdcSource as Ulong, _
xSource as Long, _
ySource as Long, _
ROP as Ulong, _
result as Boolean long
End Function

Sub hueBackground handle$, hue$
#handle$ "Down; Fill ";hue$
#handle$ "Flush; Discard"
End Sub

Sub SimpleHouse handle$, xLoc, yLoc
Call hueBackground handle$, "Darkblue"
#handle$ "Color 128 64 0; Backcolor Brown"
#handle$ "Place ";xLoc + 5;" ";yLoc + 50
#handle$ "Boxfilled ";xLoc + 95;" ";yLoc + 90
For x = xLoc to xLoc + 100
#handle$ "Line ";xLoc + 50;" ";y + 10;" ";x;" ";y + 50
Next x
#handle$ "Backcolor 128 64 0"
#handle$ "Place ";xLoc + 10;" ";yLoc + 60
#handle$ "Boxfilled ";xLoc + 30;" ";yLoc + 80
#handle$ "Place ";xLoc + 65;" ";yLoc + 60
#handle$ "Boxfilled ";xLoc + 85;" ";yLoc + 80
#handle$ "Place ";xLoc + 35;" ";yLoc + 70
#handle$ "Boxfilled ";xLoc + 60;" ";yLoc + 90
#handle$ "Color 16 16 16; Backcolor 16 16 16"
#handle$ "Place ";xLoc;" ";yLoc + 90
#handle$ "Boxfilled ";xLoc + 100;" ";yLoc + 100
Call SimpleGrass handle$, xLoc, yLoc
End Sub

Sub SimpleTrees handle$, xLoc, yLoc
#handle$ "Color 128 64 0; Backcolor Brown"
#handle$ "Place ";xLoc + 30;" ";yLoc + 60
#handle$ "Boxfilled ";xLoc + 50;" ";yLoc + 90
#handle$ "Place ";xLoc + 65;" ";yLoc + 40
#handle$ "Boxfilled ";xLoc + 75;" ";yLoc + 90
#handle$ "Color 0 64 0; Backcolor 0 128 64"
#handle$ "Place ";xLoc + 40;" ";yLoc + 40
#handle$ "Circlefilled 30"
#handle$ "Place ";xLoc + 70;" ";yLoc + 50
#handle$ "Circlefilled 20"
Call SimpleGrass handle$, xLoc, yLoc
End Sub

Sub SimpleFence handle$, xLoc, yLoc
#handle$ "Color 64 64 64; Backcolor 192 192 192"
#handle$ "Place ";xLoc + 10;" ";yLoc + 60
#handle$ "Boxfilled ";xLoc + 95;" ";yLoc + 70
#handle$ "Place ";xLoc + 10;" ";yLoc + 80
#handle$ "Boxfilled ";xLoc + 95;" ";yLoc + 90
#handle$ "Place ";xLoc + 10;" ";yLoc + 40
#handle$ "Boxfilled ";xLoc + 15;" ";yLoc + 90
#handle$ "Place ";xLoc + 30;" ";yLoc + 40
#handle$ "Boxfilled ";xLoc + 35;" ";yLoc + 90
#handle$ "Place ";xLoc + 50;" ";40
#handle$ "Boxfilled ";xLoc + 55;" ";yLoc + 90
#handle$ "Place ";xLoc + 70;" ";yLoc + 40
#handle$ "Boxfilled ";xLoc + 75;" ";yLoc + 90
#handle$ "Place ";xLoc + 90;" ";yLoc + 40
#handle$ "Boxfilled ";xLoc + 95;" ";yLoc + 90
Call SimpleGrass handle$, xLoc, yLoc
End Sub

Sub SimpleGrass handle$, xLoc, yLoc
For x = xLoc to xLoc + 100
greenHue = Int(Rnd(1) * 128) + 16
#handle$ "Color 0 ";greenHue;" 0"
y = Int(Rnd(1) * 10) + 85
#handle$ "Line ";x;" ";y;" ";x;" 100"
Next x
End Sub