Question

I would make a tools like Google toolbar translate function, but it is for desktop. What i want to do is highlight the text in any application (word,pdf,live messenger etc) , and translate by google translate api ,return as a tool tips.

I have search msdn about monitoring text, i only found using copy&paste and monitoring clipboard to tick the event.

so, any idea about that? thanks you.

Was it helpful?

Solution

A starting point would be to get a reference to the current foreground window. The code below will get the currently selected window and the title of that window:

[ DllImport("user32.dll") ]

static extern int GetForegroundWindow();

[ DllImport("user32.dll") ]
static extern int GetWindowText(int hWnd, StringBuilder text, int count); 

private void GetActiveWindow()
{

const int nChars = 256;
int handle = 0;
StringBuilder Buff = new StringBuilder(nChars);

   handle = GetForegroundWindow();

   if ( GetWindowText(handle, Buff, nChars) > 0 )
   {
   this.captionWindowLabel.Text = Buff.ToString();
   this.IDWindowLabel.Text = handle.ToString();
   }

}

You could run this code within a timer: i.e give the user 10 seconds to select a window.

I am not sure how you would retrieve selected text within a window, but I will look into it for you.

OTHER TIPS

I think you'll need to start by getting the handle of any window that is activated when your program is active. My guess is you need to look into InteropServices here to do this.

Using Windows API.

It sounds like you need to have your code intercept any window handle of any process, this is where it gets a bit complex as you have to ensure you do have access permissions to access another process.

Speaking of which, I do not think it is a good idea as you could end up crashing another process by poking around under the hood in regards to the winapi calls to trap the text selection event, not too mention the fact that you would have to determine if the process has any text selected. The best direction I can give is this...an article was written on how to spy on a process on CodeProject here, this can be a step in the right direction, bear in mind that the code used was for the .NET 1.0 framework.

Hope this helps and good luck in your coding, Best regards, Tom.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top