Paintbox + Scrollbox leads to periodical redraw lazarus / 39-Graphics ---------------------------------------------------------------------- MSABC · 2016-04-12 19:51 Perhaps the thousands question about this issues, but I did not find this one in the forum: I've got a Paintbox inside a Scrollbox in the usual way (as I think): Structure: MyScrollBox : TScrollBox    MyScreen : TPaintBox Events: MyScreen.OnPaint  : ScreenOnPaint MyScreen.OnResize : ScreenResize MyScrollBox.OnPaint: - MyScrollBox.OnResize: ScreenResize 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 procedure TFormFTAMain.ScreenResize(Sender: TObject);begin  UpdateForm(SCR_DEF); // =>  redraw bitmap and assign it to MyScreenend;             procedure TFormFTAMain.ScreenOnPaint(Sender: TObject);begin  UpdateForm(SCR_DEF); // =>  redraw bitmap and assign it to MyScreenend;        unfortunately this does not work perfect:    Works fine under Linux/KDE (QT). Works fine under Linux/XFCE (GTK). Does not work totally on Windows (Vista):  - If the bitmap and thus the screen are smaller than the scroll box, it's working.  - If the bitmap and thus the screen are larger than the scroll box, it's not working:       - Scrollbars appear, that's OK;       - The image is redrawn continuously (several times per second), while almost          any other program response is blocked.   I searched for a bug in my software like circular calling of the redraw procedure - and got stuck;  Maybe I need some help to get out of my brain-deadlock. Has any body a hint in which cases this behaviour may occur? thanks in advance Martin ---------------------------------------------------------------------- MSABC · 2016-04-14 21:11 Once again to this one: It seems to me that the ScrollBox raises an OnPaint event, everytime a scrollbar becomes visible. That would lead to an infinte loop: Redraw => OnPaint => Redraw => ... Any hints? Regards Martin ---------------------------------------------------------------------- wp · 2016-04-14 21:42 You should show what the "UpdateForm()" is doing, or better: write a compilable demo which you upload here (only .pas, .lfm, .lpi, .lpr files, plus the picture file). From my side, find in the attachment a little demo which paints the Lazarus cheetah onto a paintbox sitting in a smaller scrollbox, and this is working correctly in Windows. (Note, if the demo crashes with an error you must adapt the path to the cheetah image in the FormCreate procedure). ---------------------------------------------------------------------- howardpc · 2016-04-14 22:17 Quote from: MSABC on April 12, 2016, 09:51:08 pm Events: ... MyScrollBox.OnResize: ScreenResize Why does your scrollbox vary in size? The normal use of a scrollbox is as a fixed size control. Nor do you normally need to consider the resizing of the scrollbox contents (see wp's example - no OnResize events anywhere). The whole point of scrollbox functionality is that it adapts to contents resizing automatically. Someone else has already coded and debugged any scrolling needed because of contents size changes. Just set AutoScroll=True and relax, enjoying someone else's hard work. Then if scrollbars are needed, they will appear, whatever the relative sizes of scrollbox and contents. ---------------------------------------------------------------------- MSABC · 2016-04-15 17:10 @WP: thanks for your example; It works without periodical redrawing. Thus I made a small model of my current project and got at least one reason for the redraw I don't understand. (SWbyMSABC.zip) I added a label to the main form which caption I set to the counted redraw numbers. This labels counts up, if the scrollbars are visible. If I remove the line with the label update, redrawing does not occur !? I also added this label to your example; => it redraws at visible scrollbars (SWbyWP.zip) => Why does the change of label caption on the main formular affect redrawing dependen on the visibility of scrollbars? In my main project I added this redraw-counter only after I had this effect (why else?). I'm curious if there are other, similar effects. All only on Windows Vista 32bit - not on Linux-systems I tested: @howardpc: You're right, my resize and onpaint events are not always needed, but that does not look like the cause. Nevertheless I will clean this up. ---------------------------------------------------------------------- wp · 2016-04-15 22:38 Your Redraw code is the painting code of the Paintbox, but you change the width and height of the Paintbox in there. Don't do this here because it triggers another painting cycle! You may even end up in an infinite loop. The correct place to resize the Paintbox is in the button click events. And there, don't call "Redraw" directly, but call Paintbox.Invalidate instead. This posts a repainting request to the message loop, and the OS can decide whether it must be executed or can be skipped. (The attached modified demo of yours shows this in the comments of the ButtonClick events.) Updating a label caption within the paintbox's OnPaint event - that's what you did to my demo program - seems to trigger a repaint of the entire form. Don't ask me why... But since this repaints the paintbox it changes the label caption again, and you can nicely see the endless loop there. So, as a rule of thumb: The OnPaint handler should only execute drawing commands, nothing else. Since you want to see the redraw counter, but you cannot use a label or any other visual control, you can draw it onto the paintbox in the ReDraw method - that's ok because its the own code of the paintbox. ---------------------------------------------------------------------- MSABC · 2016-04-18 21:08 Thanks, WP I worked on that. I removed almost all things from the OnPaint-recall and left only 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 PaintBox1.Canvas.Draw(0, 0, Img);The bitmap Img is now a static image that is never freed, but every time ready to be mapped to the Paint box. I never observed problems with the resizing of th PaintBox inside the ONPaint callback. Maybe a further topic I posted, was an effect of this => "TSpeedButtons in Groupboxes" It's also removed. "...seems to trigger a repaint of the entire form. Don't ask me why...", ... only if scroll bars are activated ... still appears strange to me ... Regards M.Schäfer