In a project of mine, I’m drawing graphics in a canvas. For speed purpose (graphics may be updated several times in a second), I don’t want to use a picture as a cache, drawing in this picture and then displaying the picture in the canvas. In addition, having tested that drawing in the foreground of the canvas (in opposite the the Backdrop) is from 30% to more then 100% faster, I’m drawing in the g graphics area of the canvas. Nevertheless, I need a picture of the canvas, at least for dragging purpose and clipboard function.
Here is my problem : Using Canvas.DrawInto into the Paint event does not work, the picture is always blank.
Similarly, trying DrawInto outside of the canvas does not work.
Xojo 2018r4.0, on Mac OS 10.13.6
Am I wrong somewhere?
Is this a bug? How could I code for a workaround (eventually using MBS plugins)?
TIA for any help.
Which paint event? That of your canvas? When the paint event is triggered your canvas is blank, so you would be asking your blank canvas to be painted on your canvas.
How did you try? how did it not work?
Are you implying if you create a buffer/cahce picture you will be using the backdrop to present it? Why not the paint event?
g.DrawPicture(myBufferPicture, 0, 0)
Drawinto does not draw the backdrop or the picture in the canvas. Only the controls.
You will have to use another method.
Yes, of course.
The LR says nothing about that! Great to know that, it explains a lot of things…
In a simple PushButton. The target Picture is blank, whereas the Canvas displays some graphic.
No, I’m using the g graphics area in the Paint event.
Sorry, Michel, not agreeing with that.
RectControl.DrawInto(g as Graphics,x as Integer, y as Integer)
Supported for all project types and targets.
Draws the contents of the RectControl into the specified Graphics context. The parameters x and y are the coordinates of the top, left corner.
Many thanks to both of you, Julien’s explanation about blank canvas during paint event is probably the point, but a little bit surprising. I was expecting that as soon as drawing code in the paint event is finished and before Paint event returns, the Canvas graphic area would be “refreshed”.
AFAIK, there is no way to get the picture of a canvas, but if it’s the backdrop or using a picture cache.
And for speed purpose, I don’t want to use either one.
How would you code this?
Create a method called DoDraw(g as graphics)
Move all your drawing from Paint into there.
Call DoDraw from the Paint passing the g from Paint
If you need it for a clipboard, create a new picture, then call it passing in the g of the picture
I put the code below in a Window (.Open Event) where I have three Canvas:
[code] Dim Clip As New Clipboard
Dim Pict As New Picture(Self.Width, Self.Height, 32)
Clip.Picture = Pict
and I get the color background of the first Canvas (Parent of the two others) and the two others wo displays Image+Text and Image alone.
Michel: update your MemoryBlock: this may help.
El Capitan / Xojo 2015r1. Not tested on current versions.
Julian and Emile, your codes seem good.
I will test them ASAP and hope they will solve my problem.
I will let you know.
What I noticed is the PushButton default is not draw as default, but I do not care.
Many thanks to all replyers.
Julian’s code was the solution, it works like a charm !