65.9K
CodeProject is changing. Read more.
Home

WinXpStyle Class

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.76/5 (13 votes)

Jun 7, 2004

2 min read

viewsIcon

58529

downloadIcon

1949

You can use this class for Windows XP Style Forms.

Without WinXpStyle Class

With WinXpStyle Class

Introduction

.NET Framework documentations [1] arguments about how to implement Windows XP visual style, therefore, according to all documentation I have put all in a simple standalone class called WinXpStyle, for example with it you haven't got to think about the manifest [1] file because WinXpStyle class builds it for you if you want (it is not necessary) and also sets FlatStyle property to System for each object like Button, RadioButton, GroupBox and CheckBox controls.

This class is very easy to use, just add to your project, check the namespace name, and invoke the class according to explanations given below.

Using the class WinXpStyle without manifest

WinXpStyle class is a stand alone class, and all its member are static, so creating an instance is not necessary. There are only two steps to use WinXpStyle class, be sure that it is in the same namespace as the form class, so:

1) You have to write in the Main function of the form class the following:

static void Main() 
{
  Application.EnableVisualStyles();
  Application.Run(new Form1());
}

Where Application.EnableVisualStyles() method enables Windows XP visual styles for the application. Controls will draw with visual styles if the control and the operating system supports it. To have an effect, EnableVisualStyles must be called before creating any controls in the application; typically, EnableVisualStyles is the first line in the Main function. A separate manifest is not required to enable visual styles when calling EnableVisualStyles.

2) You have to call FormLoad function of the WinXpStyle class inside Form_load event of Form class as you see below

private void Form1_Load(object sender, System.EventArgs e)
{
  WinXpStyle.FormLoad(sender,e,this);
}

The FormLoad function sets FlatStyle property to System for each object that derives from ButtonBase likes Button, RadioButton, GroupBox and CheckBox controls. It is necessary to show the objects using visual style.

Using the class WinXpStyle with manifest file

If you want to use manifest file [1] you need in this case two steps, because it is not necessary to call Application.EnableVisualStyle in Main function. So you have to do the following things:

1) You have to create manifest file using Manifest function of WinXpStyle class in your Form Class constructor. In this case we assume that "FormTest2" is the name of our project and also is an assembly name.

public Form1()
{
...
InitializeComponent();
...
WinXpStyle.Manifest("FormTest2");
...
}        

Where "FormTest2" is an Assembly Name: it's a name of the output file will hold assembly metadata (manifest). The function Manifest of this class makes manifest file in the correct directory.

2) You have to call FormLoad function of the WinXpStyle class inside Form_load event of Form class as you see below

private void Form1_Load(object sender, System.EventArgs e)
{
  winXpStyle.FormLoad(sender,e,this);
}

The WinXpStyle class

I consider that is it a stand alone class and also all its public function are static. Also you can see in the picture below the association between Form class and WinXpStyle Class

Code

using System;
using System.Windows.Forms;
using System.IO;

namespace WinXpStyle
{
   /// <SUMMARY>
   /// Summary description for WinXpStyle by Hugo C Romano.
   /// Using Windows XP Visual Styles With Controls on Windows Forms
   /// Based on Visual Studio .NET Technical Articles
   /// </SUMMARY>
   public sealed class WinXpStyle
   {
      private static string fileName;
      private static string executableName;
         
      // You need to see if Windows XP is running and 
      // whether the manifest file is present, 
     // if it not exist constructor create it.

      private  WinXpStyle() {}

      public static void Manifest(string ExeFile) 
      {
         executableName = ExeFile ;
         fileName = executableName + ".exe.manifest";
         if(!System.IO.File.Exists(
            Application.ExecutablePath + ".manifest"))
            CreateManifest();

      }
      // Create manifest XML file
      private static void CreateManifest() 
      {
         StreamWriter outStream = File.AppendText(fileName);
         try 
         {
            outStream.WriteLine(
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
            outStream.WriteLine(
       "1.0\"' + ? "+
       "xmlns='\"urn:schemas-microsoft-com:asm.v1\"'>");
       outStream.WriteLine(
       ""/' "+
   "outStream.WriteLine(? outStream.WriteLine(?name='\"Microsoft." "+
   "+executableName+"." +   executableName+"\"");' ?"+
   "processorArchitecture='\"X86\"");'"+
   " version='\"1.0.0.0\"'>");
   outStream.WriteLine(
     "<DESCRIPTION>.NET control deployment tool</DESCRIPTION>");
       outStream.WriteLine("<DEPENDENCY>");
       outStream.WriteLine("<DEPENDENTASSEMBLY>");
       outStream.WriteLine(
        ""/' + "+
   "? type='\"win32\"' ?processorArchitecture="+
   "'\"X86\"' version='\"6.0.0.0\"' ?publicKeyToken"+
   "='\"6595b64144ccf1df\"' ?Windows.Common-Controls"+
   "\? name='\"Microsoft."'>");
            outStream.WriteLine("</DEPENDENTASSEMBLY>");
            outStream.WriteLine("</DEPENDENCY>");
            outStream.WriteLine("");
         }
         catch(System.Exception e) 
         {
            Console.WriteLine(e.Message);
         }
         finally 
         {
            outStream.Close();
         }
      }

      // This code controls if FlatStyle property is FlatStyle.System.
      private static void RecursivelyFormatForWinXP(Control control)
      {
         for(int x = 0; x < control.Controls.Count; x++)
         {
            // If the control derives from ButtonBase, 
            //  set its FlatStyle property to FlatStyle.System.
            if(control.Controls[x].GetType().BaseType == typeof(ButtonBase))
            {
               ((ButtonBase)control.Controls[x]).FlatStyle = 
                 FlatStyle.System; 
            }
  
            // If the control holds other controls, 
            // iterate through them also.
            if(control.Controls.Count > 0)
            {
               RecursivelyFormatForWinXP(control.Controls[x]);
            }
         }
      }


      // You need to see if Windows XP is running and 
      // whether the manifest file is present.


      public static void FormLoad(object sender, 
           System.EventArgs e, Form thisForm)
      {
         // Makes sure Windows XP is running  
         if(Environment.OSVersion.Version.Major > 4 
            & Environment.OSVersion.Version.Minor > 0 )
         { 
            // Iterate through the controls.
            for(int x = 0; x < thisForm.Controls.Count; x++)
            {
               // If the control derives from ButtonBase,
               // set its FlatStyle property to FlatStyle.System.
               if(thisForm.Controls[x].GetType().BaseType == 
                  typeof(ButtonBase))
               {
                  ((ButtonBase)thisForm.Controls[x]).FlatStyle = 
                      FlatStyle.System; 
               }
               RecursivelyFormatForWinXP(thisForm.Controls[x]);
            }
         }
      }
   }
}

Reference

  • [1] Seth Grossman, Visual Studio Team, Microsoft Corporation, January 2002
  • [2] .NET framework class library, Enable Windows XP visual styles for the application