[Solved] Trackbar flickering lazarus / 31-LCL ---------------------------------------------------------------------- Joanna · 2024-03-05 09:34 Hi everyone I have a ttrackbar control inside of a panel. When I resize it it flickers despite of the fact that I have set doublebuffered to true for both the trackbar and panel. I searched the forums for previous thread about this and it seemed that it had been solved by adding csParentBackground to the control style. It didn't seem to stop flickering for me and on closer inspection i saw this.QuotecsParentBackground = CSPARENTBACKGROUND enum TControlStyleType.csParentBackground C:\lazarus\lcl\controls.pp(257,5) Description [not implemented] tells WinXP to paint the theme background of parent on controls background  (Source) Package LCLBase Does anyone have any other ideas on how to reduce trackball flickering ? it only gets worse when the panel with trackbar is nested inside of another control. im using Lazarus 2.0.12 r64642 FPC 3.2.0 x86_64-win64-win32/win64 with windows 7 ---------------------------------------------------------------------- 440bx · 2024-03-05 09:39 Question: is the location of the trackbar constant ?  IOW, when resizing, does the trackbar need to move or stay in the same place ? ---------------------------------------------------------------------- Joanna · 2024-03-05 09:44 The trackbar will stretch to fill its parent and as it stretches  the little notches under it become further apart. So if I am resizing from right  the left side of trackball will not move ---------------------------------------------------------------------- wp · 2024-03-05 10:01 No flickering here: Win11, Laz/main+FPC 3.2.2 (32 bit) or Laz 2.0.12/FPC 3.2.0 (32bit). But strong flicker in a VM with Win 7, it even flickers when the form size changes but the trackbar is not anchored/aligned and remains unchanged - I don't think that there's much you can do... ---------------------------------------------------------------------- 440bx · 2024-03-05 10:19 Disclaimer: I don't know how to do this in the LCL... in a normal window procedure I'd set the result of processing the WM_ERASEBKGND  to a non-zero value (usually 1), telling Windows that the background has been erased.  Erasing the background is usually the source of flicker - since you have enabled double buffered painting it should be ok to lie about having erased the background - there is at least a chance this will eliminate the flicker.  I'd do that for _both_, the panel and the trackbar. Just in case, WM_ERASEBKGND is documented at: https://learn.microsoft.com/en-us/windows/win32/winmsg/wm-erasebkgnd HTH. ---------------------------------------------------------------------- Joanna · 2024-03-05 12:22 Thanks for the answers. I haven’t tried this on other operating systems yet hopefully it will work better. So I don’t want any windows dependencies. The thing that concerns me more is enum TControlStyleType.csParentBackground not being implemented. Why is it not implemented ? It was implemented for windows xp? ---------------------------------------------------------------------- d7_2_laz · 2024-03-05 13:02 ->  @440bx, fyi (->  as it’s Windows): I had sometimes such situation where I noticed that it had not much effect to ‘return 1’ (for True). Maybe because the control’s background itself already had an unwanted color. What did I want to do instead? To enforce a very early repaint in the desired color. Especially I had that when working with the dark mode: where I saw for some controls white flashes at resize (wm_maximize)  or at program’s start. For the scenario: a Form, and an already existing component in it, i give a simple example snippet that might be easily adapted for other controls and colors. 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 unit MainUnit;…..type   TSplitter = class(ExtCtrls.TSplitter)    procedure WMEraseBkgnd(var Message: TLMEraseBkgnd); message LM_ERASEBKGND;….. implementation …..procedure TSplitter.WMEraseBkgnd(var Message: TLMEraseBkgnd);var vRect : TRect;begin  if Not IsDarkModeEnabled then     exit;  // No need here in light mode  if Message.DC = 0 then Exit;  Brush.Color := $00353535;  vRect := Rect(0, 0, Width, Height);  FillRect(Message.DC, vRect, Brush.Reference.Handle);end; ---------------------------------------------------------------------- Joanna · 2024-03-05 13:32 Another thing I forgot to mention is that the colors in my controls are not defined by windows color themes, I choose them from inside program. Sometimes when the trackbar is nested more it will start to show as a black rectangle while resizing. {I use dark theme in windows } d7_2_laz, how is procedure WMEraseBkgnd called? I noticed that it isn’t overriding ancestor method if there is any.  What if parent.color was substituted for Brush.Color := $00353535;? ---------------------------------------------------------------------- KodeZwerg · 2024-03-05 13:40 Quote from: Joanna on March 05, 2024, 02:32:37 pmhow is procedure WMEraseBkgnd called?You do not "call" it at all. WM = Window Message EraseBkgnd = the Identifier So whenever the Window Message for EraseBkgnd flies over your TSplitter object, it will be executed automagical and overrides the original behavior. ---------------------------------------------------------------------- Joanna · 2024-03-05 13:53 Identifier not found TLMEraseBkgnd Does anyone know which unit has TLMEraseBkgnd? ---------------------------------------------------------------------- d7_2_laz · 2024-03-05 14:00 Quote from: Joanna on March 05, 2024, 02:32:37 pmd7_2_laz, how is procedure WMEraseBkgnd called? I noticed that it isn’t overriding ancestor method if there is any.  What if parent.color was substituted for Brush.Color := $00353535;? I think it’s called by one ancestor of the control, here probably (without having debugged) controls.pp  (see image). And it will be called, as KodeZwreg says, 'automagical' by intercepting the windows message loop by the anchestor. You simply hook into this message for to do something within. But - as you suspected - it probably Windows-specific, don't know if there are equivalents on other OSses. For the second question: you mean: substitute the hard coded element by any variable, eg. parent.color? Yes, of course, the essential element is only that the “unwanted color” doesn’t stay visible .. and visible .. and visible .. until the painting will come into effect. But will be applied as early as possible. I had very good results by that (need to add that the dark theme here is by metadarkstyle component). ---------------------------------------------------------------------- Joanna · 2024-03-05 14:13 Identifier not found for isdarkmodeenabled and fillrect which units am I missing? ---------------------------------------------------------------------- d7_2_laz · 2024-03-05 14:21 No no, as said this dark mode thing was only one use case example where some effects were particularly noticeable. Here, “IsDarkModeEnabled” was from the metadarkstyle package; without this package, it should be removed of course. "FillRect" ->  from unit Windows, OS-specific too. ---------------------------------------------------------------------- Joanna · 2024-03-05 14:38 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 TMY_TRACKBAR.WMERASEBKGND( VAR MESSAGE: TLMERASEBKGND) ;           var vRect : TRect;begin  //if Not IsDarkModeEnabled then  //   exit;  // No need here in light mode  if Message.DC = 0     then Exit;  Brush.Color := Parent.Color;   //       $00353535;  vRect := Rect(0, 0, Width, Height);  FillRect(Message.DC, vRect, Brush.Reference.Handle); END; it fails on line where vrect is assigned TRACKBARS_UNIT_2024.pas(179,18) Fatal: Syntax error, ")" expected but "," found ---------------------------------------------------------------------- d7_2_laz · 2024-03-05 15:03 Quote from: Joanna on March 05, 2024, 03:38:11 pmit fails on line where vrect is assigned Oh, I remember such trap .. has something to do with the order of units within the use clause (there might be different TRect definitions around). Try with direct assignment, eg. vrect.Left:=0;  vrect.Top:=0;   etc, ---------------------------------------------------------------------- Thaddy · 2024-03-05 15:42 or 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 var vRect : windows.Trectrest looks fine. ---------------------------------------------------------------------- d7_2_laz · 2024-03-05 16:08 Thaddy thanks, that’s true too    (i remember a bit of philosophical debate about somewhere else: “prefix it” vs. “change the order of units”). Joanna: mine was from “Classes”,  which allows such a constructor as used. The other is from “Windows”, which does not. A change of ordering might have impact elsewhere, so the prefixing is more sure. Sure as a direct assignment … ---------------------------------------------------------------------- Joanna · 2024-03-05 23:18 Thanks everyone for the advice I put the windows first in the uses section and it finally compiled. Unfortunately the trackbar wiggles when resized.  I’m wondering if I should try just hiding it while it’s being resized.. Nope that didn’t help either. It must be the win 7 operating system at fault. Hopefully flickering will go away if I ever manage to port this to other platforms. ---------------------------------------------------------------------- 440bx · 2024-03-06 01:41 Quote from: Joanna on March 06, 2024, 12:18:39 amNope that didn’t help either. It must be the win 7 operating system at fault. Just for precision's sake... It's extremely unlikely that the problem is in Win7, what's much more likely (read: extremely high probability) is that the trackbar implementation which is part of the Common Controls is buggy (not to mention poorly designed.) As a historical note, in Win95, MS greatly extended the common controls, unfortunately it seems they gave the responsibility for implementing them to a group of high school students who had seen a computer in pictures only (and considering the result, the pictures were probably blurry!.)  The implementation was truly _sad_.  The library has improved over the years but, it still leaves a LOT  to desire and there are still plenty of "deficiencies" in it.  It's fairly obvious why Windows Explorer does not use the Common Controls instead it uses a custom implementation (much more capable and reliable.) Anyway... the above is just FYI for whoever might be curious. ---------------------------------------------------------------------- Joanna · 2024-03-06 01:50 I’m not surprised, however I lack the knowledge of how to fix it. I believe that there is also a tcdtrackbar control I don’t know if it would work better or not. ---------------------------------------------------------------------- d7_2_laz · 2024-03-06 20:45 Joanna, just a thought besides: ok, probably pre-paint / erasebkgnd isn’t responsibly involved here. But what if the prog or component is flooded by too many paintings, maybe due to too many resize messages (remember, what wp did said: it would occur also if the size of the trackbar is fix). What does this little test app say? (Idea behind: if yes (too many messages), there should be found a hook). Btw: you said you tried by “hiding it while it’s being resized”. -> How did you detect a “resize start” vs. a “resize end”? EDIT: as a kind of reference, on Win10 i see such occurence of size/erase/paint messages as shown in the screenshot (having the trackbar aligned). ---------------------------------------------------------------------- lainz · 2024-03-06 21:45 Try bgracontrols TBCTrackBarUpDown. Or custom drawn package... ---------------------------------------------------------------------- wp · 2024-03-06 22:53 Let me test two standard ways to reduce flicker: When I take d7_2_laz's code and add "Message.Msg := 1" (or 0) to the WMEraseBkGnd handler, the flicker is gone, even on Win 7, for both aligned and unaligned cases: 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 TTrackBar.WMEraseBkgnd(var Message: TLMEraseBkgnd);var aRect : TRect;begin  Message.Msg := 1;end; An alternative possibility which seems to work in the same way is to override the CreateParams method and add a specific flag: 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 TTrackbar.CreateParams(var Params: TCreateParams);begin  inherited;  Params.ExStyle := Params.ExStyle or $02000000;end; And finally, without using a sub-classed TTrackbar, this code basically could be added to the win32 interface: 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 class function TWin32WSTrackBar.CreateHandle(const AWinControl: TWinControl;  const AParams: TCreateParams): HWND;var  Params: TCreateWindowExParams;begin  // general initialization of Params  PrepareCreateWindow(AWinControl, AParams, Params);  // customization of Params  with Params do  begin    pClassName := TRACKBAR_CLASS;    WindowTitle := StrCaption;    FlagsEx := FlagsEx or $02000000;  end;  // create window  FinishCreateWindow(AWinControl, Params, false);  Params.WindowInfo^.ParentMsgHandler := @TrackBarParentMsgHandler;  Params.WindowInfo^.ThemedCustomDraw := true;  Result := Params.Window;end; This procedure is in file lcl\interfaces\win32\win32wscomctrls.pp of your Lazarus installation. Add the highlighted line, and recompile your project (but before changing any lazarus file, always make a backup copy). I would appreciate if in particular the last method would be tested (for me it is working on Win 11 and Win 7 and does not seem to have side- effects). ---------------------------------------------------------------------- jamie · 2024-03-06 23:42 WS_EX_COMPOSITED That is a double buffer flag for a window, not supported in W2k, which people still use and I am not sure what effects it would have.  Why not have a DoubleBuffer property in the control incase this does not work in all cases ? ---------------------------------------------------------------------- d7_2_laz · 2024-03-07 09:09 WS_EX_COMPOSITED (0x02000000) – maybe DoubleBuffer hadn’t come into effect here without this flag; see:  https://devblogs.microsoft.com/oldnewthing/20171018-00/?p=97245 wp, I was somehow surprised that it helped here … being interested to know if without the “Exit(True)” (0 or 1, both seen as True? = “Windows, don’t paint, I’ll do this myself”) the amount of event calls was comparable. Could you tell or give a screenshot? (On Win10 it had behaved fully normal, so I cannot see) -- I’ ll try the change of the win32 interface later the day for to see if I notice a caveat. ---------------------------------------------------------------------- Joanna · 2024-03-07 13:37 Thanks for the answers ? Are any of the solutions cross platform without a dependency on the windows unit though? ---------------------------------------------------------------------- wp · 2024-03-07 14:58 So far, the flicker was reported by you only for Windows (7). It is not clear at all whether the Trackbars on other platforms is flickering as well. ---------------------------------------------------------------------- d7_2_laz · 2024-03-07 17:16 Sorry for the interrupt, short break back to the windows patch: @wp, I tried your fix proposal on file lcl\interfaces\win32\win32wscomctrls.pp with Laz 3.0 Win64. I couldn’t see any negative impacts as of now. Also no benefits, of course, as there hadn’t be a negative behavior before … But – just as one could expect, case trackbar not aligned – regarding the messages flow, after form’s resize there are no longer any trackbar’s resize/erasebkgnd/paint messages     // see image Of course, if trackbar is aligned, a form’s resize will be followed by trackbar resize etc. -  All just as it should be ….. Btw, would interest me: in the OS scenario where the flicker had been seen: before the fix, did you notice any strange message dispatching, an unexpected amount of resize/erasebkgnd/paint messages that differed noticeably from my first screenshot? ---------------------------------------------------------------------- wp · 2024-03-07 18:44 Quote from: d7_2_laz on March 07, 2024, 06:16:22 pmBtw, would interest me: in the OS scenario where the flicker had been seen: before the fix, did you notice any strange message dispatching, an unexpected amount of resize/erasebkgnd/paint messages that differed noticeably from my first screenshot? No, nothing special, almost no change to the non-flickering Win11 (one more EraseBkGnd). The screenshots were taken after clearing the memo and clicking the button to increase the form width by 1. I set the Trackbar's Color to yellow to see that the background color in unthemed mode is still working after the patch - yes. ---------------------------------------------------------------------- wp · 2024-03-07 18:47 Here are the screenshots for the same condition after the patch (could not send them in the previous post due to forum restrictions) (Just to explain: Win 7 is flickering before the patch, Win 11 never; and "patch" refers to the change in TWin32WSTrackBar shown above). ---------------------------------------------------------------------- d7_2_laz · 2024-03-07 19:44 Ok; thanks wp for info!              // Win 10:   before: never after: never [I’d think that’s comparibly irrelevant it there are 2 or 3 EraseBkgnd.  - Somehow strange.] ---------------------------------------------------------------------- Joanna · 2024-03-08 00:40 Thanks so for the detailed explanation. ---------------------------------------------------------------------- wp · 2024-03-10 12:00 Noticed that the FlagsEx patch in the win32 widgetset crashes the application under Win2000. The other solution, with handling the WM_ERASEBKGND message, however, works. These are the changes to be applied to win32wscomctrls.pp: - Add this function: 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 function TrackbarWndProc(Window: HWnd; Msg: UInt; WParam: Windows.WParam;  LParam: Windows.LParam): LResult; stdcall;   // <--- NEW FUNCTIONvar  Control: TWinControl;  LMessage: TLMessage;begin  case Msg of    // prevent flickering    WM_ERASEBKGND:      begin        Control := GetWin32WindowInfo(Window)^.WinControl;        LMessage.msg := Msg;      LMessage.wParam := WParam;        LMessage.lParam := LParam;      LMessage.Result := 0;        Result := DeliverMessage(Control, LMessage);      end;    else      Result := WindowProc(Window, Msg, WParam, LParam);  end;end; - Assign it to SubclassWndProc of TWin32WSTrackbar.CreateHandle (and remove the FlagsEx of previous patch): 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 class function TWin32WSTrackBar.CreateHandle(const AWinControl: TWinControl;  const AParams: TCreateParams): HWND;var  Params: TCreateWindowExParams;begin  [...]  with Params do  begin    //FlagsEx := FlagsEx or $02000000;   // <--- TO BE REMOVED    pClassName := TRACKBAR_CLASS;    WindowTitle := StrCaption;    SubClassWndProc := @TrackbarWndProc;   // <--- TO BE ADDED  end;  // create window  [...]end; ---------------------------------------------------------------------- Joanna · 2024-03-11 01:43 Thanks for the answers, someone from the forums gave me a simple solution in chat which does not require windows unit. 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 TMY_TRACKBAR = CLASS(TTrackBar)STRICT PRIVATE   PROCEDURE ERASEBACKGROUND(var Msg:TLMessage) ;MESSAGE LM_ERASEBKGND;END;   PROCEDURE TMY_TRACKBAR.ERASEBACKGROUND( VAR MSG: TLMESSAGE) ; BEGIN   MSG.Result := 1; END;       Apparently the flickering was being caused by the background being erased. ---------------------------------------------------------------------- d7_2_laz · 2024-03-11 18:30 Hi wp, do you perhaps know, why in this internal subclassing patch an ordinary return of Msg.Result := 0 (1, ..)  won’t be sufficient, but an additiona DeliverMessage (having all parameters) would do the job; what’s the difference? Is an extra trigger of the message needed so that it gets respected by the sender? ---------------------------------------------------------------------- wp · 2024-03-11 21:33 Maybe because the Windows-Message must be handled by the LCL system in the proper way? But I don't know, I just took what I found in the win32 widgetset code for other controls. ---------------------------------------------------------------------- Bart · 2024-03-11 22:28 Maybe a WinProcess := False would suffice? Bart ---------------------------------------------------------------------- wp · 2024-03-11 23:16 Quote from: Bart on March 11, 2024, 11:28:28 pmMaybe a WinProcess := False would suffice? No idea... ---------------------------------------------------------------------- d7_2_laz · 2024-03-12 09:14 wp, oh I see. I’d never really paid full attention to this layer when dealing with “Result” in my subclassed window procedures, simply assuming it would be directly passed back to the API. And will keep an eye on in future. Thanks! For erasebkgnd, that might explain some sometimes a bit strange experiences with the return values (Result) for it. Will look more closely then how it cycles through the interface part. Joanna, would the patch mentioned, seen for Win7, work for you native so that the additional helper is no longer needed? ---------------------------------------------------------------------- Joanna · 2024-03-12 09:27 I’m not sure, I don’t know much about doing patches. The simple solution which disables erase background works ok. This makes me wonder why have erase background at all? If the appearance will be refreshed anyway why is it the default behavior to erase it first? I assume if only erase background is used I would see other programs under my program. I think I’ve seen thst in owner drawn combobox before I implemented the drawing code. All this stuff is new to me ---------------------------------------------------------------------- d7_2_laz · 2024-03-12 09:47 That's a message sent by Windows that it will erase the background now (and one might want to hook into or not). The erasure itself mostly might be appropriate, but sometimes it is surely not …. The "Result := 1“ in this context does mean: “Windows, omit you erasure, I’ll do this myself      …..  (or not, if I like so)”  .. ---------------------------------------------------------------------- wp · 2024-03-12 09:57 Quote from: Joanna on March 12, 2024, 10:27:35 amI’m not sure, I don’t know much about doing patches. In the meantime, I applied the WM_EraseBkGnd fix to Laz/main, it has been merged to Laz/fixes and thus will be in Laz 3.4. Therefore, you will not have to apply any of these patches on your own once Laz 3.4 will have been released. ---------------------------------------------------------------------- 440bx · 2024-03-12 10:15 Quote from: Joanna on March 12, 2024, 10:27:35 amThis makes me wonder why have erase background at all? If the appearance will be refreshed anyway why is it the default behavior to erase it first? The answer to that is very simple.  Let's use an example to make it obvious. Imagine you have a window with an image (smaller than the window) you want to keep centered in the window.  The crucial fact in that statement is "centered in the window", this means that every time the window is resized, the image has to move to be at the new center. Now imagine that when the window size is changed, the background isn't erased.  What will happen is that part of image where the previous center used to be will remain there (because the background isn't erased) and part of it will be overpainted with the same image in the new window center. To avoid having part of the image left in the window when it is resized, the background is erased. For an instant, the window has nothing in it (just the background color, whatever it may be) then, after the erase, the image is painted in the new center.  That two- step process, erase which leaves the background clear, followed by the painting of the image is the source of the flicker.  IOW, there would be no flicker if the background had not been erased but then, if the background is not erased and no "special measures" are taken then left overs of the previous image will be visible (which is undesirable.) What can the special measures be ?... if you paint on a bitmap instead of the screen then you can lie to Windows (or whatever window manager you're using) and tell it that the background has been erased even though it hasn't.  The reason that will work is because, on the bitmap you're using as a window replacement, you'll first clear the bitmap then paint on it but, since the bitmap isn't visible until you paint it on the window, the clearing of the bitmap is not visible.  This process is what is commonly called "double buffering". There are other ways to eliminate flicker without using "double buffering" (double buffering consumes memory for the bitmap it paints on which can potentially be large.)  One of those ways is to clear only those areas that need to be clear in the final screen.  To that effect there are plenty of techniques that can be used.  You can find some of them explained here: https://forum.lazarus.freepascal.org/index.php/topic,53791.0.html along with example code you can step through using a debugger. Now you should know why I inquired about the trackbar moving when the window is resized. if the trackbar doesn't move nor changes size then it is trivial to eliminate the flicker.  Study the examples if the reason why is still unclear to you at this time. HTH. ---------------------------------------------------------------------- Joanna · 2024-03-12 11:48 Wp I’m glad problem will be fixed. All this talk about how things work reminds me of a problem I was having with tedit sometimes having the text not showing properly. I’m not sure what would make the text get lost inside the left part of the tedit. I can use the left arrow to get to it. Has anyone ever seen someone like that before. I think I ended up having to reset the text to fix it. Like text := text+’ ‘; text:= trim(text); strange workaround I know..