是否可以编写一个windows应用程序,当在另一个窗口应用程序中选择文本时,该应用程序会收到通知
Is it possible to write a windows application that gets a notification when text is selected in another windows application?
我很好奇是否可以编写一个程序来监控我的文本选择。一种可能的用途是编写一个编辑器/IDE不可知的代码格式化程序:
- 应用程序/服务P启动并以某种方式挂接到窗口,以便在任何窗口中选择文本时都会收到通知
- 启动了另一个应用程序A
- 用户选择A中的文本
- P会收到所选文本的通知
-->我很高兴能走到这一步。。。
如果不了解将要使用的每个控件/应用程序,这是不可能的,因为它们都可以以不同的方式处理/处理它。
我认为您不能注册任何类型的钩子。我认为你需要不断地对"聚焦"或选定的窗口进行民意调查。
您可能可以使用Windows Automation API来实现这一点,据我所知,这超过了旧的Accelability API:http://msdn.microsoft.com/en-us/library/ms747327.aspx
我已经使用这个API来自动化GUI测试。我对它有点生疏,所以我不确定,但我有理由相信你可以将它用于你正在尝试做的事情。基本上,API允许你遍历自动化对象的树,根在桌面上。每个自动化元素往往是某种类型的窗口控件,不同的控件实现不同的模式。您还可以访问鼠标光标下的元素,也可以直接访问当前选定/聚焦的元素。
之后,我注意到例如TextPattern类有一个GetSelection()方法,它被记录为"检索与当前文本选择相关联的不相交文本范围的集合。"我打赌文本框的自动化对象实现了TextPattern。http://msdn.microsoft.com/en-us/library/system.windows.automation.textpattern.aspx
这段代码可以帮助您在聚焦窗口中获得聚焦控件文本,我希望这能有所帮助:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace TextFocusedns
{
public partial class TextFocusedFrm : Form
{
#region APIs
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out Point pt);
[DllImport("user32.dll", EntryPoint = "WindowFromPoint", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr WindowFromPoint(Point pt);
[DllImport("user32.dll", EntryPoint = "SendMessageW")]
public static extern int SendMessageW([InAttribute] System.IntPtr hWnd, int Msg, int wParam, IntPtr lParam);
public const int WM_GETTEXT = 13;
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
internal static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
internal static extern IntPtr GetFocus();
[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(int idAttach, int idAttachTo, bool fAttach);
[DllImport("kernel32.dll")]
internal static extern int GetCurrentThreadId();
[DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount);
#endregion
private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer() { Interval = 100, Enabled = true };
public TextFocusedFrm()
{
InitializeComponent();
}
private void TextFocusedFrm_Load(object sender, EventArgs e)
{
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
try
{
MultiLineTextBox.Text = GetTextFromFocusedControl();
}
catch (Exception exp)
{
MultiLineTextBox.Text += exp.Message;
}
}
//Get the text of the focused control
private string GetTextFromFocusedControl()
{
try
{
int activeWinPtr = GetForegroundWindow().ToInt32();
int activeThreadId = 0, processId;
activeThreadId = GetWindowThreadProcessId(activeWinPtr, out processId);
int currentThreadId = GetCurrentThreadId();
if (activeThreadId != currentThreadId)
AttachThreadInput(activeThreadId, currentThreadId, true);
IntPtr activeCtrlId = GetFocus();
return GetText(activeCtrlId);
}
catch (Exception exp)
{
return exp.Message;
}
}
//Get the text of the control at the mouse position
private string GetTextFromControlAtMousePosition()
{
try
{
Point p;
if (GetCursorPos(out p))
{
IntPtr ptr = WindowFromPoint(p);
if (ptr != IntPtr.Zero)
{
return GetText(ptr);
}
}
return "";
}
catch (Exception exp)
{
return exp.Message;
}
}
//Get the text of a control with its handle
private string GetText(IntPtr handle)
{
int maxLength = 512;
IntPtr buffer = Marshal.AllocHGlobal((maxLength + 1) * 2);
SendMessageW(handle, WM_GETTEXT, maxLength, buffer);
string w = Marshal.PtrToStringUni(buffer);
Marshal.FreeHGlobal(buffer);
return w;
}
}
}
相关文章:
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- QT 样式表主题,适用于使用属性选择器的整个应用程序
- 如何在 DirectX 11 应用程序中提供选择图形适配器的选项?
- 如何为 QtQuick 应用程序选择 OpenGL 上下文
- 将Objective-C应用程序移植到Windows.什么是最好的选择
- QT 4.x/5.x和OpenGL用于桌面GUI应用程序:选择哪种模块
- 语言选择:适用于Windows应用商店应用程序的C++或C#
- 想要在Web应用程序中使用本机C库,我的选择是什么
- QT/C++,OS X:退出应用程序时可选择隐藏(),以便应用程序继续运行,并在单击dock中的图标后再次显示
- Mac应用程序开发:如何在安装时选择应用程序语言
- 可以选择在 C++ 中写入控制台的 Windows 应用程序
- 是否可以编写一个windows应用程序,当在另一个窗口应用程序中选择文本时,该应用程序会收到通知
- 选择从Windows将文件拖放到GLFW应用程序中
- openFrameworks是开发桌面gui应用程序的好选择吗
- 基于CListView的SDI应用程序中的选择更改事件
- 我应该选择什么样的同步方案来避免在实时,c++ /MFC应用程序上的死锁
- 在桌面应用程序中嵌入谷歌地球而不是网站.QT是一个真正的选择吗?
- 我应该选择什么类型的Visual Studio c++项目来制作基本的unix应用程序?
- 通过选择"退出"菜单项关闭应用程序 - wxWidgets 3.0
- Qt 5.0——它是3D游戏/应用程序开发的可行选择吗?