C# , detect selected text on windows?
c#, text
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.
Problem
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.