Tidbit on VisualStyles in Windows Forms

I wanted to implement theme support for my MessageBox component mentioned in the previous post, I came acorss this excellent post on how Application.EnableVisualStyles() works. So basically it uses the Activation Context API to specify that the v6.0 of common controls should be used.

When I looked at the API, I found that you have to specify a manifest file to specify that information. Now where does that manifest file come from? So I fired up reflector and this is what I found. Below is the code for the method that creates the ActivationContext for your application.



private bool EnsureActivateContextCreated()
{
bool flag1;
lock (typeof(SafeNativeMethods.EnableThemingInScope))
{
if (!SafeNativeMethods.EnableThemingInScope.contextCreationSucceeded)
{
string text1 =
null;
FileIOPermission permission1 =
new FileIOPermission(PermissionState.None);
permission1.AllFiles = FileIOPermissionAccess.PathDiscovery;
permission1.Assert();
try
{
text1 =
typeof(object).Assembly.Location;
}
finally
{
CodeAccessPermission.RevertAssert();
}
string text2 =
null;
string text3 =
null;
if (text1 != null)
{
text3 = Path.GetDirectoryName(text1);
text2 = Path.Combine(text3,
"XPThemes.manifest");
}
if ((text2 != null) && (text3 != null))
{
SafeNativeMethods.EnableThemingInScope.enableThemingActivationContext =
new SafeNativeMethods.EnableThemingInScope.ACTCTX();
SafeNativeMethods.EnableThemingInScope.enableThemingActivationContext.cbSize = Marshal.SizeOf(
typeof(SafeNativeMethods.EnableThemingInScope.ACTCTX));
SafeNativeMethods.EnableThemingInScope.enableThemingActivationContext.lpSource = text2;
SafeNativeMethods.EnableThemingInScope.enableThemingActivationContext.lpAssemblyDirectory = text3;
SafeNativeMethods.EnableThemingInScope.enableThemingActivationContext.dwFlags =
4;
SafeNativeMethods.EnableThemingInScope.hActCtx = SafeNativeMethods.EnableThemingInScope.CreateActCtx(
ref SafeNativeMethods.EnableThemingInScope.enableThemingActivationContext);
SafeNativeMethods.EnableThemingInScope.contextCreationSucceeded = SafeNativeMethods.EnableThemingInScope.hActCtx !=
new IntPtr(-1);
}
}
flag1 = SafeNativeMethods.EnableThemingInScope.contextCreationSucceeded;
}
return flag1;
}

So the manifest file is called XPThemes.manifest, the intresting thing was how the code determines the directory in which to find the file, the relevant line of code is

text1 = typeof(object).Assembly.Location;

Since object is defined in mscorlib.dll and that is present in the directory where .Net is installed
the code can get the location where it can find XPThemes.manifest

If you search for XPthemes.manifest it will be present in the folder where you installed the .Net runtime.





No Comments