Creating a custom graphics control

by Richard Russell, August 2014

BBC BASIC supports drawing text, graphics, images etc. to the 'mainwin' and, by means of the MULTIWIN library, to one or more separate windows. The code listed below demonstrates how to create a custom graphics control to which such output can also be drawn. This allows you to display animated graphics etc. in a dialogue box.

This example opens a dialogue box containing four graphicboxes, which are independently updated. Note that version 2.1 or later of the MULTIWIN library is required.

        INSTALL @lib$+"WINLIB2"
        INSTALL @lib$+"MULTIWIN"
 
        REM!WC Windows Constants:
        BS_DEFPUSHBUTTON = 1
        WS_BORDER = &800000
        WS_CHILD = &40000000
        WS_GROUP = &20000
        WS_VISIBLE = &10000000
 
        REM Prepare four new graphics windows:
        PROC_multiwin(4)
 
        dlg%=FN_newdialog("Dialogue box", 20, 20, 160, 128, 8, 1000)
        PROC_groupbox(dlg%, "Group box", 0, 4, 4, 152, 96, WS_GROUP)
 
        REM Create four custom graphics controls:
        PROC_dlgctrl(dlg%, "", 101, 12, 14, 64, 38, WS_VISIBLE OR WS_CHILD OR WS_BORDER, "BBCMulti")
        PROC_dlgctrl(dlg%, "", 102, 84, 14, 64, 38, WS_VISIBLE OR WS_CHILD OR WS_BORDER, "BBCMulti")
        PROC_dlgctrl(dlg%, "", 103, 12, 56, 64, 38, WS_VISIBLE OR WS_CHILD OR WS_BORDER, "BBCMulti")
        PROC_dlgctrl(dlg%, "", 104, 84, 56, 64, 38, WS_VISIBLE OR WS_CHILD OR WS_BORDER, "BBCMulti")
 
        PROC_pushbutton(dlg%, "OK", 1, 12, 108, 56, 14, WS_GROUP OR BS_DEFPUSHBUTTON)
        PROC_pushbutton(dlg%, "Cancel", 2, 92, 108, 56, 14, 0)
 
        PROC_showdialog(dlg%)
        ON CLOSE PROC_closedialog(dlg%):PROC_selectwin(0):QUIT
        ON ERROR PROC_closedialog(dlg%):PROC_selectwin(0):PRINT'REPORT$:END
 
        REM Initialise the custom controls and assign each to a window number:
        hgb1% = FN_initctrl(dlg%, 101, 1)
        hgb2% = FN_initctrl(dlg%, 102, 2)
        hgb3% = FN_initctrl(dlg%, 103, 3)
        hgb4% = FN_initctrl(dlg%, 104, 4)
 
        Click%=0
        ON SYS Click% = @wparam% : RETURN
        REPEAT
          WAIT 10
 
          PROC_selectwin(1)
          COLOUR RND(15)-1
          PRINT '"Hello world";
 
          PROC_selectwin(2)
          GCOL RND(15)-1
          RECTANGLE FILL RND(100),RND(100),RND(100),RND(100)
 
          PROC_selectwin(3)
          GCOL RND(15)-1
          CIRCLE FILL RND(100), RND(100), RND(100)
 
          PROC_selectwin(4)
          GCOL RND(15)-1
          DRAW RND(300)-50, RND(200)-50
 
          PROC_selectwin(0)
          click%=0
          SWAP Click%, click%
        UNTIL click%=1 OR click%=2 OR !dlg%=0
 
        IF click%=1 THEN
          PRINT "OK pressed"
        ELSE
          PRINT "Cancel pressed"
        ENDIF
 
        REM Close the graphics windows and the dialogue box:
        PROC_closewin(1)
        PROC_closewin(2)
        PROC_closewin(3)
        PROC_closewin(4)
        PROC_closedialog(dlg%)
        END

The parameters passed to FN_initctrl are the dialogue box pointer (as returned from FN_newdialog), the ID number assigned to the control (as specified in the call to PROC_dlgctrl) and the window number (between 1 and the number specified in the call to PROC_multiwin, in this case 4).