Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok so now it is a flickering problem with changing controls Size and Location while MainForm is resizeing. Every time user changes the Size of the main form all controls are dramaticaly flickering. Activating double buffering and some flags of "SetStyle" remove flickering only while Cursor is moving, but not while Resizeing the form. Is there a way to change dinamicaly Location and Size of my controls and avoid there flickering?
Posted
Comments
CPallini 8-May-13 5:19am    
What is the event you are handling?
Alexey Gapon 8-May-13 5:25am    
MainForm.OnSizeChange()
Mayur Panchal 8-May-13 6:23am    
Can you show a bit of code of OnSizeChange() event ?
Are you using form.Refresh() method in above event ?
Alexey Gapon 8-May-13 6:53am    
It wount help because i am writing an advanced image editor. And when the main form change it`s size I call "UpDate()" function for all custom controls Panels, Pictureboxes, TabControls, Animation sliers, Rulers and so on. And in the body of the calss i calculate the location and size then just "CustomControl.Size = new Size(x, y) and CustomControl.Location = new Point (x,y)". And while i am resizing my main form all controls "jumps" and flickering. I am using Refresh() after calculating size and location. It looked like the computer calculating and drawing controls slower then the Cursor moves. It is strange there no complex calculatings. All calculatin looks like "If (something) {control.size = new Size ( parent.Size.width-10, parent.Size.height - 10} else {{control.size = new Size ( parent.Size.width + 10, parent.Size.height + 10}}" i mean no complex calculation.In many articles other people suggest enable flags such as DoubleBufered but it dosen help with custom changeing Location and Size of the controls.

check you have done some paint event that is conflicting or adding some dynamic control and which you are adjusting runtime on load event or in constructor...

if you want to not show flicker
means below code will show your form after it complete painting

put this property in your form and check effect
C#
protected override CreateParams CreateParams 
{
    get {
        CreateParams cp = base.CreateParams;
        cp.ExStyle = cp.ExStyle | 0x2000000;
        return cp;
    }
}

Happy Coding!
:)
 
Share this answer
 
Comments
Alexey Gapon 8-May-13 8:55am    
Oh no :)) I already trieed it! It slows down my form twice. And i can see each repaint in each control step by step)))
Alexey Gapon 8-May-13 9:00am    
Listen... all my controls are on tabControls! I did`t find any DoubleBuffered option for tabControls non for tabPage... so that is why all my controls are blinking and flickering and jumping... everything is double bufered but TabConrtols - not. Is there a solution solve this out?
Alexey Gapon 8-May-13 9:19am    
Well something strange my TabControls dose not have "DoubleBuffered" propertie, and i can`t set it it true, i think that is why it is flickering... I ma Using Microsoft Visual Studio Express 2010
Aarti Meswania 8-May-13 9:24am    
only form have this property.
Alexey Gapon 8-May-13 9:26am    
http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.doublebuffered(v=vs.100).aspx

Then what is this article about?
Try setting the Main form's DoubleBuffered property to True. That should help reduce flickering.

[EDIT] Sorry, didn't read your question carefully enough, clearly you've already tried that. I don't understand why it didn't work, though - it should reduce flickering even when resizing the form... [/EDIT]
 
Share this answer
 
v2
Comments
Alexey Gapon 8-May-13 8:44am    
I already did it. I allways set this property to true.
Johnny J. 8-May-13 8:53am    
Try testing this:

protected override void OnResizeBegin(EventArgs e) {
SuspendLayout();
base.OnResizeBegin(e);
}
protected override void OnResizeEnd(EventArgs e) {
ResumeLayout();
base.OnResizeEnd(e);
}

Do you have any custom painted controls on the form?
Alexey Gapon 8-May-13 9:06am    
I tried Suspend and Resum Layout it looks not very good. The best idea is:
1) Create Bitmap from paret area of the main form
2) hide all controls
3) put this Bitmap as background image
4) resize the form
5) set back ground image to bull
6) show all controls.

Is there a way to create a printscreen bitmap from parent area of main form?
Johnny J. 8-May-13 9:09am    
Yes, I've done that before. Don't recall precisely how, it was many years ago. But it should be easy to Google that. I'm just worried that the print screen will take too long and make the resize slow.

Also, do you propose to resize the image along with the form? If so, then you will probably still get the flickering...
Johnny J. 8-May-13 9:10am    
BTW: Why is the post marked as [SOLVED]? You haven't solved it yet, have you?
Quote:
Hello to every One.
So this solved me task. I repleced all standard controls with modified custom controls based on standard controls.

Here is how i did it. If you have better solution please let me know!

1) Create what ever you want with Standard Microsoft Visual Studio Constructor.

2) Create custom controls based on standard controls.
C#
namespace Project_Name
{
    public class CustomPanel : Panel
    {
        private void CustomPanel_Resize(object sender, EventArgs e)
        {
            if (this.Visible) this.Refresh();

        }


        public CustomPanel()
        {
            this.Resize +=new EventHandler(CustomPanel_Resize);
            this.SetStyle(ControlStyles.UserPaint |
                          ControlStyles.AllPaintingInWmPaint |
                          ControlStyles.OptimizedDoubleBuffer,
                          true);
        }

    }


    public class CustomTabControl : TabControl
    {

        private void CustomTabControl_Resize(object sender, EventArgs e)
        {
           if (this.Visible) this.Refresh();
        }

        public CustomTabControl()
        {
            this.Resize +=new EventHandler(CustomTabControl_Resize);
            this.SetStyle(ControlStyles.UserPaint |
                                    ControlStyles.AllPaintingInWmPaint |
                                    ControlStyles.OptimizedDoubleBuffer,
                                    true);
        }
    }


    public class CustomButton : Button
    {
         private void CustomButton_Resize(object sender, EventArgs e)
        {
           if (this.Visible) this.Refresh();
        }


         public CustomButton()
        {
            this.Resize += new EventHandler(CustomButton_Resize);
            this.SetStyle(ControlStyles.UserPaint |
                                    ControlStyles.AllPaintingInWmPaint |
                                    ControlStyles.OptimizedDoubleBuffer,
                                    true);
        }

    }
}



NOTE (1): i created custom controls for all standard controls i used in the project (pictureboxes, checkboxes, menu stripes and so on).
NOTE (2): I need to dynamically create controls. In this case, I always use the this custom controls.

3)In every Form you can find "Form Aame".Designer.cs. Copy this file and rename.
FOR EXALE: You are working with the from named "MainForm".
Then the file name of it`s desinger will have a name "MainForm.Desirgen.cs".
In explorer copy this file and rename it "Custom_MainForm.Designer.cs".

4) Open this new renamed file in your studio. And delete
C#
private System.ComponentModel.IContainer components = null;


and

C#
protected override void Dispose(bool disposing)
       {
           if (disposing && (components != null))
           {
               components.Dispose();
           }
           base.Dispose(disposing);
       }


5) Rename this function

C#
private void InitializeComponent()


with some other name i used

C#
private void Initialize_Custom_Component()



6) Using "Ctrl+F -> Repleace " repleace all like this:
For example:

C#
            this.panel = new System.Windows.Forms.Panel();
            this.button = new System.Windows.Forms.Button();
            this.groupBox = new System.Windows.Forms.GroupBox();
            this.label = new System.Windows.Forms.Label();
            this.radioButton1 = new System.Windows.Forms.RadioButton();
            this.textBox = new System.Windows.Forms.TextBox();
//
//
//
        private System.Windows.Forms.Panel panel ;
        private System.Windows.Forms.Button button ;
        private System.Windows.Forms.GroupBox groupBox ;
        private System.Windows.Forms.Label label ;
        private System.Windows.Forms.RadioButton radioButton1 ;
        private System.Windows.Forms.TextBox textBox ;


into this

C#
             this.panel = new CustomPanel();
            this.button = new CustomButton();
            this.groupBox = new CustomGroupBox();
            this.label = new CustomLabel();
            this.radioButton1 = new CustomRadioButton();
            this.textBox = new CustomTextBox();
//
//
//
        private CustomPanel panel ;
        private CustomButton button ;
        private CustomGroupBox groupBox ;
        private CustomLabel label ;
        private CustomRadioButton radioButton1 ;
        private CustomTextBoxtextBox ;



7) Go to the constructor of your form and rename
C#
InitializeComponent()


into

C#
Initialize_Custom_Component()


If you all did correct your Controls will never blink again.
But be carefull with resizing your form this may decrease your performance.
So i created a checkbox in my global project properties form and named it - "Use Performance".
If this chekbox is checked iam using standard controls if not i am using my custom controls.

And the final code of the initialization looks like

C#
if (GLOBAL_PROPERTIES.Use_Performance_CheckBox_Checked) //public bool in static class
    InitializeComponent();
else
 Initialize_Custom_Component();


I have 521 controls on my form, and i have 6-7 resizings in second without blinking. I think it is good resault.

I hope this answer will be usefull for somebody.

Happy Coding for all you, my friends.
 
Share this answer
 
Comments
Member 14548491 9-Nov-19 22:33pm    
tf

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900