我使用

问题描述 投票:0回答:1
事件来移动无边界形式(如下所示)。

它效果很好,但是对于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
.
c# .net winforms listview
1个回答
2
投票
WM_LBUTTONDOWN

时,如果仍按下左按钮时,则在fem上移动表单(当前按下的按钮是在WM_MOUSEMOVE中指定的,请参见有关此的文档)。

使用
Application.AddMessageFilter
注册实现接口的类(在这种情况下为表单本身)。在这里,它被称为WParam。 调用

application.removemessagefilter

删除过滤器。在这里,在

OnHandleCreated
中被称为。
注意我在
OnHandleDestroyed
中使用了
Capture = true;

,因此按左鼠标按钮并拖动,例如,按钮控件,不会导致 - 在这种情况下 - 单击事件。 如果您不喜欢它,请修改它。

注: 正如Reza Aghaei建议的那样,如果将listView设置为WM_MOUSEMOVE,则可以单击其上的任何位置以拖动表单。

MultiSelect = false

我也想在Mousedown期间从ListView捕获一个Mousemove事件,以使其能够执行表单移动操作。所有其他控件(例如Picturebox和Label等)返回Mousemove事件,因此用户可以单击任何控件并将表单拖动。但不是ListView!不喜欢过度复杂的解决方案来解决简单的问题。
I发现设置“ ListView.Multiselect = false”未解决问题。但是经过大量的实验,我在“ ListView.fullrowselect = true”方面取得了成功。设置此并瞧...现在,Mousemove事件已由ListView捕获。
    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.