I add a button named “Button1� and a progressbar named “ProgressBar1�. I add the following code for the Button1 click event:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
ProgressBar1.Value = 50
End Sub
Now I add a text file to the project and rename it as ExecutableFileName.exe.manifest (Where ExecutableFileName.exe is the executable created by debugging the project). It contains the following xml code:
<?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="Microsoft.Winweb.ExecutableName"
type="win32"
/>
<description>.NET control deployment tool</description>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="X86"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>
Now build the project. Save all. Now I copy the manifest file from the default project directory to the obj\debug directory. I add the manifest to the executable as a resource.
Add the Manifest to the Executable File
Next, open the executable file within Visual Studio to add the manifest as a resource.
To add the manifest as a resource
1. In the Visual Studio development environment, on the File menu, point to Open, then click File.
2. Navigate to the directory containing this Visual Studio solution. This is the directory you saved it in during Step 1 of the "Create the Project" section.
3. Open the obj directory and then the Debug or Release directory (depending on the build option you set in the development environment).
4. Locate the executable file (it will be in the form of ProjectName.exe) and double-click it to open it with the Visual Studio environment.
5. Right-click the executable file in the designer and choose Add Resource.
6. In the Add Resource dialog box, click the Import button.
7. Navigate to the manifest file you created, which should be located in the same directory as your solution.
Note Be sure that the Files Of Type field in the dialog box is set to All Files (*.*) so that you can see the manifest file in the file picker.
8. Double-click the manifest file.
The Custom Resource Type dialog box opens.
9. In the Resource Type box, type RT_MANIFEST and click OK.
10. In the Properties window, set the ID property to 1.
11. From the File menu, choose Save All to save the changes you have made.
[COLOR=Black]
Now I build the project and run the application. The controls on your Windows Form appear in the futuristic Windows XP visual style except the button control. Adding the following code excerpt should do the trick but in vain. The visual style of the other controls obtained is also lost.
This code snippet checks whether the OS is windows XP and checks for the existence of a manifest file for the executable. Then the program iterates through the button controls in the form to change the flat style.
Private Sub RecursivelyFormatForWinXP(ByVal control As Control)
Dim x As Integer
For x = 0 To control.Controls.Count - 1
' If the control derives from ButtonBase,
' set its FlatStyle property to FlatStyle.System.
If control.Controls(x).GetType().BaseType Is _
GetType(ButtonBase) Then
CType(control.Controls(x), ButtonBase).FlatStyle = _
FlatStyle.System
End If
' If the control holds other controls, iterate through them also.
If control.Controls.Count > 0 Then
RecursivelyFormatForWinXP(control.Controls(x))
End If
Next x
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' Makes sure Windows XP is running and
' a .manifest file exists for the EXE.
If Environment.OSVersion.Version.Major > 4 And _
Environment.OSVersion.Version.Minor > 0 And _
System.IO.File.Exists((Application.ExecutablePath + ".manifest")) Then
' Iterate through the controls.
Dim x As Integer
For x = 0 To (Me.Controls.Count) - 1
' If the control derives from ButtonBase,
' set its FlatStyle property to FlatStyle.System.
If Me.Controls(x).GetType().BaseType Is _
GetType(ButtonBase) Then
CType(Me.Controls(x), ButtonBase).FlatStyle = _
FlatStyle.System
End If
RecursivelyFormatForWinXP(Me.Controls(x))
Next x
End If
End Sub
I verified whether the manifest file for the executable exits. Nevertheless, it has vanished. Therefore, I add the .manifest file again to the executable and build the application. NO ERROR IS THROWN. The most anticipated Windows XP Visual style is applied to all but the button control. Irony is that I had done this successfully some months ago. However, due to unavoidable circumstances I lost the project.
Please read the following URL in the MSDN .NET library. Try it yourself and tell me what your result is.
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dv_vstechart/html/vbtchUsingWindowsXPVisualStylesWithControlsOnWindowsForms.htm