Welcome to AddressOf.com Sign in | Join | Help

[How To] Check to see if Visual Styles are enabled

After my rant and a some other people picking it up on their blog, I got a response from Raghavendra Prabhu who is part of the .NET Client Team letting us know that VisualStyles in Whidbey has a ton of improvements and resolves the issues I point out and pointed out that using EnableVisualStyles, although has bugs, can be used as long as you do a Application.DoEvents prior to Application.Run().  For more information, please read his entry on the subject (it's a great read if your doing anything with visual styles in your application)..  In the comments of my rant, he suggested that I use GetDllVersion API in addition to the methods I'm already using to find out if visual styles are enabled.  After doing some more digging in API land, I've modified the previous code so that you can reuse it to determine whether the application you are running is using visual styles or not.  Thanks to everyone that provided feedback on this to get a solution.  Here's the code:

Private Structure DLLVERSIONINFO

  Public cbSize As Integer

  Public dwMajorVersion As Integer

  Public dwMinorVersion As Integer

  Public dwBuildNumber As Integer

  Public dwPlatformID As Integer

End Structure

 

Private Declare Function DllGetVersion Lib "comctl32.dll" (ByRef version As DLLVERSIONINFO) As Integer

 

Private Declare Function LoadLibrary Lib "kernel32.dll" Alias "LoadLibraryA" (ByVal path As String) As IntPtr

Private Declare Function GetProcAddress Lib "kernel32.dll" (ByVal library As IntPtr, ByVal procName As String) As IntPtr

Private Declare Function FreeLibrary Lib "kernel32.dll" (ByVal library As IntPtr) As Boolean

 

Private Declare Function IsThemeActive Lib "uxtheme.dll" () As Boolean

Private Declare Function IsAppThemed Lib "uxtheme.dll" () As Boolean

 

Private Function IsVisualStylesEnabled() As Boolean

 

  Dim os As OperatingSystem = System.Environment.OSVersion

 

  If os.Platform = PlatformID.Win32NT AndAlso (((os.Version.Major = 5) And (os.Version.Minor >= 1)) Or (os.Version.Major > 5)) Then

    Dim uxTheme As IntPtr = LoadLibrary("uxtheme.dll")

    If Not uxTheme.Equals(IntPtr.Zero) Then

      Dim handle As IntPtr = GetProcAddress(uxTheme, "IsThemeActive")

      If handle.Equals(IntPtr.Zero) Then

        ' an error occured, use GetLastError

      Else

        If IsThemeActive() AndAlso IsAppThemed Then

          Dim version As DLLVERSIONINFO

          version.cbSize = Len(version)

          If DllGetVersion(version) = 0 Then

            If version.dwMajorVersion > 5 Then

              Return True

            End If

          End If

        End If

      End If

    Else

      ' an error occured, use GetLastError

    End If

    FreeLibrary(uxTheme)

  End If


  Return False

 

End Function

 

 

' Updated version of above function - see here for more details.

Private Function IsVisualStylesEnabledRevisited() As Boolean

  Dim os As OperatingSystem = System.Environment.OSVersion

  If os.Platform = PlatformID.Win32NT AndAlso (((os.Version.Major = 5) And (os.Version.Minor >= 1)) Or (os.Version.Major > 5)) Then

    Dim version As New DLLVERSIONINFO

    version.cbSize = Len(version)

    If DllGetVersion(version) = 0 Then

      Return (version.dwMajorVersion > 5) AndAlso IsThemeActive() AndAlso IsAppThemed()

    End If

  End If

  Return False

End Function

Man, what a lot of work just to find out what should be a simple answer to an extremely simple question.  Now, if you are writing visual components and want to determine if you need to render one way or another depending on the usage of visual styles, now you can determine whether you are or not ;-)

[updated] Added a new version of the IsVisualStylesEnabled method based on additional information and testing.

Published Sunday, February 15, 2004 4:54 PM by CorySmith
Filed under: , ,

Comments

# How to determine if visual styles are enabled?

Sunday, February 15, 2004 9:31 PM by Cool Client Stuff

# How to determine if visual styles are enabled?

Sunday, February 15, 2004 9:40 PM by Cool Client Stuff

# re: [How To] Check to see if Visual Styles are enabled

Saturday, March 06, 2004 9:52 PM by Justin
Thanks for the code example, but this doesn't work. I've converted the code to C# and when I enable visual styles, DllGetVersion always returns version 5 for ComCtl32.dll (which is the version I have in System32).

I'm calling EnableVisualStyles in code, I haven't tried this with a manifest file yet.

# re: [How To] Check to see if Visual Styles are enabled

Sunday, March 07, 2004 3:31 PM by Cory Smith
Erik Porter (http://weblogs.asp.net/eporter) was kind enough to validate the code after I received this comment. It's definitely working as advertised. Of course, he tested the VB.NET code by cut-n-pasting it into a new project and changing the startup to use Sub Main with the following:

Sub Main()
Application.EnableVisualStyles()
Application.DoEvents()
Application.Run(New Form1)
End Sub

Removing the EnableVisualStyles makes the code return false, adding it back in, it returns true.

I suspect that there's something possibly amiss with your VB.NET->C# conversion.

# VS-Like TreeView Control and updates

Saturday, September 25, 2004 12:10 PM by HumanCompiler - Erik Porter (MS MVP) Blog

# VS-Like TreeView Control and updates

Saturday, September 25, 2004 12:48 PM by HumanCompiler - Erik Porter (MS MVP) Blog
Anonymous comments are disabled