Visual themes

From HashVB
Revision as of 13:41, 3 November 2005 by Dee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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

vbAccelerator