Ban*_*ana 15 .net c# google-chrome user32
我正在编写一个小应用程序,除了其他功能外,还可以在键入时将快捷方式扩展为全文.例如:用户在某处写"BNN"并按下相关的键盘组合,应用程序将用"Hi I am Banana"替换"BNN".
经过一些研究,我了解到它可以使用user32.dll,完成这项任务的过程如下:
1)获取活动窗口句柄
2)获取活动窗口线程句柄
3)将输入连接到活动线程
4)获得聚焦控制句柄(+插入位置但不是问题)
5)从活动线程分离输入
6)得到来自聚焦控件的文本使用其句柄
这是我目前的代码:
try
{
IntPtr activeWindowHandle = GetForegroundWindow();
IntPtr activeWindowThread = GetWindowThreadProcessId(activeWindowHandle, IntPtr.Zero);
IntPtr thisWindowThread = GetWindowThreadProcessId(this.Handle, IntPtr.Zero);
AttachThreadInput(activeWindowThread, thisWindowThread, true);
IntPtr focusedControlHandle = GetFocus();
AttachThreadInput(activeWindowThread, thisWindowThread, false);
if (focusedControlHandle != IntPtr.Zero)
{
TB_Output.Text += focusedControlHandle + " , " + GetText(focusedControlHandle) + Environment.NewLine;
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
//...
//...
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
internal static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern int GetWindowThreadProcessId(int handle, out int processId);
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
internal static extern int AttachThreadInput(IntPtr idAttach, IntPtr idAttachTo, bool fAttach);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
internal static extern IntPtr GetFocus();
Run Code Online (Sandbox Code Playgroud)
这适用于一些Windows窗体应用程序,但它不适用于WPF和浏览器,只是给我WPF应用程序的标题或铬标签的标题.
如果我在输入这个问题时在这个页面上运行应用程序,而不是问题的内容,我得到的文字是:
使用我的c#app从谷歌浏览器内部获取文本 - Stack Overflow - Google
可能是因为他们使用图形来渲染元素,我不知道如何进入活动元素并阅读它的文本.
我只提到问题标题中的Web浏览器,因为此工具主要用于Web浏览器.
如有任何反馈,请提前感谢您.
| 归档时间: |
|
| 查看次数: |
751 次 |
| 最近记录: |