作为一个选项,您可以覆盖
DefWndProc
和手柄
WM_LBUTTONDOWN
. 然后执行命中测试并检查单击的点是否为项目,恢复
Selected
项目属性:
public class MyListView : ListView
{
const int WM_LBUTTONDOWN = 0x0201;
protected override void DefWndProc(ref Message m)
{
if (m.Msg == WM_LBUTTONDOWN)
{
int x = (m.LParam.ToInt32() & 0xffff);
int y = (m.LParam.ToInt32() >> 16) & 0xffff;
var item = this.HitTest(x, y).Item;
if (item != null)
item.Selected = !item.Selected;
else
base.DefWndProc(ref m);
}
else
{
base.DefWndProc(ref m);
}
}
}