首页  编辑  

ListView捕获列右键点击

Tags: /C#/界面处理/ListView_TreeView/   Date Created:

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 WindowsApplication1

{

  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 API2007-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;

       }

   }  

}