Visual themes

From HashVB
Revision as of 23:32, 23 February 2006 by 213.224.83.4 (Talk)

Jump to: navigation, search
float
 This article is based on Visual Basic 6. Find other Visual Basic 6 articles.

If you want to make use of the XP look and feel in your applications, you need to use a new feature called application manifests. These instruct windows to load a special version of a particular library that has the XP styled controls in.

A typical application manifest looks like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
    version="1.0.0.0"
    processorArchitecture="X86"
    name="Company.Product"
    type="win32"
/>
<description>Product Name</description>
<dependency>
    <dependentAssembly>
        <assemblyIdentity
            type="win32"
            name="Microsoft.Windows.Common-Controls"
            version="6.0.0.0"
            processorArchitecture="X86"
            publicKeyToken="6595b64144ccf1df"
            language="*"
        />
    </dependentAssembly>
</dependency>
</assembly>

This can either be in the same folder as your executable called appname.exe.manifest or it can be embedded in a resource file with a type of 24 and an ID of 1.

Now, to make these work properly, you need to call the InitCommonControls function before any forms are loaded. This can either be in the startup forms Initialize event or in Sub Main()

Private Declare Sub InitCommonControls Lib "comctl32.dll" ()

InitCommonControls

Under some circumstances, using the XP styles can cause VB applications to crash on shut down after the last code is executed. This can be worked around by loading the shell32.dll into your applications memory space on startup:

Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" ( _
  ByVal lpLibFileName As String) As Long

LoadLibrary "shell32.dll"

I have these in a single routine that I call on startup:

'This needs to be called in Sub Main or Form_Initialize otherwise it will fail with a manifest
Sub XPInitStyle()
  LoadLibrary "shell32.dll"
  InitCommonControls
End Sub

Happy styling.

See also

VB.NET 2003

Check out: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchusingwindowsxpvisualstyleswithcontrolsonwindowsforms.asp

Also check out: Application.EnableVisualStyles()