Code archives/Miscellaneous/MaxGui Resize & Redraw Canvas Example

This code has been declared by its author to be Public Domain code.

Download source code

MaxGui Resize & Redraw Canvas Example by Shagwana2005
A short and simple example of how to resize and redraw a display on the fly using a event hook.

When the window is taken off screen or covered, the contence should remain as intended (a big white cross). Have fun!.
'
'  Simple example of dynamic resizing of the canvas and contence
'  Coded by Stephen Greener (aka Shagwana) www.sublimegames.com
'
'

Global pWin:TGadget=CreateWindow("Supersize me!",100,100,400,400,Null,WINDOW_TITLEBAR|WINDOW_RESIZABLE|WINDOW_CLIENTCOORDS)
Global pCan:TGadget=CreateCanvas(0,0,400,400,pWin)

SetMinWindowSize pWin,200,200    'Ensure the window dont go too small
SetGadgetLayout pCan,EDGE_ALIGNED,EDGE_ALIGNED,EDGE_ALIGNED,EDGE_ALIGNED   'Lock the size     

'Draw the display (in the window)
Function DrawDisplay()
  'Set destination for drawing
  SetGraphics CanvasGraphics(pCan)
  SetViewport 0,0,GadgetWidth(pCan),GadgetHeight(pCan)

  'Draw something
  Cls
  SetLineWidth 4
  DrawLine 0,0,GadgetWidth(pCan),GadgetHeight(pCan)
  DrawLine 0,GadgetHeight(pCan),GadgetWidth(pCan),0

  'Swap buffers
  Flip
  End Function


Function EventHook:Object(iId,tData:Object,tContext:Object)
    Local Event:TEvent=TEvent(tData)
    Select Event.Source
      Case pCan
      'Canvas event
      Select Event.id
        Case EVENT_GADGETPAINT
        'Needs redrawing
        DrawDisplay()    'Go redraw the display
        Return Null      'Dont pass the event back as its been delt with
        End Select
      End Select
    'Passback...
    Return Event         'Event pass through (nothing thats been captured)
	End Function

  
Local bQuit:Int=False             'Not to quit yet!

AddHook EmitEventHook,EventHook   'Add in an event hook


'Main event loop...
Repeat

  WaitEvent()

  Select EventSource()

    Case pCan
    Select EventID()
      Case EVENT_GADGETPAINT
      DrawDisplay()
      End Select

    Case pWin
    Select EventID()

      Case EVENT_WINDOWCLOSE
      'Quit the program
      bQuit=True
      End Select

    End Select

  Until bQuit=True
End

Comments

WendellM2005
I found this example useful for understanding hooks due to its simplicity - thanks!


skidracer2005
You could probably replace the call to DrawDisplay in your main loop with an error to ensure your hook is infact digesting the EVENT_GADGETPAINT and your display is not infact being drawn twice for each event.


Code Archives Forum