Check if a drag&drop is in progress

北战南征 提交于 2019-12-12 18:34:01

问题


Is there any way to check if a drag and drop is in progress? Some method or win32 api which can be checked? I know I can set AllowDrop and use events but it doesn't work in this case. Basically i want to check, with code, if any drag&drop is in progress.


回答1:


I had a similar question which I answered myself (after some hours messing about) See - How do I tell if a Drag Drop has ended in Winforms?.

Basically if you do as earwicker suggests you need to set the flag when the drag drop begins near the DoDragDrop call. You will need to unset the flag in both the DragDrop event and in the QueryContinueDrag if the QueryContinueDragEventArgs indicate a drop or a cancel.




回答2:


The GetCapture API function might be a good start. Basically, when a drag operation starts, the source window "captures" the mouse, which means that it will still receive all mouse events even if the mouse leaves the window.

However, applications can also capture the mouse for other reasons, so this is not 100% reliable. You can try it and see how well it works for you. And with applications doing their own drag&drop handling, there's no way to be sure what is going on anyway.




回答3:


What about QueryContinueDrag event handler http://msdn.microsoft.com/en-us/library/system.windows.forms.control.querycontinuedrag.aspx ? You can hook a handler to any control and check if there is a ongoing drag&drop operation and then cancel it if you want to.

Ooops, sorry, I just saw that the guy before me already mentioned that. Me bad.




回答4:


Assuming it's in the context of just your own code, you could identify all the places in your code where a drag/drop happens, and set a global boolean flag to true for the duration of the operation, then back to false after it finishes.

So the next question is, how are drag/drop operations being started in your application?



来源:https://stackoverflow.com/questions/373873/check-if-a-dragdrop-is-in-progress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!