TPaintBox canvas size lazarus / 31-LCL ---------------------------------------------------------------------- VitalArt · 2016-12-18 15:34 This might be a dumb question, but I seem to have run into a problem. So I have a form, a TPanel on that form, and two TPaintBoxes side-by- side on that panel. I would expect that PaintBox1.canvas would be of the same width as PaintBox1. Instead, both PaintBox1.canvas and PaintBox2.canvas are the width of the panel (minus border spacing, I think). What gives? Example project is attached below. ---------------------------------------------------------------------- Thaddy · 2016-12-18 15:37 Quote from: VitalArt on December 18, 2016, 04:34:16 pmThis might be a dumb question, but I seem to have run into a problem. So I have a form, a TPanel on that form, and two TPaintBoxes side-by- side on that panel. I would expect that PaintBox1.canvas would be of the same width as PaintBox1. Instead, both PaintBox1.canvas and PaintBox2.canvas are the width of the panel (minus border spacing, I think). What gives? No, the paintbox just occupies the size you defined at design time and besides that only that part that is really part of the canvas from the owning control.. You can set the property align to client, though. Then the paintbox is set to the client area, which is the area of the parent without headers and borders. ---------------------------------------------------------------------- VitalArt · 2016-12-18 15:46 No, I am perfectly content with how the PaintBox is situated, I just don't get why its canvas has a different size. All controls have their own Canvases, right? So if a Paintbox is, say, 100 by 50, shouldn't its canvas be 100 by 50? Instead it's the size of the parent of the PaintBox. Am I misunderstanding how the Canvas class works? ---------------------------------------------------------------------- Thaddy · 2016-12-18 15:57 Yes.You misunderstand. The paintbox occupies the area of the parent canvas you specify. If you want to cover the whole parent canvas, set property Align to alClient Maybe I misunderstand but: If that behavior I specified  is not the case, that is a bug. And it is not Delphi compatible. ---------------------------------------------------------------------- VitalArt · 2016-12-18 16:23 Okay, now I'm even more confused. What's the purpose of TPaintBox, then? You could just access the parent's canvas anyway. Also, when I draw to canvases of my two tPaintBoxes, the picture originates at the PaintBox's coordinates, and painting out of bounds of the PaintBox has no effect. Why even have the extra pixels in the Canvas if they are not drawn on the actual form? Maybe I was not clear in what I'm trying to do. I'll attach a picture. Again, my question is the following: why do canvases of the two paintboxes have width that is more than is the width of the paintbox itself? From your answer I gathered that child canvases paint to parent canvas, but again, what is the purpose of them all having the same size? Does that not only waste memory? I would understand if they were all pointers to the same parent canvas, but that is not the case. ---------------------------------------------------------------------- lainz · 2016-12-18 16:29 I've changed your test a bit: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   showmessage('paintbox1.canvas.width: w: ' + cw + ' h: ' + ch + '; paintbox1.canvas.width: ' + s2 + lineending +              ' paintbox1.width: ' + s3 + ' paintbox1.width: ' + s4 + lineending +            ' panel1.width: ' + s5);end; procedure TForm1.PaintBox1Paint(Sender: TObject);begin   cw := IntToStr(PaintBox1.Canvas.Width);   ch := IntToStr(PaintBox1.Canvas.Height);end; And it displays that the width and height of the canvas is of the same size of the form (320, 240). ---------------------------------------------------------------------- lainz · 2016-12-18 16:31 I've made some custom controls and never get this problem, maybe instead of using a paintbox you want to do that: make a custom control. ---------------------------------------------------------------------- VitalArt · 2016-12-18 16:31 Actually, I just read this line in documentation (http://lazarus- ccr.sourceforge.net/docs/lcl/controls/tgraphiccontrol.canvas.html): Don't paint on the entire Canvas! Instead use the dimensions of the TGraphicControl stored in the ClientRect. If you ask for the Canvas.Width or Canvas.Height, you are actually getting the Parent control's Canvas dimensions. But that's what I did, painted without ClienRect, and it worked fine. Is this intended behaviour? I only noticed the problem because one of my cycles kept running to Canvas.Width. ---------------------------------------------------------------------- VitalArt · 2016-12-18 16:35 Well anyway, thanks for your input. I now have a solution and a workaround, though according to the documentation, my initial code shouldn't have worked. I'm not sure if it's a bug. ---------------------------------------------------------------------- lainz · 2016-12-18 20:53 I see. I always use Width and Height inside the control, for that I never noticed this. I don't use PaintBox actually, I always do a new control. ---------------------------------------------------------------------- Remy Lebeau · 2016-12-20 00:22 Quote from: VitalArt on December 18, 2016, 05:23:26 pmOkay, now I'm even more confused. What's the purpose of TPaintBox, then? You could just access the parent's canvas anyway. You could, but it is more work to setup.  TPaintBox offers a convenient component-oriented approach to drawing in specific bounds within the parent. Quote from: VitalArt on December 18, 2016, 05:23:26 pmAlso, when I draw to canvases of my two tPaintBoxes, the picture originates at the PaintBox's coordinates, and painting out of bounds of the PaintBox has no effect. Why even have the extra pixels in the Canvas if they are not drawn on the actual form? They are drawn, just not by the TPaintBox controls.  They are drawn by the TPanel instead. A windowed control (like TPanel) can host child controls, and has its own window, thus its own canvas. A graphical control (like TPaintBox) cannot host children, and has no window of its own, thus no canvas of its own, it shares its parent's canvas instead. When the Parent is painted, it paints itself first, and then signals any graphical children to paint themselves (which rendered onto the parent's underlying canvas), on top of any painting the Parent had drawn.  Any painting done by each graphical child is clipped to stay within the bounds of each child. Quote from: VitalArt on December 18, 2016, 05:23:26 pmAgain, my question is the following: why do canvases of the two paintboxes have width that is more than is the width of the paintbox itself? Because the width is coming from the underlying canvas that belongs to the parent TPanel, and is shared by the two TPaintBoxes, and so the dimensions are coming from the TPanel canvas. Quote from: VitalArt on December 18, 2016, 05:23:26 pmFrom your answer I gathered that child canvases paint to parent canvas For graphical controls, yes.  For windowed controls, no. Quote from: VitalArt on December 18, 2016, 05:23:26 pmbut again, what is the purpose of them all having the same size? Does that not only waste memory? I would understand if they were all pointers to the same parent canvas, but that is not the case. Actually, it is the case, essentially.  The individual TCanvas objects are not shared, but the underlying device context that they render onto is shared. ---------------------------------------------------------------------- Remy Lebeau · 2016-12-20 00:24 Quote from: lainz on December 18, 2016, 05:31:30 pmI've made some custom controls and never get this problem, maybe instead of using a paintbox you want to do that: make a custom control. If by "custom control" you mean TCustomControl, then it is a windowed control and thus has its own canvas.  Create a custom TGraphicControl descendant instead and canvas management changes, but those differences are handled by the framework for you. ---------------------------------------------------------------------- Remy Lebeau · 2016-12-20 00:27 Quote from: VitalArt on December 18, 2016, 05:31:38 pmBut that's what I did, painted without ClienRect, and it worked fine. Is this intended behaviour? A graphical control shares its underlying canvas with its Parent.  After the parent paints itself and then signals the child to paint itself, the parent first clips its canvas before the child starts painting, so any drawing will not go outside the child's bounds. Quote from: VitalArt on December 18, 2016, 05:31:38 pmI only noticed the problem because one of my cycles kept running to Canvas.Width. Use the child control's ClientWidth instead of the Canvas.Width. ---------------------------------------------------------------------- VitalArt · 2016-12-21 18:05 Thanks for your clarification.