Form flickering problem
-
Hi, My application has a heavy form (meaning with lots of images etc.) and after I minimize it and then maximize it the form flickers for 2-3 seconds. I've tried to set DoubleBuffered property of my form to true and tried all kinds of SetStyle combinations but nothing helped. Is there anyone out there who has any idea how I can solve this problem or workaround it? Thanks in advance.
Hi, please tell us more. 1. what are the Controls on the Form? how many? UserControls? nested UserControls? 2. what is showing the images? are they overlapping? 3. trying some transparency stuff? 4. what code is in the Resize handler? any Controls docked, or anchored on both ends so they stretch? :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
-
Hi, please tell us more. 1. what are the Controls on the Form? how many? UserControls? nested UserControls? 2. what is showing the images? are they overlapping? 3. trying some transparency stuff? 4. what code is in the Resize handler? any Controls docked, or anchored on both ends so they stretch? :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
Hi, My form contains 2-3 panels, over 7 pictureboxes, lots of labels and groupboxes, one progressbar and one button. There are no overlapping images but some images have transparent backgrounds. There is no code in resize handler and my form is not resizeable. Form's FormBorderStyle is set to none.
-
Hi, My form contains 2-3 panels, over 7 pictureboxes, lots of labels and groupboxes, one progressbar and one button. There are no overlapping images but some images have transparent backgrounds. There is no code in resize handler and my form is not resizeable. Form's FormBorderStyle is set to none.
OK, so you don't have list stuff (ListBox, ListView, TreeView, DataGridView), that is good. here are things that help in reducing flicker: - keep the GUI simple (keep the number of Controls below say 50) - make sure you have high-performance code in the paint handlers, if any (don't create too many objects, Pens, Fonts, ...; and if you have those, don't forget to Dispose the ones you created) - use the Graphics from PaintEventArgs, don't use CreateGraphics - all the above should result in fast painting - apply double-buffering, which almost completely hides the painting work These work against you: - PictureBoxes (PB is a stupid Control, I prefer to paint images myself, in a Paint handler) - IIRC: transparancy in general, objects (e.g. Labels) on top of something transparant in particular this normally helps: - make the Form double-buffered by inserting this in its constructor:
SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer,true); SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint,true); SetStyle(System.Windows.Forms.ControlStyles.UserPaint,true);
(if such code is outside constructor it also needs a Control.UpdateStyles) - avoid huge images, even when shown in small size (heavy rescale = CPU cycles); maybe calculate the right sized image once and keep it around. - load the image from disk once (outside all Paint handlers) and keep it as an object. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
modified on Saturday, October 17, 2009 8:16 AM
-
OK, so you don't have list stuff (ListBox, ListView, TreeView, DataGridView), that is good. here are things that help in reducing flicker: - keep the GUI simple (keep the number of Controls below say 50) - make sure you have high-performance code in the paint handlers, if any (don't create too many objects, Pens, Fonts, ...; and if you have those, don't forget to Dispose the ones you created) - use the Graphics from PaintEventArgs, don't use CreateGraphics - all the above should result in fast painting - apply double-buffering, which almost completely hides the painting work These work against you: - PictureBoxes (PB is a stupid Control, I prefer to paint images myself, in a Paint handler) - IIRC: transparancy in general, objects (e.g. Labels) on top of something transparant in particular this normally helps: - make the Form double-buffered by inserting this in its constructor:
SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer,true); SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint,true); SetStyle(System.Windows.Forms.ControlStyles.UserPaint,true);
(if such code is outside constructor it also needs a Control.UpdateStyles) - avoid huge images, even when shown in small size (heavy rescale = CPU cycles); maybe calculate the right sized image once and keep it around. - load the image from disk once (outside all Paint handlers) and keep it as an object. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
modified on Saturday, October 17, 2009 8:16 AM
Thank you for your advices. They really helped me and the form. :D It doesn't have to struggle anymore. ;)
-
Thank you for your advices. They really helped me and the form. :D It doesn't have to struggle anymore. ;)
you're welcome. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
-
OK, so you don't have list stuff (ListBox, ListView, TreeView, DataGridView), that is good. here are things that help in reducing flicker: - keep the GUI simple (keep the number of Controls below say 50) - make sure you have high-performance code in the paint handlers, if any (don't create too many objects, Pens, Fonts, ...; and if you have those, don't forget to Dispose the ones you created) - use the Graphics from PaintEventArgs, don't use CreateGraphics - all the above should result in fast painting - apply double-buffering, which almost completely hides the painting work These work against you: - PictureBoxes (PB is a stupid Control, I prefer to paint images myself, in a Paint handler) - IIRC: transparancy in general, objects (e.g. Labels) on top of something transparant in particular this normally helps: - make the Form double-buffered by inserting this in its constructor:
SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer,true); SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint,true); SetStyle(System.Windows.Forms.ControlStyles.UserPaint,true);
(if such code is outside constructor it also needs a Control.UpdateStyles) - avoid huge images, even when shown in small size (heavy rescale = CPU cycles); maybe calculate the right sized image once and keep it around. - load the image from disk once (outside all Paint handlers) and keep it as an object. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
modified on Saturday, October 17, 2009 8:16 AM
-
Luc Pattyn wrote:
if you have those, don't forget to Dispose the ones you created
Hi, What if my app has values need to be passed? does
this.Dispose()
going to erase them? SunHi, IMO
this.Dispose()
does not make any sense, an object cannot dispose itself, as it would have to be alive to execute the next line of code. This is what I meant:public void Paint(object sender, PaintEventArgs e) {
Graphics g=e.Graphics;
Font myFont=new Font(...); <<<<<<<<<<<<<<<<<<<<<<<
g.DrawString(..., myFont, ...);
...
myFont.Dispose(); <<<<<<<<<<<<<<<<<<<<<<<
}:)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
-
Hi, IMO
this.Dispose()
does not make any sense, an object cannot dispose itself, as it would have to be alive to execute the next line of code. This is what I meant:public void Paint(object sender, PaintEventArgs e) {
Graphics g=e.Graphics;
Font myFont=new Font(...); <<<<<<<<<<<<<<<<<<<<<<<
g.DrawString(..., myFont, ...);
...
myFont.Dispose(); <<<<<<<<<<<<<<<<<<<<<<<
}:)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
Luc Pattyn wrote:
IMO this.Dispose() does not make any sense,
Hi, Sorry if I said it wrong, but when I mentioned
this.Dispose()
,this
= the main form. But thank you, Luc, I got your point. Again, I have another question, what if I set a new Font for one control, for example, btn_OK, am I doing it rightly by the code below:btn_OK.Font = new Font(....);
...
btn_OK.Font.Dispose();Thanks in advance!;) Sun
-
Luc Pattyn wrote:
IMO this.Dispose() does not make any sense,
Hi, Sorry if I said it wrong, but when I mentioned
this.Dispose()
,this
= the main form. But thank you, Luc, I got your point. Again, I have another question, what if I set a new Font for one control, for example, btn_OK, am I doing it rightly by the code below:btn_OK.Font = new Font(....);
...
btn_OK.Font.Dispose();Thanks in advance!;) Sun
Leapsword Sun wrote:
btn_OK.Font = new Font(....); ... btn_OK.Font.Dispose();
assuming all that is code in a single method, no it does not make sense: assuming the button is visible on some form, the font is in use, so you shouldn't dispose of it at that time. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
-
Leapsword Sun wrote:
btn_OK.Font = new Font(....); ... btn_OK.Font.Dispose();
assuming all that is code in a single method, no it does not make sense: assuming the button is visible on some form, the font is in use, so you shouldn't dispose of it at that time. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!