开发者

How to trap Drag/Drop event in Word?

I am creating an Add-in (using C#) for Microsoft Word and want to trigger some functionality when some text/image etc. is dragged 开发者_JAVA百科from any source and dropped into Word. So basically I want to trap the Drop event in Word. However I am not able to find any Word API which helps in this. Can you please help in this?


No such beast exists. The best (easy-ish) approach you can hope for is to watch the DocumentChange event, and try to monitor what changed (it'll be located at the current location of the "Selection").

A much hardy way would be to subclass the main Word Window and watch for whatever Drag/drop messages are being exchanged, and intercept them as appropriate.


The SelectionChange event can sometimes be used for this. Sometimes, because the selection will not always contain the dropped content. E.g. if you drop an image into a document, the image will not be selected.


A better alternate is to use a transparent window that you overlay on top of the Word window. This window is initiated when the drag process starts and placed on top of the document window. The drop operation is then received by this window instead of Word, so you know exactly what came in and at which location it was dropped. You can then translate these location coordinates into corresponding document location (using RangeFromPoint() method of Window class) and do whatever you need to do with the dropped data. After this you simply hide your transparent window and everything goes back to normal.

A complete implementation of this approach along with source code is available in this excellent MSDN article.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜