Sind VisualStyles aktiviert?
Frank Dzaebel, erstellt am: 28.1.2006, zuletzt geändert:
28.08.2007
Kategorie: Implementation, .NET-Version: 1.1/2.0
Unter .NET 2.0 schon einfach mit Application.RenderWithVisualStyles abfragbar, so fehlt eine solche Methode unter .NET 1.1: "IsVisualStylesEnabled". Hier eine mögliche Implementation über Windows-API. Der Code wurde unter Zuhilfenahme mehrerer Internet-Artikel erstellt.
Erste Möglichkeit: über Registry abfragbar:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\ThemeManager\
ThemeActive : 0 => klassischer Desktop ohne
VisualStyles
ThemeActive : 1 => neuer Desktop mit VisualStyles
Andere Möglichkeit: über Kommandozeile:
rundll32 shell32.dll,Control_RunDLL
desk.cpl,,2
[StructLayout(LayoutKind.Sequential)]
private struct DLLVERSIONINFO
{
public int cbSize;
public int dwMajorVersion;
public int dwMinorVersion;
public int dwBuildNumber;
public int dwPlatformID;
}
[DllImport("comctl32")]
private static extern int DllGetVersion (
ref DLLVERSIONINFO pdvi);
[DllImport("kernel32")]
private static extern IntPtr LoadLibrary (
[MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);
[DllImport("kernel32", SetLastError=true)]
private static extern IntPtr GetProcAddress(IntPtr hModule,
[MarshalAs(UnmanagedType.LPStr)] string lpProcName);
[DllImport("kernel32", SetLastError=true)]
private static extern IntPtr FreeLibrary(IntPtr hLibModule);
[DllImport("uxtheme")]
private static extern bool IsAppThemed();
[DllImport("uxtheme")]
private static extern bool IsThemeActive();
public static bool IsVisualStylesEnabled()
{
bool retVal = false;
if (IsThemesSupported())
{
IntPtr uxTheme = LoadLibrary("uxtheme.dll");
if (uxTheme != IntPtr.Zero)
{
IntPtr handle = GetProcAddress(uxTheme, "IsThemeActive");
if (handle == IntPtr.Zero)
{
MessageBox.Show(new System.ComponentModel.Win32Exception(
Marshal.GetLastWin32Error()).Message);
}
else
{
if (IsThemeActive() && IsAppThemed())
{
DLLVERSIONINFO version = new DLLVERSIONINFO();
version.cbSize = Marshal.SizeOf(version);
if (DllGetVersion(ref version) == 0)
{
if (version.dwMajorVersion > 5) retVal = true;
}
}
}
}
FreeLibrary(uxTheme);
}
return retVal;
}
public static bool IsThemesSupported()
{
OperatingSystem os = System.Environment.OSVersion;
return os.Platform == PlatformID.Win32NT &&
(((os.Version.Major == 5) && (os.Version.Minor >= 1)) ||
(os.Version.Major > 5));
}