2013-06-24 52 views
0

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

回答

0

你不能從任何應用程序中的文本...但你可以開發的擊鍵應用程序,它可以從任何窗口得到的擊鍵,並將其保存到您的應用程序....我開發的C#但擊鍵應用工作不正常......它檢測到任何窗口的按鍵,但在大寫鎖定按鍵被按下時會受到干擾....

0

不,這是可能的。 因爲我的客戶給我他的舊應用程序,將工作.. 它將與文字工作,EXCEL,PPT,記事本,寫字板等。 但現在他想它通用那種.. 他給我的.exe文件...所以我不能檢查代碼.. 而不存在的情況,我向他要舊的應用程序code..so我不能讓舊的應用程序太的代碼...

所以有修復,這是工作....