XP Themed Controls

Windows XP brought with it a completely new appearance for its forms and controls, with rounded title bars, gradient-filled backgrounds on many controls, and rounded buttons that get highlighted as you move the mouse over them. You've probably noticed in the course of your .NET programming that while your applications show the XP-style title bar, the buttons and other controls on your forms have stubbornly continued to be drawn in the old Windows 2000 style. That's also the case for all the examples we've shown so far in this book. In this section we'll quickly go over how you can modify your projects so that your controls are drawn as XP themed controls where appropriate.

The principle is actually fairly simple: you just need to make sure that your application loads the correct version of comct132.dll. This DLL is the unmanaged library in which the Windows common controls are implemented, and version 5 is the version that is loaded by default in Windows 9x/NT/2K/ME. With XP came version 6, which incorporates an awareness of XP themes and an ability to draw controls in the XP visual style. However, because themes are a new concept and do require the application to be set up correctly to use them, Microsoft decided to leave version 5 as the version of comct132.dll that is loaded by default. Hence version 6 will only be loaded if an application explicitly indicates that it requires version 6 - which presumably implies that it has been tested with themed UI. You indicate that an application should load version 6 by supplying a manifest. The manifest is an XML file, placed in the same folder as the application's executable file. The name of the manifest file should be the same as the name of the executable, with the suffix .manifest appended, thus: MyApplication.exe.manifest. A suitable manifest file looks like this:

 <?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.XPThemes"    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> 

This XML file is the one used in the XPThemes example I'm about to present, and is therefore called XPThemes.exe.manifest. To adapt it to other programs, you just need to rename it and change the name attribute to "Microsoft.Winweb.<AppName>".

For most .NET controls that is all you need to do, but for any class derived from System.Windows.Forms.ButtonBase, you need to set the FlatStyle property to the value FlatStyle.System.FlatStyle indicates the way in which buttons are drawn and is by default set to FlatStyle.Standard.FlatStyle.Standard draws standard buttons but also allows for owner-draw buttons in which your code takes responsibility for part of the drawing operation. This is not acceptable for themed controls - for that, it's important that the system controls the entire drawing process.

The XPThemes example illustrates these principles. It is a simple form with a number of miscellaneous controls, and a checkbox that indicates whether to display themes. With the checkbox checked, the example looks like this - on Windows XP that is. Obviously, if you run the example on previous versions of Windows, you'll just see standard 9x style controls.

click to expand

If you uncheck the box, the example immediately redraws itself like this;

click to expand

Note that the progress bar and the list box remain in the XP style - the example works by modifying the FlatStyle property of the controls that have this property, but it clearly cannot change the version of comct132.dll that it is using, so controls not derived from ButtonBase remain XP-themed. The Click Me button increments the progress bar and adds the line I've been clicked! to the list box when it is clicked. The radio button controls don't do anything - they are just there to show how they behave.

To create the example, we begin with a standard Windows Forms application. To this we add a couple of methods that iterate through all the controls on the form, setting the FlatStyle property of any that it finds are derived from ButtonBase.

 public void SetXPTheme() {    foreach (Control control in this.Controls)    {       if (control is ButtonBase)          ((ButtonBase)control).FlatStyle = FlatStyle.System;    }    this.Invalidate(); } public void SetPreXPStyle() {    foreach (Control control in this.Controls)    {       if (control is ButtonBase)          ((ButtonBase)control).FlatStyle = FlatStyle.Standard;    }    this.Invalidate(); } 

Then the event handler for the checkbox to set the style:

 private void cbXP_CheckedChanged(object sender, System.EventArgs e) {    if (cbXP.Checked)       SetXPTheme();    else       SetPreXPStyle(); } 

Finally, just to add some spice to the application, an event handler for clicking the button - just so the button does something:

 private void btnClickMe_Click(object sender, System.EventArgs e) {    progressBar.Increment (5) ;    lbResults.Items.Add("I've been clicked!"); } 

In order for the application to run correctly, you'll need to copy the manifest file to the folder containing its executable - either Debug or Release after the first time you compile it. You'll find the manifest has been included as a text file with the code download.



Advanced  .NET Programming
Advanced .NET Programming
ISBN: 1861006292
EAN: 2147483647
Year: 2002
Pages: 124

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net