How to load a picture in the Contents pane into a Canvas control?

Hi

I’ve got a jpeg file already listed in the Contents pane of my Xojo app because I loaded it as one of the backdrops to be used in a Canvas control. However, it’s only to be displayed when the user presses the mouse button down over that Canvas control. At all other times another jpeg picture displays in that control. I’m wondering how I can load that graphic into another canvas control at runtime, when the user pushes the mouse button down? Can I access the picture in the Contents pane or must I read it in from the jpeg file?

Here’s the picture listed in the Contents pane:
Mmmm! Not sure how to insert a picture!

The code I wrote within the MouseDown event of the Canvas control was: Me.Graphics.DrawPicture(Buttton_MainMenu_Down, 0, 0)
“Buttton_MainMenu_Down” is the ID Xojo has given that picture listed in the Contents pane.

You need to Return True from the MouseDown event if you are planning on doing anything in there (at least that’s what the documentation says)

You do not/should not (and I thought could not) access the graphics property of a canvas control directly…
Everything should happen during the PAINT event of the canvas…

So in the PAINT event you probably want “g.drawpicture button_menu_down,0,0”
assuming button_menu_down is a picture

but there is no need for a mouse click… it will just draw it… so it is kind of hard to tell exactly what it is you are wanting to do.

Sounds like the OP is making a button.
One picture shown until mousedown changes it temporarily.

Take a look at the example that comes with Xojo:

Examples/Desktop/Custom Controls/CanvasButton

Hi Shao, Dave and Jeff

Thanks for your replies. Yes, Jeff, that’s what I’m trying to do. I’ll have a look at that example.

Thanks.

Jeff, I’ve had a look at that example. That’s very helpful thanks.

I think I’m nearly there. But I need to load a picture that is already listed in the Contents pane, called “Buttton_MainMenu_Down”, into a Picture control. Is there a way to do that?

From memory in the paint event:

if me.status = down then g.drawpicture Buttton_MainMenu_Down elseif me.status = up then g.drawpicture something else end if

If the image is in the contents, then you just call it by name. Any resource you drag into the project becomes a property of the same name (without the file extension).

Hi Beatrix

I entered:

if me.status = down then
g.drawpicture Buttton_MainMenu_Down
elseif me.status = up then
g.drawpicture Buttton_MainMenu_Normal
end if

It said the word “status” was a bug and also the names Buttton_MainMenu_Down and Buttton_MainMenu_Normal were bugs.

Hi Tim

So if I want to load that picture into a canvas control, Introduction to Programming with Xojo says to do this call:
“Canvas1.Graphics.DrawPicture(myPic,x,y)”

myPic is a picture that has already been loaded into a Picture control.

If I’m wanting to load my picture that’s in the contents pane - Buttton_MainMenu_Down - into the Canvas control how would i do that please?

Replace “MyPic” with “Button_MainMenu_Down”. However, that code is out of date and should no longer work. Try replacing the backdrop instead, in the MouseDown of your canvas:

Me.Backdrop = Button_MainMenu_Down

Hi Tim

Thanks for that. It worked! That’s a very simple way of doing it!

Is there a more up-to-date source of information on Xojo?

Thanks again.