Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C#
Article

WinXpStyle Class

Rate me:
Please Sign up or sign in to vote.
3.76/5 (15 votes)
6 Jun 20042 min read 57.9K   1.9K   33   9
You can use this class for Windows XP Style Forms.

Image 1

Without WinXpStyle Class

Image 2

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:

C#
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

C#
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.

C#
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

C#
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

Image 3

Code

C#
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(
       "<ASSEMBLY ?manifestVersion='\"1.0\"' + ? "+
       "xmlns='\"urn:schemas-microsoft-com:asm.v1\"'>");
       outStream.WriteLine(
       "<ASSEMBLYIDENTITY + ? type='\"win32\"/' "+
   "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(
        "<ASSEMBLYIDENTITY language='\"*\"/' + "+
   "? type='\"win32\"' ?processorArchitecture="+
   "'\"X86\"' version='\"6.0.0.0\"' ?publicKeyToken"+
   "='\"6595b64144ccf1df\"' ?Windows.Common-Controls"+
   "\? name='\"Microsoft."'>");
            outStream.WriteLine("</DEPENDENTASSEMBLY>");
            outStream.WriteLine("</DEPENDENCY>");
            outStream.WriteLine("</ASSEMBLY>");
         }
         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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Israel Israel
EDUCATION:
Information Systems Engineer-National Technological University–Argentina-1995
Systems Analyst-National Technological University–Argentina-1991

LANGUAGES:
Spanish (mother tongue),English,Hebrew

SPECIFIC SKILLS:
•OOD, OOP, UML
•Programmer of:C#,C++,C,JAVA,Progress 4GL, VB,VFPro,Access
•Data Bases Management in SQL Server, Progress ,Interbase and MySQL
•Operative System Windows XP, Windows NT 4.0, Windows 2000, Windows 3.11
•Networks: Windows, Novell, TCP/IP
•Knowledge: .Net Framework, MFC, COM,DDE, HTML, DHTML,XML,ADO,ODBC, Java scrip and VB script

PROFESSIONAL EXPERIENCE:
•Designing, developing and supporting software for many commercial companies : Accounting – Billing – stock control – costumer control – salesperson – from 1995 to 2002.
•Consultant of telecomunication network, Municipality of Yerba Buena – Tucuman – Argentina, 1997 - 1998
•Consultant and Supervisor of informatic project , Govermment of Tucuman, Cience and Tehnology Department 1996 – 1998
•STARTEL S.A. Official Agent in telecommunications : Designed telecommunication project, using VSAT Technology and WAN network. from 1995 to 1998
•TECOTEX S.A. textile factory. Responsible for all development projects for the technical, cost, schedule, and testing, from 1991 to 1995:
Control of knitting plant production, spinning plant production, textile plant preparation, spinning plant preparation, mix fiber plant,preparation, Stock

ACADEMIC EXPERIENCE:
System Department (National Technological University,FRT–1995 to 2002), responsible for:
System Design (UML), System Design Methodology (Unified Process Meotodology), System Design Subject, Data Base Systems, Data Management, Paradigms of Programming, Programming (Pascal), Data System

RESEARCH EXPERIENCE AND SCHOLARSHIPS:
At National Technological University, Regional Tucuman Department
Research made on “Access to Educational Resources through 2nd and 3rd generational mobile phones using UMTS technology”.

Comments and Discussions

 
GeneralThanks you saved my day ! Pin
mee2011-Jun-07 21:57
mee2011-Jun-07 21:57 
GeneralI use Win 2000, I can't demo this version. Pin
nghiak46cb31-Jul-05 23:37
nghiak46cb31-Jul-05 23:37 
:->I run it on Win XP, it is OK.
When run it on Win 2000,
it is not visual style.
What can I do ?
GeneralRe: I use Win 2000, I can't demo this version. Pin
BenSoft22-Nov-05 3:54
BenSoft22-Nov-05 3:54 
Generalbutton.ForeColor doesn't work Pin
Cong Dan8-Oct-04 16:56
Cong Dan8-Oct-04 16:56 
GeneralEnableVisualStyles Pin
Tom Spink8-Jun-04 1:37
Tom Spink8-Jun-04 1:37 
GeneralRe: EnableVisualStyles Pin
dzCepheus10-Jun-04 16:58
dzCepheus10-Jun-04 16:58 
GeneralRe: EnableVisualStyles Pin
Tom Spink10-Jun-04 21:26
Tom Spink10-Jun-04 21:26 
GeneralRe: EnableVisualStyles Pin
dzCepheus11-Jun-04 1:50
dzCepheus11-Jun-04 1:50 
GeneralRe: EnableVisualStyles Pin
HugoRomano17-Jun-04 4:18
HugoRomano17-Jun-04 4:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.