Το Application.EnableVisualStyles() πρέπει να ακολουθείται κατ' ευθείαν από ένα Application.DoEvents(), αλλιώς μπορούν να παρουσιαστούν πολλά προβλήματα. Περισσότερα μπορείς να βρείς και στο blog του Raghavendra Prabhu:
Q I sometimes have weird problems with Application.EnableVisualStyles. Some controls don't seem to pick up theming; images that are assigned to TreeView nodes or ListView items sometimes don't render at all. However, when I use a manifest, I don't see these problems.
Yes, this is a known issue with the implementation of Application.EnableVisualStyles. The reason for this is as follows. As I mentioned before, the activation context is setup around the application's message loop. So any comctl32 handles created after the message loop is setup will be correctly redirected to v6.0. However, when you have the following code in Main:
Application.EnableVisualStyles();
Application.Run(new Form1());
Form1 is constructed before the message loop is setup. Therefore, any handle created in Form1's constructor code will result in that control being bound to v5.8 instead of v6.0, since the activation context has not yet been setup. This isn't as bad as it may sound, since in most cases, we delay handle creation as much as possible, so control handles aren't typically created in InitializeComponent. One rather common exception is the Imagelist - it turns out that in many cases, the Imagelist's handle is created in InitializeComponent, so you get a v5.8 imagelist that doesn't interop well with v6.0 controls on XP (on Windows Server 2003, the v5.8 imagelist does interop well with v6.0 controls, so you won't see this problem on that platform), the result being that the images don't show up. There are also some cases where some other control handles may get created early, in which case, those controls too will get bound to v5.8 and will not appear themed. We are obviously planning to fix this in the future, but for now, there are several workarounds that customers have used successfully. I list some of them here for reference:
- Include a call to Application.DoEvents() before Application.Run().
- Move the erring code (that causes handle creation to happen) from InitializeComponent to the Form's Load method.
- Use a manifest!
Note that the above discussion applies only to the app's "Main Form", i.e., the Form that is passed to the Application.Run. Forms that are instantiated after the message loop is setup will be constructed after the context is activated, so code in their constructors shouldn't face this problem.
Χρήστος Γεωργακόπουλος