Welcome to AddressOf.com Sign in | Join | Help

IsVisualStylesEnabled Revisited

Erik Porter and I had some discussion via IM concerning his latest entry (where he gave me credit).  I noticed that he wasn't using LoadLibrary and GetProcAddress when checking whether Visual Styles were enabled.  I was sure that it was needed so that you would be sure that you were referencing the correct DLL that was being loaded in memory, but it turns out this was completely unnecessary and was purely a product of me converting code from C/C++ and Delphi.  One side effect though to doing it this way was that you could verify that that method was indeed within that DLL in the event that Microsoft decided to pull it or change it's name in a future version of Windows.  I don't really think that would happen, but figured I'd throw it in there nonetheless.  So, after some experimentation and verifying both versions of the code... here is an updated method that replaces the previous one (be sure to reference the previous code for all the additional supporting code).

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

Just remove the 'Revisited' portion of the method name.  This significantly reduces the amount of code needed and is a little more readable.  In addition, the number of P/Invoke calls is significantly reduced.  If VisualStyles is currently not available, only one call is necessary (DLLGetVersion).  The second two (IsThemeActive and IsAppThemed) are only called if the version of the comctl32.dll is greater than 5 (version 6 or greater).

On interesting point that someone brought up on Erik's blog comments is when checking for the platform, his code wasn't future safe as for the version information.  He's since updated his code appropriately, however, he pointed out that it's probably not 'future safe' when checking on a Windows 64 bit platform.  I, unfortunately, have no way to verify whether or not the 64 bit version of Windows in addition to using the .NET Framework would still report as being Win32NT.  I did do a bit of checking though and with the Beta 1 bits of Visual Basic Express Edition, there is no addition of 64 bit enumators for Platform; so still not sure.  However, there was a surprise enum addition... Unix!?!?!?  I guess that Microsoft is playing nice in regards to Novell/Mono and other similar projects ;-)

Published Monday, September 27, 2004 2:47 PM by CorySmith
Filed under: , ,

Comments

# re: IsVisualStylesEnabled Revisited

Wednesday, February 02, 2005 9:03 PM by Mo
Hi,
I must make a VB.NET application for my college.
The college PC's OS is XP but it's theme is classic, I have a manifest but it doesn't work because XP theme isn`t enabled ni the PC.
I have .NET 1.0.
I don't want to have "luna", "olive" or other XP style, I want just classic XP style.
Can I make my application with XP visual style without changing the PC theme to XP (even if XP theme is disabled)?
If no, Can I do it without working in register ? (with an API or other way)
If no, so how can I have XP style for my application ?
I need VB.NET or C# code.
Thanks.
Mo

# re: IsVisualStylesEnabled Revisited

Friday, March 25, 2005 8:55 PM by FsX
Did anyone convert this to C#?

Thanks.

# re: IsVisualStylesEnabled Revisited

Friday, March 25, 2005 9:22 PM by Cory Smith
If you follow the link to Eric Porter's site, you'll see that his code is in C#. This update was optimized further based on him and I discussing some things, so what he has on his site isn't an exact port of the above code, but should be enough to get you started. My code is just optimized a bit more over what he has on his site.
Anonymous comments are disabled