Microsoft Small Basic

Program Listing: PXQ334
'Basic Challenge 2
'Write a program with 4 buttons, one in each corner or the GraphicsWindow. They will:
'Change the background colour
'Make a sound
'Show an image in the middle of the GraphicsWindow
'Clear the GraphicsWindow, but not the buttons!

'TO DO Animate opn screen??
'DONE Preview Next pic & BG colour.

flag_ShowImage = 0
flag_clear = 0
colour = GraphicsWindow.GetRandomColor()

Controls.ButtonClicked = ButtonClicked

SetGW()
OpeningMessage()
LoadPic()
Controls.Remove(openningMsg)
Offsets() 'pos for buttons and main pic and btn sizes
ColourPreview()
GraphicsWindow.DrawResizedImage(pic, gw-60, 60, 50, 50) 'preview



AddButtons()

While "true"
If flag_ShowImage = 1 Then 'load next pic didn't work in event routine
GraphicsWindow.DrawResizedImage(pic, picOffset_X, picOffset_Y, PW, PH)
Controls.SetButtonCaption(btn[3], "Wait a Sec")
LoadPic()
GraphicsWindow.DrawResizedImage(pic, gw-60, 60, 50, 50)
Controls.SetButtonCaption(btn[3], "Show Image")
flag_ShowImage = 0
ElseIf flag_clear = 1 Then
AddButtons() 'add this stuff at the same time the pic preview has loaded
ColourPreview()
GraphicsWindow.DrawResizedImage(pic, gw-60, 60, 50, 50)
flag_clear = 0
EndIf
'Program.Delay(20) not needed there's no events triggered
EndWhile

Sub ButtonClicked
Sound.PlayClickAndWait()
caption = Controls.GetButtonCaption(Controls.LastClickedButton)
If caption = "BG Colour" Then 'can change colour while pic is loading in the event loop
GraphicsWindow.BackgroundColor = colour
colour = GraphicsWindow.GetRandomColor()
ColourPreview()
ElseIf caption = "Make Sound" Then
Sound.PlayChimesAndWait() 'play while event loop is running
ElseIf caption = "Show Image" Then
flag_ShowImage = 1
ElseIf caption = "Clear" Then
ClearScreen() 'clear while pic is loading in the event loop
flag_clear = 1
EndIf
EndSub

Sub ColourPreview
GraphicsWindow.DrawRectangle(10, 60, 45, 45)
GraphicsWindow.BrushColor = colour
GraphicsWindow.FillRectangle(10, 60, 45, 45)
EndSub

Sub ClearScreen
shortString = "Clear the Screen"
GraphicsWindow.ShowMessage(shortString, "Clear Message")
GraphicsWindow.Clear()
GraphicsWindow.BackgroundColor = "white"
EndSub

Sub OpeningMessage
openningMsg = Shapes.AddText("Loading.... Please Wait")
Shapes.Move(openningMsg, gw/2-80, gh/2)
Shapes.Zoom(openningMsg, 2,2)
EndSub

Sub LoadPic
url = Flickr.GetPictureOfMoment()
pic = ImageList.LoadImage(url)
EndSub

Sub SetGW
GraphicsWindow.CanResize = 0
gw = 600
gh = 350
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.Left = (Desktop.Width - gw) / 2
GraphicsWindow.Top = (Desktop.Height - gh) / 2 - 50
EndSub

Sub Offsets
capt = "1=BG Colour;2=Make Sound;3=Show Image;4=Clear"
size = "bw=100;bh=35"
btnOffset_R = gw-size["bw"] -10
btnOffset_B = gh-size["bh"] -10

picOffset_X = size["bw"] + 2*10
picOffset_Y = size["bh"] + 2*10
PW = gw - picOffset_X*2
PH = gh - picOffset_Y*2
EndSub

Sub AddButtons
GraphicsWindow.BrushColor = "black"
For i = 1 To 4
btn[i] = Controls.AddButton(capt[i], 0,0)
Controls.SetSize(btn[i], size["bw"], size["bh"])
EndFor

Controls.Move(btn[1], 10, 10)
Controls.Move(btn[2], 10, btnOffset_B)
Controls.Move(btn[3], btnOffset_R , 10)
Controls.Move(btn[4], btnOffset_R, btnOffset_B)
EndSub