C#手动触发MouseDown无法触发MouseUp [英] C# Manually triggered MouseDown fails to trigger MouseUp

查看:579
本文介绍了C#手动触发MouseDown无法触发MouseUp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已经创建了一个Form类和一个CustomControl类。



在我的表单上我有两个CustomControl和一个Panel的实例。 >

面板有4个事件处理程序:MouseEnter(提供不同的游标),MouseLeave(重置游标),MouseDown(启动拖动线程)和MouseUp(杀死拖动线程并执行后拖动逻辑)。



我可以将Panel拖到CustomControl上。当我这样做时,Form中的代码检测到我从Form.Controls中完成并删除了Panel,并将一些元信息传递给CustomControl,然后自定义一个Panel。



基本上,这是一个移交。 Panel对象现在属于CustomControl。
(这是必要的,解释为什么是复杂的,但是想像自定义控件有一些滚动条,Panel必须属于CustomControl,以便它将与CustomControl滚动。)



现在,当我点击CustomControl中的Panel时,面板的MouseDown被触发,它从CustomControl.Controls中删除并发送一些元信息返回到父级(表单),然后重新创建面板,因为它在开始 - 但是已经处于拖动状态,以便用户可以将面板重新定位到第二个CustomControl ,或者可能把它放回到表单上。当Form初始化时创建Panel的功能与现在创建的功能完全相同。



但是,Panel的MouseDown没有被触发。鼠标 下来,但是事件没有触发,因为鼠标在创建时已经关闭。因此,我手动调用Form中的MouseDown处理函数,该函数接受CustomControl中的元信息。



不幸的是,这只是一半。 MouseUp处理程序未触发。我可以从CustomControl中取出Panel,并按预期的方式将其拖动到Form上,但是当我释放鼠标时,Panel被卡在光标上。



I '不确定如何解决这个问题?



一个理想的解决方案是,当元信息被传回到Form并创建新的Panel时, MouseDown事件以某种方式自然地起作用,就像用户刚刚在Panel上点击一样。

解决方案

听起来你正在创建一个新的Panel实例,当您将它从CustomControl移动到Form并返回并失去它的状态。



您应该尝试将Form所有的实例传递给CustomControl,而不会创建一个新的Panel,或者您可以在另一个对象中捕获Panel的状态,您可以在创建新Panel时将其传递给构造函数,使其与所拖动的对象处于同一状态。



似乎您正在尝试手动启动鼠标事件以补偿设计中的问题。



如果可以比冗长的文字说明更好地给出一些示例代码。


Let's say I have a created a Form class and a CustomControl class.

On my Form I have two instances of the CustomControl, and a Panel.

Panel has 4 event handlers: MouseEnter (to give a different cursor), MouseLeave (to reset the cursor), MouseDown (to start the dragging thread), and MouseUp (to kill the dragging thread and do post-drag logic).

I can drag the Panel onto the CustomControl. When I do this, the code in Form detects what I have done and deletes Panel from Form.Controls, passes some meta-information to CustomControl, which then creates a Panel on itself.

Basically, it is a hand-over. The Panel object now belongs to the CustomControl. (This is necessary. It's complicated to explain why, but imagine the custom control has something like scrollbars, and it's necessary for the Panel to belong to the CustomControl so that it will scroll with the CustomControl.)

Now, when I click down on the Panel in the CustomControl, the Panel's MouseDown is triggered, it gets deleted from CustomControl.Controls and sends some meta-information back to the Parent (the Form), which then re-creates the Panel as it was at the start - however already in a dragging state so that the user can re-position the Panel onto the second CustomControl, or perhaps put it back onto the Form. The function which creates the Panel when the Form is first initialised is exactly the same function which creates it now.

However, the Panel's MouseDown has not been triggered. The mouse is down, but the event is not firing because the mouse was already down when it was created. So, I manually call the MouseDown handler in the function in Form which accepts the meta-information from CustomControl.

Unfortunately, this only half-works. The MouseUp handler isn't firing. I can pick up the Panel off the CustomControl and drag it around on the Form as expected, but when I release the mouse, the Panel is stuck to the cursor.

I'm not really sure how to get around this?

An ideal solution would be for, when the meta-information is passed back to Form and the new Panel is created, the MouseDown event to somehow fire naturally as though the user had just clicked down on the Panel.

解决方案

It sounds like you are creating a new instance of Panel when you move it from CustomControl to Form and back and loosing it's state.

You should either try to pass the actual instance owned by Form to CustomControl without creating a new one or you could capture the state of the Panel in another object which you can pass to the constructor when you create a new Panel so that it is in the same state as the one you were dragging?

It seems as though you are trying to manually fire mouse events to compensate for problems in your design.

Always better to give some example code if you can than lengthy textual explanations.

这篇关于C#手动触发MouseDown无法触发MouseUp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆