我正在開發一個通用應用程序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提前。