5
如何读取使用C#的任何窗口中高亮显示/选择高亮显示文本从任何窗口中的文本。捕获使用C#
我试图2点的方法。
- 每当用户选择一些东西时发送“^ c”。但在这种情况下,我的剪贴板充斥着大量不必要的数据。有时它也复制密码。
所以我换我的方法来第二方法,发送消息的方法。
看到此示例代码
[DllImport("user32.dll")]
static extern int GetFocus();
[DllImport("user32.dll")]
static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
[DllImport("kernel32.dll")]
static extern uint GetCurrentThreadId();
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(int hWnd, int ProcessId);
[DllImport("user32.dll") ]
static extern int GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam);
// second overload of SendMessage
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, uint Msg, out int wParam, out int lParam);
const int WM_SETTEXT = 12;
const int WM_GETTEXT = 13;
private string PerformCopy()
{
try
{
//Wait 5 seconds to give us a chance to give focus to some edit window,
//notepad for example
System.Threading.Thread.Sleep(5000);
StringBuilder builder = new StringBuilder(500);
int foregroundWindowHandle = GetForegroundWindow();
uint remoteThreadId = GetWindowThreadProcessId(foregroundWindowHandle, 0);
uint currentThreadId = GetCurrentThreadId();
//AttachTrheadInput is needed so we can get the handle of a focused window in another app
AttachThreadInput(remoteThreadId, currentThreadId, true);
//Get the handle of a focused window
int focused = GetFocus();
//Now detach since we got the focused handle
AttachThreadInput(remoteThreadId, currentThreadId, false);
//Get the text from the active window into the stringbuilder
SendMessage(focused, WM_GETTEXT, builder.Capacity, builder);
return builder.ToString();
}
catch (System.Exception oException)
{
throw oException;
}
}
这个代码在记事本中工作正常。但是,如果我尝试从另一个应用程序(如Mozilla Firefox或Visual Studio IDE)捕获它,则不会返回文本。
任何人可以帮我,我哪里做错了吗?首先,我选择了正确的方法?
Firefox没有实现UI自动化的要求),我曾试图3.0版本,并没有工作,希望它将来可能会支持。 – 2010-05-04 07:50:55
我们可以做些什么来改善我的第一种方法。这是非常好的工作,除了不必要的拷贝........ – Dinesh 2010-05-04 08:52:43