我可以检测用户是否右键单击 Winforms 中的 ListView 列标题吗?

发布于 2024-12-11 06:31:11 字数 36 浏览 0 评论 0原文

我指的不是列表视图项的内部,而是允许您调整列大小的列标题。

I don't mean the inside of the listview items but the column header that allows you to resize the column.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗心遗梦遗幸福 2024-12-18 06:31:11

一个简单的 UserControl 覆盖 ListViewOnMouseEnter OnMouseLeave & WndProc

public partial class MyListView : ListView
{
    public MyListView()
    {
    }

    public delegate void ColumnContextMenuHandler(object sender, ColumnHeader columnHeader);
    public event ColumnContextMenuHandler ColumnContextMenuClicked = null;

    bool _OnItemsArea = false;
    protected override void OnMouseEnter(EventArgs e)
    {
        base.OnMouseEnter(e);
        _OnItemsArea = true;
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        base.OnMouseLeave(e);
        _OnItemsArea = false;
    }

    const int WM_CONTEXTMENU = 0x007B;

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_CONTEXTMENU)
        {
            if (!_OnItemsArea)
            {
                Point p = base.PointToClient(MousePosition);
                int totalWidth = 0;
                foreach (ColumnHeader column in base.Columns)
                {
                    totalWidth += column.Width;
                    if (p.X < totalWidth)
                    {
                        if (ColumnContextMenuClicked != null) ColumnContextMenuClicked(this, column);
                        break;
                    }
                }
            }
        }
        base.WndProc(ref m);
    }
}

及其用法

 myListView1.ColumnContextMenuClicked += (sndr, col) =>
 {
    this.Text = col.Text;
 };

A simple UserControl overriding ListView's OnMouseEnter OnMouseLeave & WndProc

public partial class MyListView : ListView
{
    public MyListView()
    {
    }

    public delegate void ColumnContextMenuHandler(object sender, ColumnHeader columnHeader);
    public event ColumnContextMenuHandler ColumnContextMenuClicked = null;

    bool _OnItemsArea = false;
    protected override void OnMouseEnter(EventArgs e)
    {
        base.OnMouseEnter(e);
        _OnItemsArea = true;
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        base.OnMouseLeave(e);
        _OnItemsArea = false;
    }

    const int WM_CONTEXTMENU = 0x007B;

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_CONTEXTMENU)
        {
            if (!_OnItemsArea)
            {
                Point p = base.PointToClient(MousePosition);
                int totalWidth = 0;
                foreach (ColumnHeader column in base.Columns)
                {
                    totalWidth += column.Width;
                    if (p.X < totalWidth)
                    {
                        if (ColumnContextMenuClicked != null) ColumnContextMenuClicked(this, column);
                        break;
                    }
                }
            }
        }
        base.WndProc(ref m);
    }
}

and the usage

 myListView1.ColumnContextMenuClicked += (sndr, col) =>
 {
    this.Text = col.Text;
 };
心意如水 2024-12-18 06:31:11

OnMouseEnter/Leave 解决方案有时甚至不在标题上显示上下文菜单。这是更好的解决方案用于列标题的ListView ContextMenuStrip

The OnMouseEnter/Leave solution shows the context menu sometimes even not on the header. Here is better solution ListView ContextMenuStrip for column headers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文