Picture v Canvas control

I have written some code that implements the canvas control and I was considering replacing the canvas control with the picture object which with the graphics command will pretty well do what the canvas does. What is preventing me from doing this is that the picture object has no events associated with it and thus cannot receive handy detection events such as ‘Mouse Down’, ‘Mouse Drag’ and so on. Would I be right in saying that mouse events with the picture object are click through and are received by the underlying host such as window or whatever the picture is drawn on. The reason that I would like to use the picture object is that it appears to be a much lighter weight control than the Canvas.

They are different things. Canvas is a control used for displaying graphics, including pictures. Picture is not a control, and therefore cannot be placed on a window.

Picture is a in-memory object. Canvas is a UI control.

I better report this issue to Xojo as I can create an picture instance on a window. Try it for yourself Thom. My original question was can you use the picture object ‘click through’ to receive mouse events.

You can create an instance of any class in a window, but:

  • only from classes where the is a constructor without parameters
  • it won’t be visible (or invisible) – it will be nothing else than an ivar

The only reason this is allowed, is that you can use events on such a class instance without AddHandler. Timer (in the classic framework) is a good example.

And Picture is not such a class. It will probably not even compile.

Eli, please do me a favour and load this example project ‘CanvasDrawDrag’. It is located in the Graphics and Multimedia section of the examples. I think you will find that the picture object can be placed on a form (although not via the library of course). It also compiles just fine on my Mac.

Chris, you seem to be confusing things a bit. The CanvasDrawDrag contains only canvases.

Maybe you are confused by a Xojo feature that lets you drag a picture from the disk onto a window, and it automagically appear. But the magic is simply that Xojo creates a new Canvas that has the picture in question as backdrop, while the picture is added to the project.

Yes, I am confused. The following code made me believe that picture objects are being created and not canvas. Maybe I need to eat a big slice of humble pie and spend more time reading code.

[code]Sub Open()
Dim rand As New Random()

#If TargetCarbon
#Pragma Error “Carbon Does Not support Pictures With alpha channels”

// let’s create 26 images to drag around…
For i As Integer = 0 To 25
// define the size, and create the picture
Dim size As Integer = 32 + i * 2
Dim p As New Picture(size * 2, size)

// draw the picture image
Dim randColor As Color = RGB( rand.InRange( 155, 255 ), rand.InRange( 155, 255 ), _
rand.InRange( 155, 255 ), 66 )
p.Graphics.ForeColor = randColor
p.Graphics.FillOval( 0, 0, p.Width, p.Height )

p.Graphics.ForeColor = &c00000000
p.Graphics.DrawOval( 0, 0, p.Width, p.Height )

Dim text As String = Chr( 65 + i )
p.Graphics.TextSize = size * 3/4
p.Graphics.DrawString( text, p.Width / 2 - p.Graphics.StringWidth( text ) / 2, _
p.Height / 2 - p.graphics.TextHeight / 2 + p.Graphics.TextAscent )

// add this to the canvas in a random location
MainCanvas.AddPicture( p, rand.InRange( 0, MainCanvas.Width ), rand.InRange( 0, MainCanvas.Height ) )


End Sub


Look at the second line: MainCanvas.AddPicture

[quote=182788:@chris benton]// add this to the canvas in a random location
MainCanvas.AddPicture( p, rand.InRange( 0, MainCanvas.Width ), rand.InRange( 0, MainCanvas.Height ) )[/quote]

You can create a picture at any time indeed, but you still need a control, such as a canvas or an ImageWell, to display it.

If you look at the code you posted, it does end up by placing the picture into a canvas.