Code
Structure myGadget
rRect.RECT
FG_Img.i
BG_Img.i
EndStructure
Global NewList llGadgets.myGadget()
Global glNormalCur, glMoveCur
Global gOnGadget=-1, gStartMove
Global gCursorPos.POINT
glNormalCur=LoadCursor_(0,#IDC_ARROW)
glMoveCur=LoadCursor_(0,#IDC_SIZEALL)
#Width=800:#Height=600
Procedure DoGadget(sType.s)
If sType="Add"
AddElement(llGadgets())
lWidth=Random(100)+20:lHeight=Random(100)+20
llGadgets()\rRect\left=Random(#Width)-lWidth
llGadgets()\rRect\top=Random(#Height)-lHeight
If llGadgets()\rRect\left<0:llGadgets()\rRect\left=0:EndIf
If llGadgets()\rRect\top<0:llGadgets()\rRect\top=0:EndIf
llGadgets()\rRect\right=llGadgets()\rRect\left+lWidth-1
llGadgets()\rRect\bottom=llGadgets()\rRect\top+lHeight-1
llGadgets()\FG_Img=CreateImage(#PB_Any,lWidth,lHeight)
StartDrawing(ImageOutput(llGadgets()\FG_Img))
Box(0,0,lWidth-1,lHeight-1,RGB(Random(255),Random(255),Random(255)))
StopDrawing()
llGadgets()\BG_Img=GrabImage(0,#PB_Any,llGadgets()\rRect\left,llGadgets()\rRect\top,lWidth,lHeight)
StartDrawing(ImageOutput(0))
DrawImage(ImageID(llGadgets()\FG_Img),llGadgets()\rRect\left,llGadgets()\rRect\top)
StopDrawing()
SetGadgetState(0,ImageID(0))
EndIf
If sType="Del"
If ListSize(llGadgets())
LastElement(llGadgets())
FreeImage(llGadgets()\FG_Img)
StartDrawing(ImageOutput(0))
DrawImage(ImageID(llGadgets()\BG_Img),llGadgets()\rRect\left,llGadgets()\rRect\top)
StopDrawing()
SetGadgetState(0,ImageID(0))
FreeImage(llGadgets()\BG_Img)
DeleteElement(llGadgets())
EndIf
EndIf
EndProcedure
CreateImage(0,#Width,#Height-200)
CreateImage(1,16,16)
StartDrawing(ImageOutput(1))
LineXY(ImageWidth(1)/2,0,ImageWidth(1)/2,ImageHeight(1)-1,RGB(127,127,127))
LineXY(0,ImageHeight(1)/2,ImageWidth(1)-1,ImageHeight(1)/2,RGB(127,127,127))
StopDrawing()
hBrush=CreatePatternBrush_(ImageID(1))
OpenWindow(0,0,0,#Width,#Height,"TEST")
ImageGadget(0,0,0,#Width,#Height-200,ImageID(0))
ButtonGadget(1,50,#Height-100,100,25,"Add gadget")
ButtonGadget(2,650,#Height-100,100,25,"Del gadget")
hdc=StartDrawing(ImageOutput(0))
rRect.RECT\top=0:rRect\left=0
rRect.RECT\bottom=#Height-200:rRect\right=#Width
FillRect_(hdc,rRect,hBrush)
StopDrawing()
SetGadgetState(0,ImageID(0))
Repeat
EventID=WaitWindowEvent()
Select EventID
Case #PB_Event_Gadget
Select EventGadget()
Case 0
EventGadgetID=EventGadget()
EventType=EventType()
If EventType=#PB_EventType_LeftClick
If gOnGadget>=0
gStartMove=1
EndIf
EndIf
Case 1 : DoGadget("Add")
Case 2 : DoGadget("Del")
EndSelect
Case #WM_LBUTTONDOWN
Case #WM_LBUTTONUP
If gStartMove
SetCursor_(glNormalCur)
gStartMove=0
gOnGadget=-1
EndIf
Case #WM_MOUSEMOVE
If gStartMove
lCursorPos.POINT\x=WindowMouseX(0)
lCursorPos\y=WindowMouseY(0)
If lCursorPos\x<>gCursorPos\x Or lCursorPos\y<>gCursorPos\y
SelectElement(llGadgets(),gOnGadget)
StartDrawing(ImageOutput(0))
DrawImage(ImageID(llGadgets()\BG_Img),llGadgets()\rRect\left,llGadgets()\rRect\top)
StopDrawing()
lWidth=ImageWidth(llGadgets()\BG_Img)
lHeight=ImageHeight(llGadgets()\BG_Img)
FreeImage(llGadgets()\BG_Img)
llGadgets()\rRect\left+lCursorPos\x-gCursorPos\x
llGadgets()\rRect\top+lCursorPos\y-gCursorPos\y
llGadgets()\rRect\right=llGadgets()\rRect\left+lWidth-1
llGadgets()\rRect\bottom=llGadgets()\rRect\top+lHeight-1
llGadgets()\BG_Img=GrabImage(0,#PB_Any,llGadgets()\rRect\left,llGadgets()\rRect\top,lWidth,lHeight)
StartDrawing(ImageOutput(0))
DrawImage(ImageID(llGadgets()\FG_Img),llGadgets()\rRect\left,llGadgets()\rRect\top)
StopDrawing()
SetGadgetState(0,ImageID(0))
gCursorPos\x=lCursorPos\x:gCursorPos\y=lCursorPos\y
EndIf
Else
If ListSize(llGadgets())
gCursorPos\x=WindowMouseX(0)
gCursorPos\y=WindowMouseY(0)
ResetList(llGadgets())
While NextElement(llGadgets())
If PtInRect_(@llGadgets()\rRect,gCursorPos\y<<32+gCursorPos\x)
gOnGadget=ListIndex(llGadgets())
SetCursor_(glMoveCur)
Break
EndIf
Wend
EndIf
EndIf
EndSelect
Until EventID=#PB_Event_CloseWindow