2

I have only put several controls into my main form in C# and I see that each control is drawn very slowly when the form loads. This is very noticeable to user and it's bothering me. I am not creating any controls on run-time, they are inside the form already. I have seen some related questions and found the following suggestion:

this.SetStyle(ControlStyles.UserPaint, true);

this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);

this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

this.SetStyle(ControlStyles.ResizeRedraw, true);

this.UpdateStyles();

This solution actually causes my form to load even slower. I can't imagine what would happen to the form if it were to contain more than 30 controls.

Any suggestions on what I should do?

LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
Mert
  • 21
  • 1
  • 2
  • Is it specifically this application? Have you create a blank application with one control and it still does the same thing? I'm trying to see whether your application is doing something you're not aware of, or probably there is an issue with your display drivers – BeemerGuy Nov 18 '10 at 12:10
  • 1
    What controls are in the form? – Guy Nov 18 '10 at 12:10
  • What else is happening when your Form loads? Is it doing anything resource-intensive? More information would help. – Jazza Nov 18 '10 at 12:11
  • What are the specs of the machine? – Bobby Nov 18 '10 at 12:11
  • 1
    .Net Windows Forms applications have very poor performance when you use controls that have images with transparency used for their backgrounds. Is this your case? – Florin Dumitrescu Nov 18 '10 at 12:13
  • possible duplicate of [How to fix the flickering in User controls.](http://stackoverflow.com/questions/2612487/how-to-fix-the-flickering-in-user-controls) – Hans Passant Nov 18 '10 at 13:08

5 Answers5

1

You need to provide more info.

If you use our-of-the box visual studio controls and you have 5 of them and are experiencing such issues, then the problem MUST be somewhere else - bad graphic driver or something alike.

So, what controls do you use? What specifically do you set on them? If you set some of the properties, try to reset them to default and see if something changes.

Flags you set to the form AREN'T important for controls that aren't owner-drawn, and you didn't say that is the case.

Daniel Mošmondor
  • 19,718
  • 12
  • 58
  • 99
  • They are out of box visual studio controls but their background color is set to transparency. My graphic driver is up-to-date and works really great. I believe this issue have to do with controls transparency settings since I have not really played much with their properties. – Mert Nov 18 '10 at 19:49
  • OK, did you try to turn off the transparency and try that? Please try that and get back... – Daniel Mošmondor Nov 18 '10 at 21:10
1

Not sure this is related to your problem, because the lack of info, but I ran into the same problem at some point and I will provide the details.

More than 5 years ago I worked on a Windows Forms kiosk application, which was built on .Net 1.1 (the latest at the time). The application needed fancy graphics which consisted in a background image for the main form and images with transparency layers for the different controls on the form. Implementing that in the standard manner, by using the BackgroundImage property or the OnPaint event in the controls, resulted in a slow interface. Flickering was present and in the cases where a lot of controls were added to the form you could actually see them being loaded one by one.

The problem with controls that involve transparency is that they involve a different paint model compared with the ones that don't. Such a control, when receiving a paint event would have to forward that event to its parent, which would be rendered first, and only after that the child's graphics would be rendered. That should mean one paint event translated into three (paint event to control with transparency, then paint event forwarded to parent, then paint event back to control with transparency), but for some strange reason, it seems that in .Net's Windows Forms there are more such paint events passed back and forth from children to parents. Don't know what is the reason for this. The one thing I know is that VB6 or C++ MFC applications, that as the .Net Windows Forms ones, are also built on top of Win32, do not have the problem.

At the time, I tried a great amount of tricks from the internet (like the ones you are quoting), but none of them really worked. I ended up building my own rendering engine on top of GDI+. That is not something that I would recommend, since it took me a great amount of time to implement and in the process I lost functionalities like the visual forms editor.

What I would recommend though, considering the technologies that appeared since then, is that if you need to build a graphics intensive application, you would better do it with Windows Presentation Foundation (WPF), which has been optimized for this, rather than with Windows Forms.

Florin Dumitrescu
  • 8,182
  • 4
  • 33
  • 29
  • 1
    I apologize for not providing enough information but it sounds very similar to what Florin has explained. My form has a 1024 * 768 background image and on top of it I have 15 labels that have background color set to transparency and 3 picture-boxes that also have their background color set to transparency. I also have a user control which consists of one listview and one treeview. When this form loads the controls are drawn one by one, very slowly. I think it's a simple Windows form application but it may be related to transparency settings. I have already coded a lot so I can't switch to WPF. – Mert Nov 18 '10 at 19:43
0

I realized that backgroundImageLayout was set to "tile" and I switched to "stretch" which has significantly improved GUI performance. I've also changed some controls' background color to white and this helped a lot especially with panels that were hidden in my form.

Thanks to everyone.

Mert
  • 21
  • 1
  • 2
0

If you use the paint event to draw the background image on the form, it can really slow things down. Instead, draw the background image once at form load or in design mode, and let take everything out of the paint event handler.

xpda
  • 15,585
  • 8
  • 51
  • 82
0

Try to put a Image on top of the form, with height and width and draw on it. It will refresh and draw faster.

radu florescu
  • 4,315
  • 10
  • 60
  • 92