This content has moved - please find it at https://devblog.cyotek.com.

Although these pages remain accessible, some content may not display correctly in future as the new blog evolves.

Visit https://devblog.cyotek.com.

Enabling shell styles for the ListView and TreeView controls in C#

For those who remember the Common Controls OCX's featured in Visual Basic 5 and 6, there was one peculiarity of these. In Visual Basic 5, the Common Controls were linked directly to their shell counterparts. As the shell was updated, so did the look of any VB app using these. However, for Visual Basic 6, this behaviour was changed and they didn't use the shell for drawing.

Curiously enough, history repeats itself in a limited way with Visual Studio .NET. If you use the ListView or TreeView controls on Windows Vista or higher, you'll find they are somewhat drawn according to the "classic" Windows style - no gradients on selection highlights, column separators (ListView) or alternate +/- glyphs (TreeView).

Examples of the default TreeView and ListView controls in Windows 7

Fortunately however, it is quite simple to enable this with a single call to the SetWindowTheme API when creating the control.

[DllImport("uxtheme.dll", CharSet = CharSet.Unicode)]
public extern static int SetWindowTheme(IntPtr hWnd, string pszSubAppName, string pszSubIdList);

In the sample application (available for download from the link below), we create two new ListView and TreeView classes which inherit from their System.Windows.Forms counterparts.

In each class, override the OnHandleCreated method, and check to see what OS is being run - if you try to call SetWindowTheme on an unsupported OS, you'll get a crash. In this case, I'm checking for Windows Vista or higher.

If the version is fine, call SetWindowTheme with the handle of the control, and the name of the shell style - explorer in this case.

It's as simple as that - now when you run the application, the controls will be drawn using whatever shell styles are in use.

using System;

namespace ShellControlsExample
{
  class TreeView : System.Windows.Forms.TreeView
  {
    protected override void OnHandleCreated(EventArgs e)
    {
      base.OnHandleCreated(e);

      if (!this.DesignMode && Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major >= 6)
        NativeMethods.SetWindowTheme(this.Handle, "explorer", null);
    }
  }
}

For the TreeView control, I'd also recommend setting the ShowLines property to false as it will look odd otherwise.

Examples of the TreeView and ListView controls in Windows 7 after using the SetWindowTheme API

Update History

  • 2011-04-16 - First published
  • 2020-11-21 - Updated formatting

Downloads

Filename Description Version Release Date
shellcontrolsexample.zip
  • md5: 47ec793695147b1fd5c45608fae7673b

Sample which shows how to display ListView and TreeView controls using Visual Styles in Windows Vista or higher via the SetWindowTheme API.

16/04/2011 Download

About The Author

Gravatar

The founder of Cyotek, Richard enjoys creating new blog content for the site. Much more though, he likes to develop programs, and can often found writing reams of code. A long term gamer, he has aspirations in one day creating an epic video game. Until that time, he is mostly content with adding new bugs to WebCopy and the other Cyotek products.

Leave a Comment

While we appreciate comments from our users, please follow our posting guidelines. Have you tried the Cyotek Forums for support from Cyotek and the community?

Styling with Markdown is supported

Comments

DotNetKicks.com

# Reply

[b]Enabling shell styles for the ListView and TreeView controls in C#[/b] You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetShoutout

# Reply

[b]Enabling shell styles for the ListView and TreeView controls in C#[/b] Thank you for submitting this cool story - Trackback from DotNetShoutout

Gravatar

GoluSingh

# Reply

This is one of the best articles so far I have read online. Just useful information. Very well presented. It helped me lot to complete my task. When I was searching an article on Treeview control over the internet, I found another nice article related to this topic which is also helped me lot. I would like share that article with you, please see at once... http://www.mindstick.com/Articles/022711d0-b15d-4dc0-b50b-e1a9b4cb6a82/?How%20to%20use%20Tree%20View%20Control%20in%20C#

Thanks Everyone for your precious post.