禁止的ListView具有零所选项目

问题描述:

我的项目是.NET / WinForms的。

My project is .NET/WinForms.

我有一个总是充满着项目的列表视图。我想有选择它始终。不过,如果我点击下面列表视图项的空白区域,它失去的选择。

I have a list view which is always filled with items. I would like it to have selection always. However, if I click on an empty area below list view items, it looses selection.

该列表中有多个选择=真正的和隐藏的选择=假的。

The list has multiple selection = true and hide selection = false.

您需要prevent从看到鼠标的点击,所以它不会取消选择一个项目的本地控制权。添加一个新类到您的项目并粘贴如下所示的code。编译。从工具箱到窗体顶部砸,替换现有之一。

You need to prevent the native control from seeing the mouse click so it won't unselect an item. Add a new class to your project and paste the code shown below. Compile. Drop it from the top of the toolbox onto your form, replacing the existing one.

using System;
using System.Drawing;
using System.Windows.Forms;

class MyListView : ListView {
    protected override void WndProc(ref Message m) {
        // Swallow mouse messages that are not in the client area
        if (m.Msg >= 0x201 && m.Msg <= 0x209) {
            Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
            var hit = this.HitTest(pos);
            switch (hit.Location) {
                case ListViewHitTestLocations.AboveClientArea :
                case ListViewHitTestLocations.BelowClientArea :
                case ListViewHitTestLocations.LeftOfClientArea :
                case ListViewHitTestLocations.RightOfClientArea :
                case ListViewHitTestLocations.None :
                    return;
            }
        }
        base.WndProc(ref m);
    }
}