How to create a flat columnheader button in listview (Windows Forms C#)

3.1k views Asked by At

How would it be possible to create a 'FlatStyle' button for the columnheader of a listview in C#? I have researched this a little and I came across this code for VB: http://www.freevbcode.com/ShowCode.asp?ID=426

I am not sure how I could do this in C#, and I would also like to know if there is a better way of doing this.

Thanks!

3

There are 3 answers

0
Hans Passant On BEST ANSWER

That code dates back to before Vista and no longer works with the new Vista visual style for header controls. You will also need to turn off visual style rendering. It also won't work in 64-bit mode, common these days. Yet another side-effect is that the user no longer gets visual feedback when hovering the column header, an inevitable side-effect of turning off the HDS_BUTTON style flag.

Anyhoo, here's code that makes the headers look flat. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class FlatListView : ListView {
    public FlatListView() {
        this.HeaderStyle = ColumnHeaderStyle.Nonclickable;
    }
    protected override void OnHandleCreated(EventArgs e) {
        if (this.View == View.Details) {
            IntPtr hHeader = SendMessage(this.Handle, LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero);
            SetWindowTheme(hHeader, "", "");
        }
        base.OnHandleCreated(e);
    }

    private const int LVM_GETHEADER = 0x1000 + 31;
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
    [DllImport("uxtheme.dll")]
    private static extern int SetWindowTheme(IntPtr hWnd, string appname, string idlist);
}
0
XikiryoX On

I don't immediatly know a way on how to do this in a windows form however, if your code allows it, you could try to create a WPF application instead of a windows form. The code behind it will still be C# but it allows far more enhanced graphical changes.

Windows forms sometimes just block certain visual posibilities.

Regards, Kevin

1
Alon Adler On

You can do it but the user wont be able to click on the headers.

If you create the columns of a list view, when the user displays the detail view, the column headers appear and behave like regular buttons. This also means that the user can click a column header and you can take action. If you don't want this appearance and this behavior, you can make the columns appear flat. This characteristics is controlled by the HeaderStyle property of the ListView class. This property is based on the ColumnHeaderStyle enumerator. Its members are:

Clickable: This is the default style. The columns headers appear and behave like buttons:

listView1.HeaderStyle = ColumnHeaderStyle.Clickable;

enter image description here

Nonclickable: The columns are flat and don't change their appearance when the user clicks one:

listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;

enter image description here