On Jul 7, 2006, at 8:24 AM, Chuck Pelto wrote:

Greetings All,

Could anyone point me at an example of how to make a canvas interactive so that a user can click and/or drag to resize and relocate a canvas object inside of a window?


Sure. Define a Canvas subclass called DraggableCanvas. Add private properties

MouseDragX as Integer
MouseDragY as Integer.

Then implement the following two event handlers.

Function MouseDown(X As Integer, Y As Integer) As Boolean
  me.MouseDragX = me.Window.MouseX
  me.MouseDragY = me.Window.MouseY
  Return true
End Function


Sub MouseDrag(X As Integer, Y As Integer)
If me.MouseDragX <> me.Window.MouseX or me.MouseDragY <> me.Window.MouseY then
    me.Left = me.Left + (me.Window.MouseX - me.MouseDragX)
    me.Top = me.Top + (me.Window.MouseY - me.MouseDragY)
    me.MouseDragX = me.Window.MouseX
    me.MouseDragY = me.Window.MouseY
  End if
End Sub


Now drop a DraggableCanvas onto a window. Implement its Paint event handler so that you can see the thing.

Sub Paint(g As Graphics)
  g.ForeColor = &cffffff
  g.FillRect 0, 0, g.Width, g.Height
End Sub


Run the project and drag the Canvas around until you become bored.

Implementing a resizable canvas is more work, though not otherwise difficult (once you know how, of course). In the next two issues of RBD I will have articles that describe how to implement a simple visual layout editor in which you can add, remove, resize and relocate widgets on a canvas.

Charles Yeomans


_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Reply via email to