Getting selected item in active window

2020-07-30 04:13发布

问题:

I am using C# to develop an application that works with the clipboard, an was wondering if its possible to get the contents of what the user has selected in the current window, no matter what window it is (e.g. could be FireFox, notepad, etc.).

回答1:

I'm not sure if you're going to be able to get a general solution for this problem. Take a look at this link;

http://www.eggheadcafe.com/software/aspnet/33899121/get-selected-text-of-active-window-.aspx

The author of this post reports that their solution works in Notepad, but not in Wordpad or Word. I don't know much about this area, but I would guess that this means there are various means through which text selection is implemented - even within Windows.

I think this also extends to Control-C, not just text selection. I believe that there are different ways of handling the copy command depending on the context in which it's used.

For example, if you press Control-C on a dialog box in Windows, it will copy the text without you have to select it manually.

Hopefully the link above is of some use to you. This seems like an interesting problem to overcome.

Edit:

Spoke too soon - just found this link;

http://social.msdn.microsoft.com/Forums/en/winformsapplications/thread/1dc356e6-9441-44de-9eda-247003fa6ef5

Looks like this is exactly what you're after - hope that works.



回答2:

If something was only selected, it won't appear in the Clipboard. But if user copied some object that he had previously selected, then, yes, it will be in the Clipboard.