现在的位置: 首页 > 综合 > 正文

Window编程下ListView“无法获取点击空白处所在组”的完美解决方案

2012年11月28日 ⁄ 综合 ⁄ 共 3226字 ⁄ 字号 评论关闭
感谢兄弟陈振忠提出的技术解决方案!

情况这样的:在Windows编程下,当用ListView控件来进行编程时,我想通过点击空白处来获取该空白处所属的“组”,你遇到过这种情况么?
我的好哥们遇到这种情况了,然后他就想了个解决方案,通过座标来算出空白处所在组。可是仅凭通过座标来判断还不够完善,因为它是可以带有滚动条的,如果滚动滚动条的话再通过座标来判断它属于哪个组则会出现偏差了。后来他想出了通过截获鼠标滚轮事件来判断滚动了几行,然后再相应的做一下数学运算,就完美地实现了以上要求的功能。
以下是他写出的一个重载了ListView的类(此类就拥有此功能)

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using System.Data;
 5using Microsoft.Win32;
 6using System.Windows.Forms;
 7using System.Runtime.InteropServices;
 8
 9using System.Reflection;
10using System.Threading;
11
12using System.ComponentModel;
13
14using Microsoft.Win32.SafeHandles;
15
16namespace WindowsApplication13
17{
18    delegate void DelegeteScrollBottom(int newvalue, int oldvalue);
19    class MyListView2 : ListView
20    {
21        public MyListView2()
22        {
23
24        }

25        ///   <summary > 
26        ///   水平滚动事件 
27        ///   </summary > 

28        public event DelegeteScrollBottom Scroll;
29        private const int WM_HSCROLL = 0x114;
30        private const int WM_VSCROLL = 0x0115;
31        private int OldVscrollValue = 0;
32
33        [DllImport("user32")]
34        public static extern int GetScrollPos(IntPtr hWnd, int nBar);
35        [DllImport("user32")]
36        public static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);
37        [DllImport("User32.dll", EntryPoint = "SendMessage")]
38        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);   
39
40        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
41        public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
42
43
44   
45
46        protected override void WndProc(ref   Message m)
47        {
48            int NewVscrollValue = GetScrollPos(m.HWnd, 1);
49
50            switch (m.Msg)
51            {
52                case WM_VSCROLL:
53
54                    //int NewVscrollValue = GetScroiliofo(m.HWnd, 1);
55
56                    if (Scroll != null)
57                    { Scroll(NewVscrollValue, OldVscrollValue); }
58
59                    OldVscrollValue = NewVscrollValue;
60                    base.WndProc(ref   m);
61                    break;
62
63                //case 0x020A:
64                //    //NewVscrollValue = GetScroiliofo(m.HWnd, 1);
65                //    //if (NewVscrollValue <= 0)
66                //    //{
67                //    //    SetScrollPos(m.HWnd, 1, 63, true);
68                //    //}
69                //    NewVscrollValue = GetScrollPos(m.HWnd, 1);
70
71                //    if (Scroll != null)
72                //    {
73
74                //        Scroll(NewVscrollValue, OldVscrollValue);
75
76                //    }
77
78                //    OldVscrollValue = NewVscrollValue;
79                //   // NewVscrollValue = GetScrollPos(m.HWnd, 1);
80
81                //    base.WndProc(ref   m);
82                //    break;
83                //    //Message msg;
84
85                //    ////msg = Message.Create(m.HWnd, WM_VSCROLL, new IntPtr(-1), new IntPtr(-1));
86                //    //SendMessage(m.HWnd, WM_VSCROLL, 1, 0);
87
88                //    break;
89                default:
90
91                    base.WndProc(ref   m);
92                    break;
93            }

94        }

95    }

96}

97
98

抱歉!评论已关闭.