Newbie Question about Canvases

I have a simple main window where I placed a canvas object. I override the paint event to write a line of text and draw a line and a rectangle. I anchor the canvas to the right and bottom of the main window.

My goal is to have the rectangle, lines and text resize if I change the size of the main window. IOW, I’d like the text and line elements to get bigger or smaller depending upon how large or small the parent window gets. I added an resizing event to the main window, but I’m at a loss as to how I can then cause a repainting of the campus with font sizes and line lengths somehow proportional to the main window’s size.

The canvas itself seems to be resizing properly as I expand the parent window.

Any thoughts will be greatly appreciated!

if resizing event… put “CANVAS1.INVALIDATE”
that will force the canvas to call its PAINT event
put code in the Paint Event to set textsize baseds on the Canvas Height or Width

g.textsize=g.height/50  ..... just an example

[quote=364149:@Dave S]if resizing event… put “CANVAS1.INVALIDATE”
that will force the canvas to call its PAINT event[/quote]

No need to do that, as Harry is anchoring the right and the bottom of the canvas it will trigger a paint event when resizing the window anyway.

In your canvas.paint:

You can use https://documentation.xojo.com/index.php/Graphics.StringWidth and/or http://documentation.xojo.com/index.php/Graphics.StringHeight to find out how large your text will be.

Make adjustments to g.TextSize based on what you can fit in your g.Width

Then call your Graphics.DrawString to render your text.

With your lines, you can use g.Width to set how long they will be.

So you can use g.DrawLine(0, 0, g.width, 0) to draw a line the whole width of your canvas no matter how wide it is.

g.Height and g.Width will be the width and height of the canvas which will be anchored to your window so will change as the window is resized.

That should get you pointed in the right direction, let me know if you need a bit of sample code.

Oh, you guys are good!!!

Here’s what I did with my paint:

Dim factor as double
// Create white background
g.ForeColor = &cffffff
g.FillRect(0, 0, g.Width, g.Height)
factor = me.Width/530
g.ForeColor = &c0080C0
g.PenHeight = 1
g.PenWidth = 3
g.TextFont = “Arial”
g.TextSize = (14 * factor)
g.DrawRect(10, 10, 100 * factor, 100 * factor)
g.DrawString(“Mary Jane factor = " + format(factor,”#0.00"), 10, 300)
g.DrawString(“Canvas width " + format(me.Width,”#0.00"), 10, 250)

Any yes Julian, I didn’t need the Invalidate. Thanks for the pointer to the string sizing commands!

Thanks to everyone!!

Yep! I can testify to that - I get help here ALL the time.