Every C# Winform project has this line of code in its Main method, so what does it do?

 

Application.EnableVisualStyles();
Copy the code

 

From the comments, this is a line of code that sets styles, so what styles does it set?

Source analyses

public static void EnableVisualStyles() {
            string assemblyLoc = null;
 
            // SECREVIEW : This Assert is ok, getting the module path is a safe operation, 
            //             the result is provided by the system.
            //
            FileIOPermission fiop = new FileIOPermission(PermissionState.None);
            fiop.AllFiles = FileIOPermissionAccess.PathDiscovery;
            fiop.Assert();
            try {
                assemblyLoc = typeof(Application).Assembly.Location;
            }
            finally {
                CodeAccessPermission.RevertAssert();
            }
            // Pull manifest from our resources
            if (assemblyLoc != null) {
                EnableVisualStylesInternal(assemblyLoc, 101);
            }
        }
Copy the code

From the point of view of the code, the most important is the last sentence

EnableVisualStylesInternal(assemblyLoc, 101);
Copy the code

In this code, you pass in the system.windows.forms. DLL path and a “101” from the local GAC_MSIL folder to set the so-called style.

When we enter the CreateActivationContext method, we see the following code:

[ResourceExposure(ResourceScope.None)] [ResourceConsumption(ResourceScope.Process, ResourceScope.Process)] public static bool CreateActivationContext(string dllPath, int nativeResourceManifestID) { lock (typeof(ThemingScope)) { if (! contextCreationSucceeded && OSFeature.Feature.IsPresent(OSFeature.Themes)) { enableThemingActivationContext = new ACTCTX(); enableThemingActivationContext.cbSize = Marshal.SizeOf(typeof(ACTCTX)); enableThemingActivationContext.lpSource = dllPath; enableThemingActivationContext.lpResourceName = (IntPtr)nativeResourceManifestID; enableThemingActivationContext.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID; hActCtx = CreateActCtx(ref enableThemingActivationContext); contextCreationSucceeded = (hActCtx ! = new IntPtr(-1)); } return contextCreationSucceeded; }}Copy the code

After setting the ACTCTX property, it is passed into the CreateActCtx method, which is used to create the ActivationContext.

That “101” is assigned to lpResourceName, so if we want to understand what “101” does, we need to understand what lpResourceName means.

The answer can be found in Microsoft’s official documentation:

ACTCTXA structure

LpResourceName refers to a resource in the same directory as a DLL or exe.

EnableVisualStyles sets application information, such as DLL references, registration paths, version numbers, etc. The EnableVisualStyles method does not set the manifest file.

This is the contents of the manifest file.

<? XML version="1.0" encoding="UTF-8" standalone="yes"? > <assembly XMLNS ="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <description>Windows Forms Common Control manifest</description> <dependency> <dependentAssembly> <assemblyIdentity type="win32" Name = "HTTP: / / Microsoft.Windows.Com mon - Controls" version = "6.0.0.0" processorArchitecture = "*" publicKeyToken = "6595 b64144ccf1df" language="*" /> </dependentAssembly> </dependency> </assembly>Copy the code

From here we can see that this maps to a folder named “Microsoft.Windows.Common-Controls” and sure enough I found it in C:\Windows\WinSxS.

Here you can see that the version number is the same, so what are the things that are recorded in it?

I’m going to pick one at random

Saw here we knew, the comctl32. DLL are some commonly used ICONS and such record inside resource file, and the Application. The EnableVisualStyles (); That’s what I set up.