Using Windows XP Visual Styles with Controls on Windows Forms

“ms-help://MS.MSDNQTR.2003FEB.1033/dv_vstechart/html/vbtchUsingWindowsXPVisualStylesWithControlsOnWindowsForms.htm.

Please read the MSDN document at the URL within the double quotes.

The above URL is from the MSDN library for visual studio.NET 2003. I came upon this URL by accident.

I tried this trick and the project did not come as expected. I am able to add the visual style to the controls that change visual style when bound to Comctl32.dll, version 6.0. All works fine until this step.

All the visual style gained until this step is lost while adding the code, which dynamically changes the FlatStyle property to System.

I am running Windows XP sp2, Visual Studio .NET 2003 sp1. Surprisingly I had this project done successfully before some time. However, due to some unavoidable reason I had to format my computer and lost this project. Since then I am not able to get this done.

Some more information on your problem as well as some code snippets would make it a lot easier to figure out what's going on. What exactly do you mean by 'visual style is lost'. Do the controls render as flat? Does your application produce an error? Also make sure you're not running windows in Classic mode, that disables styles no matter what your code says.

Please post back with some more information and I'm sure we can get this straightened out for you.

Wally
www.power-coder.net

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.

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

This article has been dead for over six months. Start a new discussion instead.