简体   繁体   English

将过滤器框添加到C#和WinForms中ListView的列标题中

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

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 example you can only show files starting with "A" or files > 128 MB. 在Windows资源管理器(至少在Win7中)中,当您将鼠标悬停在列标题上时,会出现带箭头的过滤器框,您可以在ListView中过滤结果,例如,您只能显示以“A”开头的文件或文件> 128 MB。 Can this feature be enabled in the basic ListView control in C# without subclassing or modifying the ListView? 可以在C#的基本ListView控件中启用此功能而无需子类化或修改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. 将新的ListViewEx控件从工具箱顶部拖放到表单上。 In the form constructor, call the SetHeaderDropdown() method to enable the button. 在表单构造函数中,调用SetHeaderDropdown()方法以启用该按钮。 Implement the HeaderDropdown event to return a control to display. 实现HeaderDropdown事件以返回要显示的控件。 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. 实现相同类型的接口可能很棘手,但您可以让ListView通过处理TextBoxTextChanged事件并根据内容过滤列表来响应TextBox的内容。 If you put the list in a DataTable then filtering will be easy and you can repopulate your ListView each time the filter changes. 如果将列表放在DataTable则过滤将很容易,并且每次过滤器更改时您都可以重新填充ListView

Of course this depends on how many items are in your list. 当然,这取决于列表中的项目数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM