使用.Net检测外部应用程序中的拖放操作 [英] Detect drag and drop operations in an external application using .Net

查看:220
本文介绍了使用.Net检测外部应用程序中的拖放操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在外部应用程序中检测拖放操作,是可能吗?我想起了一个钩子来检测这些操作。任何人都可以指出我正确的方向,尽可能的消息或api功能,我可以尝试钩钩检测拖&放下事件?我想在C#中这样做。



提前感谢

解决方案

p>有两个明显的路径,你可以尝试这个。



第一个是挂钩涉及调用拖放操作的OLE函数。这假设其他应用程序正在使用真正的拖放,而不是一些仅内部版本。确定这是超出我的能力 - 无需访问程序 - 但是如果可能从该外部应用程序拖动到其他应用程序窗口(包括桌面),它很好地表明应用程序正在使用真实的东西。



您需要挂钩的功能是 RegisterDragDrop ,虽然 DoDragDrop 也可能是感兴趣的。 #code> RegisterDragDrop 被调用来注册一个放置目标,并给你一个机会,在你需要的maner中包装 IDropTarget 可能IPC进入你的实际应用程序),RevokeDragDrop然后通过注册,但现在与你的包装对象。 IDropTarget :: Drop 大概是你的你会做大部分的IPC。



第二个是通过在应用程序中注入一个Wi​​ndows钩子。如果您了解触发拖动的用户操作,并可以检测到它们,那么您只需要区分拖动操作和常规键盘/鼠标操作即可。所涉及的钩子是 WH_CALLWNDPROC 钩子,已注册到 SetWindowsHookEx 。基本上,您定义一个 WndProc ,它可以获取应用程序窗口执行的所有消息(包括鼠标移动),并由您决定拖动操作何时启动以及何时停止。



在这两种情况下,很多代码都可以用C#编写。使用p \invoke可以查看 SetWindowsHookEx ,您可以实现COM C#中的对象(以及这些COM对象的扩展名)。实际上,已经有一个 IDropTarget 课程.NET 2.0;这可能是COM可见的。



不幸的是,挂钩的API是非常低的水平,你通常需要使用C或C ++来完成任何事情。我听说过有关绕行者以及微软的好消息。 / p>

I need to detect drag and drop operations in an external application, is it possible? I thought of writing a hook to detect these operations. Can anybody point me in the right direction as far as which messages or api functions I could attempt to hook to detect the Drag & Drop events? I want to do this in C#.

Thanks in advance.

解决方案

There are two obvious paths you can go down to attempt this.

The first, is to hook the OLE functions involved in mediating drag/drop operations. This assumes that the other application is using real drag and drop and not some internal only version. Determing this is beyond my ability - without access to the program - but if its possible to drag from this external application to some other applications window (including the desktop) its a good indication that the app is using the real stuff.

The function you'd need to hook is RegisterDragDrop, though DoDragDrop may also be of interest. RegisterDragDrop is called to register a drop target, and gives you an opportunity to wrap the IDropTarget in the maner you desire (presumably IPC'ing into your actual app), RevokeDragDrop and then pass the registration on, but now with your wrapper object instead. IDropTarget::Drop is presumably where you'll do the bulk of your IPC.

The second, is by injecting a Windows hook into the application. This is only tractable if you understand the user actions that trigger a drag, and can detect them as you're going to have to distinguish between a drag operation and normal keyboard/mouse actions. The hook in question is the WH_CALLWNDPROC hook, registered with SetWindowsHookEx. Basically, you define a WndProc which gets all the messages the applications window does (including mouse movement) and its up to you to determine when a drag operation starts and when it stops.

In either case, alot of the code can be written in C#. With p\invoke you can get at SetWindowsHookEx, and you can implement COM objects (and by extension, wrappers for those COM objects) in C# as well. Infact, there's an IDropTarget class already in .NET 2.0; which might be COM visible already.

Unfortunately, hooking APIs is very low level and you're generally going to have to work in C or C++ to get anything done. I've heard good things about Detours, plus its from Microsoft.

这篇关于使用.Net检测外部应用程序中的拖放操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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