ListView控件的ContextMenuStrip的列标题 [英] ListView ContextMenuStrip for column headers

查看:313
本文介绍了ListView控件的ContextMenuStrip的列标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我显示不同的ContextMenuStrip当我右键点击的ListView列标题,另一个里面的ListView。

I display a different ContextMenuStrip when I right click the ListView column header, and another inside ListView.

class ListViewExx : ListView
{
    public ContextMenuStrip HeaderContextMenu { get; set; }
    int contextMenuSet = 0;
    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        base.WndProc(ref m);
        switch(m.Msg)
        {
            case 0x210: //WM_PARENTNOTIFY
                contextMenuSet = 1;
                break;
            case 0x21:  //WM_MOUSEACTIVATE
                contextMenuSet++;
                break;
            case 0x7b:  //WM_CONTEXTMENU
                if(contextMenuSet == 2 && HeaderContextMenu != null)
                    HeaderContextMenu.Show(Control.MousePosition);
                break;
        }
    }
}

这工作得很好。问题是我第一次右键单击里面的ListView - 显示的ContextMenuStrip头

This works very well. The problem is the FIRST TIME I right click inside the ListView - the headers contextMenuStrip is shown.

推荐答案

依托激活状态太哈克。它是简单得多,所述WM_CONTEXTMENU消息传递生成该消息的窗口的句柄。所以,你可以简单地把它比作列表视图的句柄。如果不匹配,那么你知道它是头控制:

Relying on the activation state is too hacky. It is far simpler, the WM_CONTEXTMENU message passes the handle of the window that generated the message. So you can simply compare it to the handle of the listview. If it doesn't match then you know it was the header control:

protected override void WndProc(ref System.Windows.Forms.Message m)
{
    base.WndProc(ref m);
    if (m.Msg == 0x7b) {  //WM_CONTEXTMENU
        if (m.WParam != this.Handle) HeaderContextMenu.Show(Control.MousePosition);
    }
}

从技术上讲,你应该使用LVM_GETHEADER但是这应该只是罚款。

Technically you should use LVM_GETHEADER but this should work just fine.

这篇关于ListView控件的ContextMenuStrip的列标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆