Difference between revisions of "Visual themes"
Revision as of 11:16, 20 July 2009
|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="18.104.22.168" processorArchitecture="X86" name="Company.Product" type="win32" /> <description>Product Name</description> <dependency> <dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="22.214.171.124" 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
Real common controls
If you want to have XP styled common controls (ListView, TreeView, etc...) then there is another hurdle.
You have to be using the VB5 version of the common controls OCX which unfortunately is missing a fair amount of functionality. If you can live without this, go ahead and swap them over. (Please note that this is NOT version 5 of VB and this will still work fine in VB6)
Unfortunatley, there is no practical method of using the XP password character in a VB text box.
There are two recomended methods for standard win32 applications:
- Create the window with the ES_PASSWORD style. Unfortunatley, we have no access to this in VB.
- Send the EM_SETPASSWORDCHAR message. This sounds promising, but as the VB edit box is not a unicode version, trying to set the right character just shows a letter I with a Diaeresis.
Also check out: Application.EnableVisualStyles() as descibed (with its issues) here: http://blogs.msdn.com/rprabhu/archive/2003/09/28/56540.aspx