# PictureButtons - from Memory, and bitmap Files

This simple example shows how one can create pixmaps in memory, which can be used in pictureButton() controls, to give you 'custom' colored buttons with colored text, to be used in your program. It also shows how you can use a bitmap file to 'fill in' a pictureButton.

You should make note of the fact that the pictureButtons are not actually created until *after* all the pixmaps have been drawn into memory first.

-- picturebtn1.exw

    include win32lib.ew
    without warning

    integer x  x = 0

    -- first, declare a normal window,
    -- and 4 pixmaps which will be used in the pictureButtons later.
    constant
        Win = create( Window, "PictureButtons", 0, 10, 10, 220, 160, 0 ),
        btnpix1 = create( Pixmap, "", 0, 0, 0, 80, 25, 0 ),
        btnpix2 = create( Pixmap, "", 0, 0, 0, 80, 25, 0 ),
        btnpix3 = create( Pixmap, "", 0, 0, 0, 80, 25, 0 ),
        btnpix4 = create( Pixmap, "", 0, 0, 0, 80, 25, 0 )
    ----

    -- now, draw the four pixmaps,
    -- by choosing a background color to draw a filled rectangle,
    -- then choose a text color and a font for the button label,
    -- then set the position for the text in the pixmap,
    -- and finally, write the text into the pixmap.
    setPenColor( btnpix1, BrightRed )
    drawRectangle( btnpix1, 1, 0, 0, 80, 25 )
    setTextColor( btnpix1, BrightWhite )
    setFont( btnpix1, "Arial", 16, Bold )
    setPenPos( btnpix1, 12, 1 )
    wPuts( btnpix1, "STOP" )

    -- for our second button
    setPenColor( btnpix2, Yellow )
    drawRectangle( btnpix2, 1, 0, 0, 80, 25 )
    setTextColor( btnpix2, Black )
    setFont( btnpix2, "System", 0, 0 )
    setPenPos( btnpix2, 20, 5 )
    wPuts( btnpix2, "SLOW" )

    -- for our third button
    setPenColor( btnpix3, BrightGreen )
    drawRectangle( btnpix3, 1, 0, 0, 80, 25 )
    setTextColor( btnpix3, Black )
    setFont( btnpix3, "MS Sans Serif", 18, or_all( {Bold, Italic} ) )
    --setPenPos( btnpix3, 0, 0 )
    wPuts( btnpix3, "FAST" )

    -- for our fourth button
    setPenColor( btnpix4, BrightBlue )
    drawRectangle( btnpix4, 1, 0, 0, 80, 25 )
    setTextColor( btnpix4, BrightWhite )
    setFont( btnpix4, "System", 0, 0 )
    setPenPos( btnpix4, 25, 4 )
    wPuts( btnpix4, "Bye !" )

    -- now that we've drawn our buttons into memory,
    -- let's 'link' them to our pictureButtons.

    constant
        button1 = create(  PictureButton,  "",  Win,  0,  0,  84,  29,  getHandle( btnpix1 )  ),
        button2 = create(  PictureButton,  "",  Win,  0,  30,  84,  29,  getHandle( btnpix2 )  ),
        button3 = create(  PictureButton,  "",  Win,  0,  60,  84,  29,  getHandle( btnpix3 )  ),
        button4 = create(  PictureButton,  "",  Win,  0,  90,  84,  29,  getHandle( btnpix4 )  ),
    -- note that button 5 is loaded from a bitmap file !
        button5 = create(  PictureButton,  "",  Win,  130,  12,  36,  36,  loadBitmapFromFile( ".\\Bin\\race2.bmp" )  ),
        display = create(  RText,  "0",         Win,  90,  90,  115,  32,  0  ),

    timer1=1

    -- we'll add some 'tooltips' for two of the buttons.
    setHint( button1, "A ( STOP ) PictureButton from a pixmap drawn in memory" )
    setHint( button5, "A ( FAST ) PictureButton from a bitmap file" )
    setHintFont(  "MS Sans Serif",  12,  or_all( {Bold, Underline} )  )

    setFont( display, "Arial", 20, Bold )
    setTimer( Win, timer1, 1000 )
    showWindow( Win, True )
    setFocus( button2 )

    -- next, all our event handlers for the buttons.

    procedure stop(  integer self,  integer event,  sequence params  )
        killTimer( Win, timer1 )
    end procedure

    procedure slow(  integer self,  integer event,  sequence params  )
        setTimer( Win, timer1, 800 )
    end procedure

    procedure fast(  integer self,  integer event,  sequence params  )
        setTimer( Win, timer1, 200 )
    end procedure

    procedure bye(  integer self,  integer event,  sequence params  )
        closeWindow( Win )
    end procedure

    -- here's our timer routine, which resets after counting to 999,999.

    procedure tick(  integer self,  integer event,  sequence params  )
        x=x+1
        if x > 999999 then x=0 end if
        setText( display, sprintf( "%d", {x} ) )
    end procedure

    setHandler(  button1,  w32HClick,  routine_id( "stop" )  )
    setHandler(  button2,  w32HClick,  routine_id( "slow" )  )
    setHandler(  button3,  w32HClick,  routine_id( "fast" )  )
    setHandler(  button4,  w32HClick,  routine_id( "bye" )  )
    setHandler(  button5,  w32HClick,  routine_id( "fast" )  )
    setHandler(  Win,  w32HTimer,  routine_id( "tick" )  )

    WinMain(Win, Normal)

    ----

* I've commented out the 'position' line for the third button simply because the chosen font 'just-barely' fits into the button anyways.

* I've set the 'focus' onto the second button because this is the 'speed' our timer starts out at.

.. unfortunately, this focus code won't work unless you modify most win32lib's, in the openWindow() procedure, something like the following:

    VOID=w32Func(xShowWindow,{hWnd,styleFlag})
    window_closed[id]=0
    w32Proc(xUpdateWindow,{hWnd})
    -- comment out the xSetFocus() call
    -- below the xShowWindow(), xUpdateWindow() sequence.
    --w32Proc(xSetFocus,{hWnd})
    if(style=Modal

..end of lesson.

CONTENTS