它效果很好,但是对于listView,它仅在我单击列表中的项目(文本)时才有效。如果我单击包含任何项目的ListView的空间,它就不起作用。
有一种解决此问题的可能方法吗?
private bool mouseDown;
private Point lastLocation;
private void ListView1_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
lastLocation = e.Location;
}
private void ListView1_MouseMove(object sender, MouseEventArgs e)
{
if(mouseDown)
{
this.Location = new Point(
(this.Location.X - lastLocation.X) + e.X, (this.Location.Y - lastLocation.Y) + e.Y);
this.Update();
}
}
private void ListView1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
要移动表单,单击和拖动任何控件,您可以实现ImessageFilter接口
。您将在发送到目标控件之前收到消息(可以选择抑制它们,返回
true
)。 实施要求您实施
Prefiltermessage.。
WM_LBUTTONDOWN
时,如果仍按下左按钮时,则在fem上移动表单(当前按下的按钮是在WM_MOUSEMOVE
中指定的,请参见有关此的文档)。
使用Application.AddMessageFilter
WParam
。
调用application.removemessagefilter删除过滤器。在这里,在
OnHandleCreated
中被称为。
注意我在
OnHandleDestroyed
中使用了
Capture = true;
,因此按左鼠标按钮并拖动,例如,按钮控件,不会导致 - 在这种情况下 - 单击事件。
如果您不喜欢它,请修改它。
注:
正如Reza Aghaei建议的那样,如果将listView设置为WM_MOUSEMOVE
,则可以单击其上的任何位置以拖动表单。
MultiSelect = false
I发现设置“ ListView.Multiselect = false”未解决问题。但是经过大量的实验,我在“ ListView.fullrowselect = true”方面取得了成功。设置此并瞧...现在,Mousemove事件已由ListView捕获。