Doublebuffer form flickering

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 2 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
nikcomp7109
Posts: 13
Last visit: Thu Mar 02, 2023 6:43 am

Doublebuffer form flickering

Post by nikcomp7109 »

Product, version and build:5.8.191
Operating system:10
PowerShell version(s):7


Trying to create some larger forms but when resizing I get a lot of flicker
I understand that powershell uses dialogs vs true forms
A method called doublebuffering was mentioned as something that could help but no idea where that would be applied in my project.
Any assitance appreciated
nikcomp7109
Posts: 13
Last visit: Thu Mar 02, 2023 6:43 am

Re: Doublebuffer form flickering

Post by nikcomp7109 »

So I did find doublebuffer for form but its not really making a difference with all the flickering on the controls. I suspect I need to use suspendlayout() or something similar but no idea how to apply this as it pertains to the controls in the project as the studio kind of hides the "add.control" for example.
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Doublebuffer form flickering

Post by Alexander Riedel »

You don’t provide a lot of information here. “Larger” is relative. “A lot of flicker” is equally relative. “Controls” is too vague. The rendering of a list control and a tree controls is substantially different from a label or a button.
But first of all, there is no such thing as “true forms”, PowerShell, C#, VB.NET, Managed C++ all use Windows Forms (in this context). There are differences in threading models and execution speed (compiled vs interpreted) etc. but it is always Windows Forms.

You don’t mention what flickers, so I am going a little broad here.
Double buffering usually refers to drawing content (usually images) into an off screen bitmap of the correct size and then render that bitmap (quickly) when redrawing a window is required. That way the user does not see the slow rendering of an image to another size, but only see the quick painting of the prepared bitmap. You can also pre-render things in a variety of sizes.

I am going out on a limb here and guess that you probably have a lot of controls and they are all anchored so that they respond properly to sizing of the host window.
If that is the case, I would do the following:
Add a size event handler to the main form.
In that size handler, suspend the layout of all controls that pose a problem. It should not be required to suspend the layout of all controls, just the ones flickering.
Set a timer of a reasonable duration. If another size event hits BEFORE the timer expires, leave the layout suspended and reset the timer.
Only when the timer hits you kill that timer and call ResumeLayout for each suspended control.
This way redrawing problematic controls is delayed until the user stops resizing the window.
You will have to play with the timer duration depending on your form, size, resolution etc, to make it appear it snaps into place just when you stop sizing.

Additionally it is a good practice to make the default size of your form large enough that sizing is generally not required. If your form pops up and the user is compelled to resize it before using it, considers making it bigger to begin with. For multiple resolutions (if you face that) is it usually best to evaluate the true size of the screen and chose a suitable default size for your form in the form load event. As screen sizes get larger you likely want to stop filling the entire screen and stick to a suitable size.

PowerShell Studio does not hide anything. If you export the code as a script, it is all there.
The controls you add all exist as variables by the names you gave them. So they are always accessible within your code.

Hope this helps.
Alexander Riedel
SAPIEN Technologies, Inc.
nikcomp7109
Posts: 13
Last visit: Thu Mar 02, 2023 6:43 am

Re: Doublebuffer form flickering

Post by nikcomp7109 »

Thanks for the repsonse that pointed to several ideas. When you mentioned the options of different resolutions at load time that kind of hit on my issue. Due to so many people at our group using 1440p vs 1080 p monitors its causing the form to be too small or too big. If there was a function that could sense the screen resolution then set the forms size that would definately help reduce the flickering so people don't have to resize. THe form does have a lot of anchored controls as you mentioned and what I notice is some will maximize the form for the smaller 1080p monitor vs the larger 1440p where they shrink it. THis is where a lot of the 'ugly' flickering happens as we have the panel encapsulating the controls centered so they don't end up stuck in a corner.
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Doublebuffer form flickering

Post by Alexander Riedel »

[Topic moved by moderator]

PS> Add-Type -AssemblyName System.Windows.Forms
PS> [System.Windows.Forms.Screen]::AllScreens


BitsPerPixel : 32
Bounds : {X=0,Y=0,Width=1280,Height=800}
DeviceName : \\.\DISPLAY1
Primary : True
WorkingArea : {X=0,Y=0,Width=1280,Height=770}

BitsPerPixel : 32
Bounds : {X=1280,Y=0,Width=1920,Height=1200}
DeviceName : \\.\DISPLAY2
Primary : False
WorkingArea : {X=1280,Y=0,Width=1920,Height=1170}
Alexander Riedel
SAPIEN Technologies, Inc.
This topic is 2 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked