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

[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

 

 

<FONT color=#008000>' Updated version of above function - see here for more details.</FONT>

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.

This post is licensed under CC BY 4.0 by the author.