2013-06-24 54 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我不能让旧的应用程序太的代码...

所以有修复,这是工作....