March 27, 2007

How to Add XP Visual Style to your Visual Basic Application

Recently, I got some friends asking me about how can they add Windows XP visual style on their Visual Basic application.

Why can't you get the XP style look when you run or compile your Visual Basic application anyway? For your information, the Windows XP Visual Styles are provided by ComCtl32.dll version 6 or later. The ComCtl32 does not apply the latest styles to the client area of an application by default. So, in order to enable it, you need to ensure that your application is linked to the new ComCtl32.dll by calling the ComCtl's InitCommonControls API call. And then, you also need to provide a Manifest file which specifies that the new version of the control styles should be used.

To make this short. All you need to add to your code is this lines of codes on a module...

Private Type tagInitCommonControlsEx
lSize As Long
lICC As Long
End Type

Private Declare Function
InitCommonControlsEx Lib "comctl32.dll" _
(iccex As tagInitCommonControlsEx) As Boolean
Private Const
ICC_USEREX_CLASSES = &H200

Public Function InitCommonControlsVB() As Boolean
On Error Resume Next
Dim
iccex As tagInitCommonControlsEx

With iccex
.lSize = LenB(iccex)
.lICC = ICC_USEREX_CLASSES
End With
InitCommonControlsEx iccex
InitCommonControlsVB = (Err.Number = 0)
On Error Goto 0
End Function


Public Sub Main()
' Call the function before other codes
InitCommonControlsVB
'
' Start your application here:
'

End Sub

Then add this text in a text file...

<?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="CompanyName.ProductName.YourAppName"
type="win32" />
<description>Your application description here
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="X86"
publicKeyToken="6595b64144ccf1df"
language="*" />
</dependentAssembly>
</dependency>
</assembly>

and rename the text file to 'yourexefilename.exe.manifest' and save it in the same folder with your exe file. Also make sure that your application Startup Object is Sub Main (check this in Project > [your app] Properties).

That's all. You can see your application is using XP Style after you compile it and run it outside Visual Basic. You can't see the changes if you run it using Visual Basic.

You can also get detail explanation on this at vbAccelerator. Enjoy!

Share This Article:


Bookmark This Article:
Feed Me Digg Technorati del.icio.us Best to Stumbleupon Reddit Blinklist Furl Spurl Yahoo Simpy

1 comments:

Interpreter of the Obvious said...

I'm a VB6 programmer. I think your tips are gold, Jerry. Gold! Seriously.

I noticed that although your code displays properly, when I copy and paste it, the formatting is lost. The other day I wrote some VB6 code that formats VB6 source code such that formatting is preserved during clipboard operations. See "Formatting Visual Basic 6 code for HTML".