Code: Select all
; I found a PB_StructureDataBase structure and example somewhere
; on the PB English forum, and got the idea that it might be useful
; for replacing the HBITMAP in a PB image with an HBITMAP obtained
; elsewhere. This appears to work fine, but I assume it may not be
; future-proof. I'm really not even sure how safe this is, but it
; sure seems to do what I need it to!
; Thanks go To whoever found out this structure and how to apply it
Structure PB_StructureDataBase
Bitmap.l
Width.w
Height.w
Depth.w
ColorArray.l
EndStructure
Procedure.l Hbitmap2PBImage(hbitmap.l)
Protected *idb.PB_StructureDataBase
GetObject_(hbitmap, SizeOf(BITMAP), @bm.BITMAP)
If bm\bmBits
*idb = CreateImage(#PB_Any, bm\bmWidth, bm\bmHeight, bm\bmBitsPixel)
If IsImage(*idb)
DeleteObject_(*idb\Bitmap)
*idb\Bitmap = hbitmap
EndIf
EndIf
ProcedureReturn *idb
EndProcedure
; This test is a bit pointless, but I'm sure you clever lot will
; be able to imagine a better way to implement the idea...
img1 = CreateImage(#PB_Any, 256, 256, 24)
img2 = Hbitmap2PBImage(ImageID(img1))
; img1 and img2 now share the same HBITMAP! Let's play with this
; by drawing onto img1 and then displaying img2:
StartDrawing(ImageOutput(img1))
Box(0, 0, 256, 256, #White)
DrawText(20, 20, "PureBasic is #1!", #Red)
StopDrawing()
OpenWindow(0, 0, 0, 256, 256, "Bitmap Fun", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
CreateGadgetList(WindowID(0))
ImageGadget(0, 0, 0, 256, 256, ImageID(img2))
Repeat : Until WindowEvent() = #PB_Event_CloseWindow