There have been discussions on the Internet about implementing XP style through programming for a long time, but there is no perfect solution for implementing XP style through VB programming. The author finally revealed the secret through N days and nights of hard study. The following is divided into three aspects to share with you.
Click to download the source code file
one. Using manifest files to achieve XP style
Under normal circumstances, in Windows XP systems, only the window title bar of an application developed with VB6 has the XP style, and the buttons, text boxes and other controls on the form still display the traditional Windows style. As shown in Figure 1:
By consulting the Visual Style chapter in MSDN, we know that Windows XP uses Comctl32.dll (version 6) to load components with XP style, and the application notifies the system to do this through an XML resource file. The contents of the XML file are as follows:
<?xml version=1.0 encoding=UTF-8 standalone=yes?>
<assembly xmlns=urn:schemas-microsoft-com:asm.v1 manifestVersion=1.0>
<assemblyIdentity
name=XP style manifest
processorArchitecture=x86
version=1.0.0.0
type=win32/>
<dependency>
<dependentAssembly>
<assemblyIdentity
type=win32
name=Microsoft.Windows.Common-Controls
version=6.0.0.0
processorArchitecture=x86
publicKeyToken=6595b64144ccf1df
language=*
/>
</dependentAssembly>
</dependency>
</assembly>
Assume that the name of your final compiled program is abc.exe, and the working directory is d:/vbxp. Copy the above XML content and save it as a text file. Then rename the file abc.exe.manifest (note that the .txt extension should be removed). In the VB program, we need to call the InitCommonControlsEx function to initialize the component class from comctl32.dll (version 6) before all forms are loaded. The API function InitCommonControlsEx and related constants and data types are declared as follows:
Private Declare Function InitCommonControlsEx Lib comctl32.dll _
(iccex As tagInitCommonControlsEx) As Boolean
Private Type tagInitCommonControlsEx
lngSize As Long
lngICC As Long
End Type
Private Const ICC_USEREX_CLASSES = &H200
Here we write a function to encapsulate the initialization operation:
Public Function InitCommonControlsVB() As Boolean
On Error Resume Next
Dim iccex As tagInitCommonControlsEx
With iccex
.lngSize = LenB(iccex)
.lngICC = ICC_USEREX_CLASSES
End With
InitCommonControlsEx iccex
InitCommonControlsVB = (Err.Number = 0)
On Error Goto 0
End Function
Note that the initialization action must be completed before all forms are loaded, so the relevant statements must be placed in Sub Main(), and the project must be started from Sub Main(). The code is as follows:
Sub Main()
InitCommonControlsVB
Form1.Show
End Sub
At this point, your compiled abc.exe will have XP style, as shown in Figure 2: