Hier mal mein Lösungsvorschlag.
Code: Alles auswählen
EnableExplicit
Structure canvasitem
img.i
x.i
y.i
width.i
height.i
alphatest.i
name.s
EndStructure
Enumeration
#MyCanvas = 1 ; just to test whether a number different from 0 works now
EndEnumeration
Enumeration 10
#ImagePopup
EndEnumeration
Enumeration 10
#RemoveImage
#ChangeImage
EndEnumeration
Define isCurrentItem=#False
Define currentItemXOffset.i, currentItemYOffset.i
Define Event.i, x.i, y.i, drag.i, hole.i
NewList Images.canvasitem()
Procedure AddImage (List Images.canvasitem(), name.s, x, y, img, alphatest=0)
If AddElement(Images())
Images()\img = img
Images()\x = x
Images()\y = y
Images()\width = ImageWidth(img)
Images()\height = ImageHeight(img)
Images()\alphatest = alphatest
images()\name = name.s
EndIf
EndProcedure
Procedure DrawCanvas (canvas.i, List Images.canvasitem())
If StartDrawing(CanvasOutput(canvas))
Box(0, 0, GadgetWidth(canvas), GadgetHeight(canvas), RGB(255,255,255))
DrawingMode(#PB_2DDrawing_AlphaBlend)
ForEach Images()
DrawImage(ImageID(Images()\img),Images()\x,Images()\y,images()\width,images()\height) ; draw all images with z-order
DrawImage(ImageID(Images()\img),(Images()\x)+images()\width,(Images()\y)+images()\height,6,6)
Next
StopDrawing()
EndIf
EndProcedure
Procedure.i HitTest (List Images.canvasitem(), x, y, *sizing.Integer = 0)
Shared currentItemXOffset.i, currentItemYOffset.i
Protected alpha.i, isCurrentItem.i = #False
If (*sizing)
*sizing\i = #False
EndIf
If LastElement(Images()) ; search for hit, starting from end (z-order)
Repeat
If (*sizing)
If x >= Images()\x + images()\width And x < Images()\x + Images()\width + 6 And y >= Images()\y + Images()\height And y < Images()\y + Images()\height + 6
*sizing\i = #True
MoveElement(Images(), #PB_List_Last)
isCurrentItem = #True
currentItemXOffset = x - Images()\x - Images()\width
currentItemYOffset = y - Images()\y - Images()\height
EndIf
EndIf
If x >= Images()\x And x < Images()\x + Images()\width
If y >= Images()\y And y < Images()\y + Images()\height
alpha = 255
If Images()\alphatest And ImageDepth(Images()\img)>31
If StartDrawing(ImageOutput(Images()\img))
DrawingMode(#PB_2DDrawing_AlphaChannel)
alpha = Alpha(Point(x-Images()\x,y-Images()\y)) ; get alpha
StopDrawing()
EndIf
EndIf
If alpha
MoveElement(Images(), #PB_List_Last)
isCurrentItem = #True
currentItemXOffset = x - Images()\x
currentItemYOffset = y - Images()\y
Break
EndIf
EndIf
EndIf
Until PreviousElement(Images()) = 0
EndIf
ProcedureReturn isCurrentItem
EndProcedure
AddImage(Images(),"PureBasic.bmp", 10, 10, LoadImage(#PB_Any, #PB_Compiler_Home + "/examples/sources/Data/PureBasic.bmp"))
AddImage(Images(),"AlphaChannel.bmp", 100,100, LoadImage(#PB_Any, #PB_Compiler_Home + "/examples/sources/Data/AlphaChannel.bmp"))
AddImage(Images(),"Geebee2.bmp", 50,200, LoadImage(#PB_Any, #PB_Compiler_Home + "/examples/sources/Data/Geebee2.bmp"))
hole = CreateImage(#PB_Any,100,100,32)
If StartDrawing(ImageOutput(hole))
DrawingMode(#PB_2DDrawing_AllChannels)
Box(0,0,100,100,RGBA($00,$00,$00,$00))
Circle(50,50,48,RGBA($00,$FF,$FF,$FF))
Circle(50,50,30,RGBA($00,$00,$00,$00))
StopDrawing()
EndIf
AddImage(Images(),"Loch",170,70,hole,1)
If OpenWindow(0, 0, 0, 420, 420, "Move/Drag Canvas Image", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) = 0
MessageRequester("Fatal error", "Program terminated.")
End
EndIf
CanvasGadget(#MyCanvas, 10, 10, 400, 400)
DrawCanvas(#MyCanvas, Images())
CreatePopupMenu(#ImagePopup)
MenuItem(#RemoveImage,"Remove")
MenuItem(#ChangeImage,"Change")
Define sizing = #False
Repeat
Event = WaitWindowEvent()
If Event = #PB_Event_Gadget And EventGadget() = #MyCanvas
Select EventType()
Case #PB_EventType_LeftButtonDown
x = GetGadgetAttribute(#MyCanvas, #PB_Canvas_MouseX)
y = GetGadgetAttribute(#MyCanvas, #PB_Canvas_MouseY)
isCurrentItem = HitTest(Images(), x, y, @sizing)
If isCurrentItem
DrawCanvas(#MyCanvas, Images())
EndIf
Drag = #True
Case #PB_EventType_LeftButtonUp
Drag = #False
Case #PB_EventType_MouseMove
If Drag = #True
If isCurrentItem
x = GetGadgetAttribute(#MyCanvas, #PB_Canvas_MouseX)
y = GetGadgetAttribute(#MyCanvas, #PB_Canvas_MouseY)
If LastElement(Images())
If (sizing)
Images()\width = x - currentItemXOffset - Images()\x
Images()\height = y - currentItemYOffset - Images()\y
Else
Images()\x = x - currentItemXOffset
Images()\y = y - currentItemYOffset
EndIf
DrawCanvas(#MyCanvas, Images())
EndIf
EndIf
EndIf
Case #PB_EventType_RightButtonUp
x = GetGadgetAttribute(#MyCanvas, #PB_Canvas_MouseX)
y = GetGadgetAttribute(#MyCanvas, #PB_Canvas_MouseY)
isCurrentItem = HitTest(Images(), x, y)
If isCurrentItem
DrawCanvas(#MyCanvas, Images())
DisplayPopupMenu(10,WindowID(0))
EndIf
EndSelect
ElseIf Event = #PB_Event_Menu
Select EventMenu()
Case #RemoveImage
If LastElement(Images())
DeleteElement(Images())
DrawCanvas(#MyCanvas, Images())
EndIf
Case #ChangeImage
If LastElement(Images())
images()\height = 50
images()\width = 50
DrawCanvas(#MyCanvas, Images())
EndIf
EndSelect
EndIf
Until Event = #PB_Event_CloseWindow
Ich hab mich mal versucht an deine Variablenkonventionen zu halten. Aber etwas verwirrend sind manche Sachen schon. Dennoch gut geschrieben.