要获取主题名称,您可以调用非托管的 GetCurrentThemeName 方法:
public string GetThemeName()
{
StringBuilder themeNameBuffer = new StringBuilder(260);
var error = GetCurrentThemeName(themeNameBuffer, themeNameBuffer.Capacity, null, 0, null, 0);
if(error!=0) Marshal.ThrowExceptionForHR(error);
return themeNameBuffer.ToString();
}
[DllImport("uxtheme.dll", CharSet=CharSet.Auto)]
public static extern int GetCurrentThemeName(StringBuilder pszThemeFileName, int dwMaxNameChars, StringBuilder pszColorBuff, int dwMaxColorChars, StringBuilder pszSizeBuff, int cchMaxSizeChars);
可以在GAC中右键主题.dll(如PresentationFramework.Aero)找到版本和公钥令牌(在Exporer中打开c:\Windows\Assembly),也可以使用代码来做.只需使用 AppDomain.CurrentDomain.LoadedAssemblies 遍历所有加载的程序集并找到您想要的:
foreach(Assembly a in AppDomain.CurrentDomain.LoadedAssemblies)
if(a.Name.StartsWith("PresentationFramework."))
return a.FullName;
请注意,循环加载的程序集还会告诉您当前的主题名称如果当前 AppDomain 中仅加载了一个主题。