Canvas.Paint problem


Bellow is code where I drawing rectangles for my app.
Sub bellow is called from Canvas.Paint, where g is sent from Canvas.Paint as its Graphics.
rct() is declared as Graphics.
I am creating clip for each rectangle I draw.

Canvas object in window is resized to Window.height-15 and Window.width-15 and there is small label as status bar in bottom of window with height=15.

My problem is, when I resize window size (Canvas is also sized after resize), then my drawings is not ending at Canvas end but is overlapping to end of window and over label object.
As I understand drawings should ends at border of Canvas.
Here is screenshot how my rectangles are painted over label and out of Canvas.

[code]Sub DrawGuardOutline(i as integer, g as Graphics)

OvwData(i).X=ClipColumnOffset + ((ClipColumn-1)(ClipWidth+ClipColumnOffset))
OvwData(i).Y=ClipRowOffset + (ClipRow

rct(i)=g.clip(OvwData(i).X,OvwData(i).Y,ClipWidth,ClipHeight)//Create Clip
rct(i).DrawRoundRect(0,0,rct(i).Width,rct(i).Height,7,7)//Draw Outline
rct(i).FillRoundRect(2,2,rct(i).Width-4,rct(i).Height-4,7,7)//Fill Rect
End Sub

Any tips ?



Make sure you have the right and bottom locks set for the canvas.

Hi Greg,

I had unchecked this, but enable of locks did not help.
I am resizing Canvas on each resize of parent window by code.

Weird is, but when I create non-resizeable small canvas 100x100 pixels I got entire rectangles painted outside of Canvas.
It looks like my rectangles are painted on Window background.
But Window.Paint method is empty.


have you tried NOT doing that? let it resize itself by virtue of the fact you locked the corners in place

Tested now…not success with locked corners

Where are ClipWidth and ClipHeight set? Are they taking into account the current size using g.Width and g.Height?

ClipWidth and ClipHeight are set as Property in this window and Value is set in declaration.

ClipWidth As Integer=140

Here is screenshot of rectangles where I have Canvas non-resizable.
Grey is Canvas, Blue is Window Background.

Rectangles are out of Canvas.

It looks as if the g that is being passed in is actually the Window’s graphics property, not the canvas.
I do wonder why you are using a clip?

If the canvas size is known, all you need do is draw rectangles on the canvas, at known positions.

Narrow things down:

In the canvas paint event, try
g.drawrect g.width + 10,g.height +10, 50,50

Does that draw on the window?

If it does, call a method, passing g
Then in the method use the line above.

Does THAT paint outside the canvas?

And finally, if it still doesn’t, change that method to create a clip, and try that.

After those steps, it will at least be clear where the trouble lies.

Hi Jeff,

You are right.
When I pass Graphics.Clip as parameter to Sub then Paint is done on Window background.
I recode it to not use CLIPs and rectangles are now correctly inside Canvas.

Reason why I used clips is, that I pass various objects and texts into rectangles and it was easier for coding to use coordinates of clip.
E.g. draw from position 0,0 etc…Now I will also need to refine code for checking of text length, because I have resizeable rectangles and longer text is outside of rectangle and clip solves it automatcally. (text was trimmed at clip edges)

Btw I think it should work also for clips. So maybe it is bug.