Microsoft Small Basic

Program Listing: MHC784
' Button without Controls
' Copyright © 2018 Nonki Takahashi. The MIT License.

GraphicsWindow.Title = "Button without Controls"
Init()
GraphicsWindow.FontSize = 16
GraphicsWindow.FontName = "Courier New"
GraphicsWindow.BrushColor = "Black"
btn = "width=100;height=30;color=LightGray;caption=CLICK ME;"
btn["x"] = Math.GetRandomNumber(gw - btn["width"] - 20) + 10
btn["y"] = Math.GetRandomNumber(gh - btn["height"] - 20) + 10
DrawButton()
GraphicsWindow.MouseMove = OnMouseMove
GraphicsWindow.MouseDown = OnMouseDown
While "True"
If mouseDown Then
If (x1 <= dx) And (dx <= x2) And (y1 <= dy) And (dy <= y2) Then
ClearButton()
btn["x"] = Math.GetRandomNumber(gw - btn["width"] - 20) + 10
btn["y"] = Math.GetRandomNumber(gh - btn["height"] - 20) + 10
DrawButton()
EndIf
mouseDown = "False"
ElseIf mouseMove Then
If (x1 <= mx) And (mx <= x2) And (y1 <= my) And (my <= y2) Then
btn["color"] = "LightBlue"
Else
btn["color"] = "LightGray"
EndIf
DrawButton()
mouseMove = "False"
Else
Program.Delay(100)
EndIf
EndWhile

Sub OnMouseDown
mouseDown = "True"
dx = GraphicsWindow.MouseX
dy = GraphicsWindow.MouseY
EndSub

Sub OnMouseMove
mouseMove = "True"
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
EndSub

Sub Init
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
EndSub

Sub DrawButton
bc = GraphicsWindow.BrushColor
GraphicsWindow.BrushColor = btn["color"]
GraphicsWindow.FillRectangle(btn["x"], btn["y"], btn["width"], btn["height"])
fs = GraphicsWindow.FontSize
len = Text.GetLength(btn["caption"])
x = btn["x"] + (btn["width"] - len * fs * 0.6) / 2
y = btn["y"] + (btn["height"] - fs) / 2
GraphicsWindow.BrushColor = bc
GraphicsWindow.DrawText(x, y, btn["caption"])
x1 = btn["x"]
y1 = btn["y"]
x2 = x1 + btn["width"] - 1
y2 = y1 + btn["height"] - 1
EndSub

Sub ClearButton
bc = GraphicsWindow.BrushColor
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(btn["x"], btn["y"], btn["width"], btn["height"])
GraphicsWindow.BrushColor = bc
EndSub