捕获来自其他应用程序的问题,选定的文本 [英] capture selected text from other applications problem
问题描述
我写的代码,一个小样本在C#中捕捉到从其他应用程序中选择文本:
SendKeys.SendWait(^ C);
串searchedText = Convert.ToString(Clipboard.GetText());
我的问题是,它不工作,如果我捕获从浏览器文本浏览器
有谁知道为什么出现这种情况,或是否有另一种方式做同一个任务
更新
我做这个应用程序从任何其他应用程序捕捉选定的文本,并将其保存在一个文件中,当用户按下一个快捷键。
保护覆盖无效的WndProc(参考System.Windows.Forms.Message米)
{
//让基类处理消息
base.WndProc(REF米);
//如果这是一个WM_HOTKEY消息,通知父对象
const int的WM_HOTKEY = 0x312;
如果(m.Msg == WM_HOTKEY)
{
SendKeys.SendWait(^ C);
串searchedText = Convert.ToString(Clipboard.GetText());
保存(searchedText);
Clipboard.Clear();
}
}
好,我们假定你是在这里,按Ctrl-C总是将文本复制到剪贴板中的每个应用程序。这是一个很大的禁忌,你不知道什么会文本复制到剪贴板。
另外,应该说,使用这种剪贴板是一个非常坏主意,如果你不是专门试图改变剪贴板的内容。看来你只是用它来从其他应用程序复制内容。
话虽这么说,我推荐你使用Microsoft UI自动化库这一点。它的参考是在这里:
的 http://msdn.microsoft.com/en-us/library/ms747327.aspx
也有关于这个问题的好文章在MSDN杂志:
http://msdn.microsoft.com/en-us/magazine/cc163288.aspx
比较特别,如果你正在寻找让选定的文本,然后你想看看 UI自动化的Textpattern概述位于:
的http:// MSDN。 microsoft.com/en-us/library/ms745158.aspx
I wrote a small sample of code in C# to capture selected text from other applications:
SendKeys.SendWait("^c");
string searchedText = Convert.ToString(Clipboard.GetText());
my problem is its not working if i captured text from the browser Chrome anyone know why this happen or if there is another way to do the same task
UPDATE
I am doing this application to capture selected text from any other application and save it in a file when the user press a hot key.
protected override void WndProc(ref System.Windows.Forms.Message m)
{
// let the base class process the message
base.WndProc(ref m);
// if this is a WM_HOTKEY message, notify the parent object
const int WM_HOTKEY = 0x312;
if (m.Msg == WM_HOTKEY)
{
SendKeys.SendWait("^c");
string searchedText = Convert.ToString(Clipboard.GetText());
Save(searchedText);
Clipboard.Clear();
}
}
Well, you are assuming here that Ctrl-C is always going to copy text to the clipboard in every application. That's a big no-no, you don't know what will copy text to the clipboard.
Also, it should be said that using the clipboard for this is a very bad idea if you are not specifically trying to change the contents of the clipboard. It seems you are just using it to copy contents from another application.
That being said, I recommend that you use the Microsoft UI Automation Library for this. The reference for it is here:
http://msdn.microsoft.com/en-us/library/ms747327.aspx
There is also a good article on the subject in MSDN magazine:
http://msdn.microsoft.com/en-us/magazine/cc163288.aspx
Specifcally, if you are looking to get selected text, then you want to look at the UI Automation TextPattern Overview located at:
http://msdn.microsoft.com/en-us/library/ms745158.aspx
这篇关于捕获来自其他应用程序的问题,选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!