我正在开发一个通用应用程序c#其中我想从任何活动窗口的文本。当用户按热键时,我的应用程序将启动活动的Windows文本数据。从活动窗口访问数据
这是一个拼写检查器应用程序。 我的问题是如何从Microsoft Document访问文本。 我尝试了所有system32.dll功能
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, IntPtr msg,
int wParam, int lParam);
[DllImport("kernel32.dll")]
static extern uint GetCurrentThreadId();
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(int hWnd, int ProcessId);
System.Threading.Thread.Sleep(1000);
StringBuilder builder = new StringBuilder(500000);
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);
Console.WriteLine("Text in active window was " + builder);
builder.Append(" Extra text");
//Change the text in the active window
SendMessage(focused, WM_SETTEXT, 0, builder);
//Console.ReadKey();
Console.Read();
但是这将只从记事本,浏览器地址栏,写字板等。 返回文本但实际上它不是微软的产品那样工作的MS Office,excel..etc。
我想从任何活动窗口进行拼写检查的文本任何人都可以帮助我。
Thanx提前。