简体   繁体   English

从活动窗口中获取突出显示的文

[英]Get highlighted text from active window

I would like to know how can I get highlighted text from any window for example: (excel, ie, firefox,…). 我想知道如何从任何窗口获取突出显示的文本,例如:(excel,即firefox,...)。 please note that the following message not work in the above application WM_GETTEXT,WM_COPY,EM_GETSELTEXT. 请注意以下消息在上述应用程序WM_GETTEXT,WM_COPY,EM_GETSELTEXT中不起作用。

I have also tried control C (copy) and get selected text from clipboard but it is not a good idea. 我也尝试过控制C(复制)并从剪贴板中获取所选文本,但这不是一个好主意。

Language used: C# 使用的语言:C#

No answers huh? 没有答案吧? Well, I know you can get it from Excel, Word etc using interop. 好吧,我知道你可以使用互操作从Excel,Word等中获取它。 Look into that. 看看那个。 It might give you som ideas on how to proceed with ie and ff. 它可能会给你关于如何继续ie和ff的som想法。 But basically the recieving application must have some sort of fascility for letting you do this and I don't think there's any general way which works all the time. 但基本上接收应用程序必须有一些让你这样做的能力,我不认为有任何一般的方法可以一直工作。

There is no general purpose answer to this question. 这个问题没有通用的答案。 Each window class will have a different solution. 每个窗口类都有不同的解决方案。

For instance, if the hilighted text is in an edit window, then you can use EM_GETSEL to get the range of the selection, then WM_GETTEXT to get the text (and then throw the unselected part a way) or EM_LINEFROMCHAR to turn that range into line indexes, and then EM_GETLINE to get the selected text one line at a time. 例如,如果高亮显示的文本位于编辑窗口中,则可以使用EM_GETSEL获取选择范围,然后使用WM_GETTEXT获取文本(然后将未选择的部分抛出一个方向)或EM_LINEFROMCHAR将该范围转换为直线索引,然后EM_GETLINE获取所选文本一行。

But this won't work for any other window class. 但这不适用于任何其他窗口类。

I haven't tried it myself, but the Microsoft UI Automation API should have the functionality that you need. 我自己没有尝试过,但Microsoft UI Automation API应该具备您需要的功能。

The UI Automation API is what you would use if you were building a screen reader to assist blind people. 如果您构建屏幕阅读器以帮助盲人,则可以使用UI Automation API。 So it should definitely be able to access the selected text in an arbitrary application. 所以它绝对应该能够在任意应用程序中访问所选文本。

A good place to start would be with the "Text Pattern Overview" at http://msdn.microsoft.com/en-us/library/ms745158.aspx 一个好的起点是http://msdn.microsoft.com/en-us/library/ms745158.aspx上的“文本模式概述”。

Also keep your eye on question 517694 . 另请密切关注问题517694 I think you'll find that answers to that question will solve your problem. 我想你会发现这个问题的答案可以解决你的问题。

No need to write this in C# from scratch. 无需从头开始在C#中编写它。 What's wrong with using the clipboard? 使用剪贴板有什么问题? This script ensures that it restores what was on the clipboard when it has finished. 此脚本确保它在完成后恢复剪贴板上的内容。

Autohotkey makes this much simpler. Autohotkey使这更加简单。

; Hotkey:  Ctrl Shift t

^!t::

; Remember what was in the clipboard
clipboardPrev = %clipboard%

; Clear the clipboard
clipboard:=

Sleep,200

; Send a Ctrl C to copy the current selection
SendInput, {Ctrl down}c{Ctrl up}

Sleep,200

; Get the current selection from the clipboard
selectedText=%Clipboard%

if SelectedText =
{
    ; If the first attempt didn't get any test, try again
    Sleep,200

    ; Send a Ctrl C to copy the current selection
    SendInput, {Ctrl down}c{Ctrl up}

    ; Get the current selection from the clipboard
    selectedText=%Clipboard%

}

; Restore the clipboard
clipboard=%clipboardPrev% 

MsgBox, %selectedText%

return

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM