Export (0) Print
Expand All

Applying Windows XP Visual Styles to Windows Forms Applications

Visual Studio .NET 2003

Windows XP has introduced a new look and feel to the Windows user interface (UI). Composed of the same controls that ship with Visual Studio .NET, the Windows XP UI features an updated, modern look. Controls with rounded corners that light up on mouse over and a futuristic ProgressBar control are two of the enhancements Windows XP introduces.

Some Windows Forms controls will assume their new guise as soon as the application is bound to the new version of Comctl32.dll (the DLL that controls the appearance of the common controls). Other controls need more encouragement. Specifically, the controls that derive from the System.Windows.Forms.ButtonBase class (the Button, RadioButton, and CheckBox controls) and the GroupBox control have a property, FlatStyle, that indicates how the controls should be drawn. When the FlatStyle property is set to System, the appearance of the control is determined by the user's operating system. This setting makes controls that have this property paint themselves in the manner of Windows XP. Note that there are some Windows Forms controls that will look the same on all operating systems (Label, LinkLabel, DomainUpDown, NumericUpDown, and CheckedListBox controls).

For more infomation on working with visual styles, see Using Windows XP Visual Styles With Controls on Windows Forms in the MSDN Library Online.

To apply visual styles to a form

  1. For controls that have a FlatStyle property (see explanation above), set this property to System.
  2. Right-click the form and choose View Code.
    1. In Visual Basic, add the following code to your form's class. The code snippet below assumes the form's name is Form1.
      ' Visual Basic
      Shared Sub Main()
          Application.EnableVisualStyles()
          Application.Run(New Form1)
      End Sub
      
    2. In Visual C#, find the Main() method and modify it, so that it has the following bold line of code. The code snippet below assumes the form's name is Form1.
      // C#
      static void Main() 
      {
          Application.EnableVisualStyles();
          Application.Run(new Form1());
      }
      
    3. In Visual C++, find the _tWinMain procedure and modify it, so that it has the following bold line of code The code snippet below assumes the form's name is Form1.
      // C++
      int APIENTRY _tWinMain(HINSTANCE hInstance,
                           HINSTANCE hPrevInstance,
                           LPTSTR    lpCmdLine,
                           int       nCmdShow)
      {
          System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA;
          Application::EnableVisualStyles();
          Application::Run(new Form1());
          return 0;
      }
      
    Note   If one of the controls on your form is an ImageList control, be sure to include the following line of code before calling Application.Run().
    ' Visual Basic
    Application.DoEvents()
    
    // C#
    Application.DoEvents();
    
    // C++
    Application::DoEvents();
    
  3. In Visual Basic:
    1. In Solution Explorer, right-click the project and choose Properties.
    2. In the project's property page dialog box, set the Startup Object field to Sub Main.
    3. Click Apply.

At this point, your form and all its controls will appear with visual styles applied at run time.

See Also

Using Windows XP Visual Styles With Controls on Windows Forms | FlatStyle enumeration | Creating Windows Forms

Show:
© 2014 Microsoft