Click here to Skip to main content
12,954,856 members (77,641 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 23 Jan 2012

Enabling Shell Styles for the ListView and TreeView Controls in C#

, 23 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Enabling shell styles for the ListView and TreeView controls in C#

For those who remember the Common Controls OCXs 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).

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 above), 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)

      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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Richard James Moss
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Viktor_BVP17-Apr-12 2:05
memberViktor_BVP17-Apr-12 2:05 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170525.1 | Last Updated 23 Jan 2012
Article Copyright 2012 by Richard James Moss
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid