Does Application.EnableVisualStyles() do anything?
I'm very picky when it comes to understanding a new language, and recently I've taken up learning C#. So I like to know everything that is going on when I create a new Application - in this case a new Windows Forms Application. I created one and was开发者_运维知识库 given some generated code from Visual Studio and one line was Application.EnableVisualStyles()
.
I did some research on MSDN and found this article: Application.EnableVisualStyles(). I performed the example that they presented expecting the button not to be visible when I commented out the said line. Nothing happened! It appeared that nothing changed. I know this is a very basic example but shouldn't something have changed? If this is so critical in the Main() procedure what exactly is it doing that I'm missing?
Yes it does, but you need have certain settings turned on in your OS otherwise you won't be able to see what it is.
Here are some screenshots of that same application on my system:
Application.EnableVisualStyles();
// Application.EnableVisualStyles();
If buttons in other applications look like the second screenshot then you will always see buttons un-themed regardless of whether you use Application.EnableVisualStyles()
, because they are turned off by the OS - the way to turn them back on depends on your OS but usually involves going to the Performance >> Visual Effects dialog.
It turns on the more creative stock drawings of scrollbars and buttons and such. If false, you get the plain gray stock controls, etc.
EnableVisualStyles()
doesn't mean that the buttons are now visually drawn when that method is run, it means that it will use the built-in Windows theming to style controls instead of the "classic Windows" look and feel. If your computer is running without a style, then you wouldn't notice any difference.
This answer may be interesting to you as well: Should I call Application.EnableVisualStyles() on terminal services?
This is what I got from Microsoft Support in response to an obscure application crash inquiry:
The gist is that The crash is a known bug in the version 5.0 of comctl32.dll (Windows Common Controls), which ships with the Windows OS. This bug will not be fixed in version 5.0 of the common controls, because that version was for applications existing prior to Windows XP. It has since been fixed in version 6.0 of comctl32.dll, which is included with Windows XP and later. Note that both versions of comctl32.dll (5.0 and 6.0) are included with every version of Windows since Windows XP. The older one is just there for backwards compatibility purposes for very old applications.
To resolve the problem, you need to change the application to have it opt into version 6.0 of comctl32.dll. Within a Windows Forms application, this is done by calling into the Application.EnableVisualStyles method at startup of the application. If you are developing within a C# project, then you can do this by adding the call prior to your Application.Run call within your application's entry point. For example:
[STAThread]
static void Main()
{
Application.EnableVisualStyles(); //Add this line
Application.Run(new Form1());
}
If you are in a Visual Basic .Net project, you can opt into this by going to your project properties, and then selecting the "Enable Application Framework" and "Enable XP Visual Styles" checkboxes on the Application property page.
精彩评论