开发者

Adding filter boxes to the column headers of a ListView in C# and WinForms

开发者 https://www.devze.com 2023-04-12 07:36 出处:网络
In Windows Explorer (at least in Win7) when you hover the mouse over a column header, a filter box with an arrow appears that lets you filter the results in the ListView, so for exampl开发者_运维百科e

In Windows Explorer (at least in Win7) when you hover the mouse over a column header, a filter box with an arrow appears that lets you filter the results in the ListView, so for exampl开发者_运维百科e you can only show files starting with "A" or files > 128 MB. Can this feature be enabled in the basic ListView control in C# without subclassing or modifying the ListView?


Here's some code to play with. Add a new class to your project and paste the code shown below. Compile. Drop the new ListViewEx control from the top of the toolbox onto your form. In the form constructor, call the SetHeaderDropdown() method to enable the button. Implement the HeaderDropdown event to return a control to display. For example:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        listViewEx1.SetHeaderDropdown(0, true);
        listViewEx1.HeaderDropdown += listViewEx1_HeaderDropdown;
    }

    void listViewEx1_HeaderDropdown(object sender, ListViewEx.HeaderDropdownArgs e) {
        e.Control = new UserControl1();
    }
}

The below code has a flaw, the popup is displayed in a form. Which can't be too small and takes the focus away from the main form. Check this answer on hints how to implement a control that can be displayed as a toplevel window without needing a form. The code:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class ListViewEx : ListView {
    public class HeaderDropdownArgs : EventArgs {
        public int Column { get; set; }
        public Control Control { get; set; }
    }
    public event EventHandler<HeaderDropdownArgs> HeaderDropdown;

    public void SetHeaderDropdown(int column, bool enable) {
        if (column < 0 || column >= this.Columns.Count) throw new ArgumentOutOfRangeException("column");
        while (HeaderDropdowns.Count < this.Columns.Count) HeaderDropdowns.Add(false);
        HeaderDropdowns[column] = enable;
        if (this.IsHandleCreated) SetDropdown(column, enable);
    }
    protected void OnHeaderDropdown(int column) {
        var handler = HeaderDropdown;
        if (handler == null) return;
        var args = new HeaderDropdownArgs() { Column = column };
        handler(this, args);
        if (args.Control == null) return;
        var frm = new Form();
        frm.FormBorderStyle = FormBorderStyle.FixedSingle;
        frm.ShowInTaskbar = false;
        frm.ControlBox = false;
        args.Control.Location = Point.Empty;
        frm.Controls.Add(args.Control);
        frm.Load += delegate { frm.MinimumSize = new Size(1, 1);  frm.Size = frm.Controls[0].Size; };
        frm.Deactivate += delegate { frm.Dispose(); };
        frm.StartPosition = FormStartPosition.Manual;
        var rc = GetHeaderRect(column);
        frm.Location = this.PointToScreen(new Point(rc.Right - SystemInformation.MenuButtonSize.Width, rc.Bottom));
        frm.Show(this.FindForm());
    }

    protected override void OnHandleCreated(EventArgs e) {
        base.OnHandleCreated(e);
        if (this.Columns.Count == 0 || Environment.OSVersion.Version.Major < 6 || HeaderDropdowns == null) return;
        for (int col = 0; col < HeaderDropdowns.Count; ++col) {
            if (HeaderDropdowns[col]) SetDropdown(col, true);
        }
    }

    private Rectangle GetHeaderRect(int column) {
        IntPtr hHeader = SendMessage(this.Handle, LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero);
        RECT rc;
        SendMessage(hHeader, HDM_GETITEMRECT, (IntPtr)column, out rc);
        return new Rectangle(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
    }

    private void SetDropdown(int column, bool enable) {
        LVCOLUMN lvc = new LVCOLUMN();
        lvc.mask = LVCF_FMT;
        lvc.fmt = enable ? LVCFMT_SPLITBUTTON : 0;
        IntPtr res = SendMessage(this.Handle, LVM_SETCOLUMN, (IntPtr)column, ref lvc);
    }

    protected override void WndProc(ref Message m) {
        Console.WriteLine(m);
        if (m.Msg == WM_NOTIFY) {
            var hdr = (NMHDR)Marshal.PtrToStructure(m.LParam, typeof(NMHDR));
            if (hdr.code == LVN_COLUMNDROPDOWN) {
                var info = (NMLISTVIEW)Marshal.PtrToStructure(m.LParam, typeof(NMLISTVIEW));
                OnHeaderDropdown(info.iSubItem);
                return;
            }
        }
        base.WndProc(ref m);
    }

    private List<bool> HeaderDropdowns = new List<bool>();

    // Pinvoke
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, ref LVCOLUMN lvc);
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, out RECT rc);
    [DllImport("user32.dll")]
    private static extern IntPtr SetParent(IntPtr hWnd, IntPtr hParent);

    private const int LVM_SETCOLUMN = 0x1000 + 96;
    private const int LVCF_FMT = 1;
    private const int LVCFMT_SPLITBUTTON = 0x1000000;
    private const int WM_NOTIFY = 0x204e;
    private const int LVN_COLUMNDROPDOWN = -100 - 64;
    private const int LVM_GETHEADER = 0x1000 + 31;
    private const int HDM_GETITEMRECT = 0x1200 + 7;


    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct LVCOLUMN {
        public uint mask;
        public int fmt;
        public int cx;
        public string pszText;
        public int cchTextMax;
        public int iSubItem;
        public int iImage;
        public int iOrder;
        public int cxMin;
        public int cxDefault;
        public int cxIdeal;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct POINT {
        public int x, y;
    }
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct RECT {
        public int left, top, right, bottom; 
    }
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct NMHDR {
        public IntPtr hwndFrom;
        public IntPtr idFrom;
        public int code;
    }
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct NMLISTVIEW {
        public NMHDR hdr;
        public int iItem;
        public int iSubItem;
        public uint uNewState;
        public uint uOldState;
        public uint uChanged;
        public POINT ptAction;
        public IntPtr lParam;
    }
}


It might be tricky to implement the same type of interface, but you could have your ListView respond to the contents of a TextBox by handling the TextBox's TextChanged event and filtering the list based on the contents. If you put the list in a DataTable then filtering will be easy and you can repopulate your ListView each time the filter changes.

Of course this depends on how many items are in your list.

0

精彩评论

暂无评论...
验证码 换一张
取 消