gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:38:32 25 4
gpt4 key购买 nike

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

最佳答案

一个简单的 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;
};

关于c# - 我可以检测用户是否右键单击了 Winforms 中的 ListView 列标题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7844306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com