Microsoft Small Basic

Program Listing: NWQ566
' Drag and Drop Sample

GraphicsWindow.Title = "Drag and Drop Sample"
Init()
MainLoop()

Sub MainLoop
' main loop
While "True"
While Not[mouseDown] ' wait until mouse down
Program.Delay(200)
EndWhile
x1 = Shapes.GetLeft(rect)
y1 = Shapes.GetTop(rect)
x2 = x1 + size
y2 = y1 + size
If (x1 <= mx) And (mx < x2) And (y1 <= my) And (my < y2) Then
' mouse clicked on the rectangle
dx = mx - x1
dy = my - y1
While mouseDown ' move the rectangle while mouse down
If mouseMove Then
Shapes.Move(rect, mx - dx, my - dy)
mouseMove = "False"
EndIf
EndWhile
EndIf
EndWhile
EndSub

Sub Init
' initialization
Not = "True=False;False=True;"
GraphicsWindow.BackgroundColor = "#333333"
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "Gold"
size = 40
rect = Shapes.AddEllipse(size, size)
mouseDown = "False"
mouseMove = "False"
GraphicsWindow.MouseDown = OnMouseDown
GraphicsWindow.MouseUp = OnMouseUp
GraphicsWindow.MouseMove = OnMouseMove
EndSub

Sub OnMouseDown
' mouse down event handler
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
mouseDown = "True"
EndSub

Sub OnMouseUp
' mouse up event handler
mouseDown = "False"
EndSub

Sub OnMouseMove
' mouse move event handler
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
mouseMove = "True"
EndSub