日期:2014-05-20  浏览次数:20873 次

如何判断ListView控件 右击了 Column
如题


------解决方案--------------------
this.listView1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.listView1_MouseClick);
private void listView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
MessageBox.Show( "right ");
}
}
------解决方案--------------------
这个啊,要复写defwinproc,有一个WM_PARENTNOTIFY消息,捕获这个消息就可以了。前提是你用一个类继承一下listview,复写listview的defwinproc

2)用WM_NOTIFY,这个要思路也是一样,不过不用继承了,直接在winform里面写

象private void listView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
MessageBox.Show( "right ");
}
}
是拿不到的
3)变态的方法,应为colum的高度是固定的22,所以。。。。你可以自己作判断拉
------解决方案--------------------
//比我想象的复杂多了
//如果楼主觉得满意的话别忘记多加几分 :)
//参考如下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication14
{
public struct NMHDR
{
public IntPtr hwndFrom;
public uint idFrom;
public int code;
}
public struct HDHITTESTINFO
{
public Point pt;
public uint flags;
public int iItem;
};

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region API接口 设计 ZswangY37 2007-01-29
private WndProcDelegate wndProcDelegate;
private IntPtr oldWndFunc;
private delegate IntPtr WndProcDelegate(IntPtr hwnd, int Msg, IntPtr wParam, ref NMHDR lParam);
private const int GWL_WNDPROC = -4;
[DllImport( "User32.dll ")]
private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, WndProcDelegate wndProcDelegate);
[DllImport( "User32.dll ")]
private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr wndFunc);
[DllImport( "User32.dll ")]
private static extern IntPtr CallWindowProc(IntPtr prevWndFunc, IntPtr hWnd, int iMsg, IntPtr wParam, ref NMHDR lParam);
[DllImport( "User32.DLL ")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int iParam);
[DllImport( "User32.DLL ")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, ref HDHITTESTINFO iParam);
[DllImport( "User32.DLL ")]
private static extern bool ScreenToClient(IntPtr hWnd, ref Point lpPoint);
private IntPtr wndColumnHeader;
private const uint LVM_FIRST = 0x1000;
private const uint LVM_GETHEADER = LVM_FIRST + 31;
private const int WM_NOTIFY = 0x004E;
private const int NM_FIRST = 0;
private const int HDM_FIRST = 0x1200;
private const int NM_RCLICK = NM_FIRST - 5;
private const int HDM_HITTEST = HDM_FIRST + 6;
private IntPtr ListViewWndProc(IntPtr hWnd, int Msg, IntPtr wParam, ref NMHDR lParam)
{
switch (Msg)
{
case WM_NOTIFY:
if (lParam.hwndFrom == wndColumnHeader)