如何判断ListView控件 右击了 Column

aierduo 2007-01-29 07:00:28
如题
...全文
185 点赞 收藏 8
写回复
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
aierduo 2007-01-29
谢谢
有没有简单一点的
回复
王集鹄 2007-01-29
//比我想象的复杂多了
//如果楼主觉得满意的话别忘记多加几分 :)
//参考如下代码:

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)
switch(lParam.code)
{
case NM_RCLICK:
Point vPoint = new Point(MousePosition.X, MousePosition.Y);
ScreenToClient(wndColumnHeader, ref vPoint);
HDHITTESTINFO vTestInfo = new HDHITTESTINFO();
vTestInfo.flags = 0;
vTestInfo.iItem = 0;
vTestInfo.pt = vPoint;
SendMessage(wndColumnHeader, HDM_HITTEST, 0, ref vTestInfo);
Text = vTestInfo.iItem.ToString();
break;
}
break;
}
return CallWindowProc(oldWndFunc, hWnd, Msg, wParam, ref lParam);
}
#endregion

private void Form1_Load(object sender, EventArgs e)
{
wndColumnHeader = SendMessage(listView1.Handle, LVM_GETHEADER, 0, 0);
wndProcDelegate = new WndProcDelegate(ListViewWndProc);
oldWndFunc = SetWindowLong(listView1.Handle, GWL_WNDPROC, wndProcDelegate);
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
SetWindowLong(listView1.Handle, GWL_WNDPROC, oldWndFunc);
oldWndFunc = IntPtr.Zero;
}
}
}
回复
liu2002081319 2007-01-29
恩,固定Column 的高度和宽度,拿到e的点击位置坐标,进行比较,可以确定,比较极端。。。。。
回复
superxiaomm 2007-01-29
这个啊,要复写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,所以。。。。你可以自己作判断拉
回复
aierduo 2007-01-29
好的
回家后再看谢谢
回复
王集鹄 2007-01-29
先顶,我现在再写代码
回复
aierduo 2007-01-29
自己顶
回复
liu2002081319 2007-01-29
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");
}
}
回复
发动态
发帖子
C#
创建于2007-09-28

10.4w+

社区成员

.NET技术 C#
申请成为版主
社区公告

全世界最好的语言,没有之一.