c# - Get active window text (and send more text to it) -


I am creating a small feature in C #, which, when pressed a global hotkey, in the active text box It will add some text, it is a type of auto complete function. I am working on my global hotkey, but now I do not know how to get the existing text in the active text box (if the active window is a text box). I have tried to use it so far Is

. GetForegroundWindow and then GetWindowText using the caller handles. This gave me the window title of the active window, not the textbox content.

b GetActiveWindow and GetWindowText using that handle to call it does not give me any text.

Here is an example of what I did

  [DllImport ("user32.dll")] Personal Static Extra Bull UnregisterHotKey (Interrupted HWN, Int IP); [DllImport ("user32.dll")] stable extern int GetForegroundWindow (); [DllImport ("user32.dll")] Static extern c GetWindowText (int hWnd, StringBuilder text, int count); [DllImport ("user32.dll")] stable extern int GetActiveWindow (); Public Static Zero Test A () {int h = GetForegroundWindow (); Stringbuilder b = new string builder (); GetWindowText (H, B, 256); MessageBox.Show (b.ToString ()); } Public static void TestB () {int h = GetActiveWindow (); Stringbuilder b = new string builder (); GetWindowText (H, B, 256); MessageBox.Show (b.ToString ()); }  

So, any ideas about how to achieve this?

Edit on 28.01.2009: So, I came to know how to do this. This is what I've used:

  using the system; Using System.Text; Using System.Runtime.InteropServices; Public class examples {[DllImport ("user32.dll")) static extern int GetFocus (); [DllImport ("user32.dll")] Static Extra Bull AttachDaddyPutput (UIT IDAtach, UIT IDetate to, Bull F.Hatch); [DllImport ("kernel32.dll")] Fixed Extern uint GetCurrentThreadId (); [DllImport ("user32.dll")] static extern ut gatwandotrodprocideID (int hwnd, int processID); [DllImport ("user32.dll")] stable extern int GetForegroundWindow (); [DllImport ("user32.dll", Chargset = Charset.auto, Setlist error = false)) Stable extern int sendmessage (int hWnd, int message, int wParam, stringbuilder lParam); Const int WM_SETTEXT = 12; Const int WM_GETTEXT = 13; Public Static Zero Main () Waiting for 5 seconds to give us a chance to focus on some editing windows, for example System Note 3. threading. Thread Sleep (5000); Stringbuilder builder = new string builder (500); Int foregroundworldly = gatemoorgroundwondo (); Uint remoteThreadId = GetWindowThreadProcessId (foreground windowHandle, 0); UIT truetredIDid = GetCurrentThreadId (); // AttachTrheadInput is required so that we can get handle of the focus window in any other App AttachThreadInput (RemoteTrade, Current or Path, True); // Get the focus of a focused window INT focused = GetFocus (); // now separately since we got AttachThreadInput handle focus (remote transread, current thread ID, wrong); // Receive the text in the text stringbilder Send Message (Focus, WM_GETEactor, Builder. Capacity, Builder) from the active window; Console.light line ("text in active window" was + creator); Builder. Append ("extra text"); // Change the text in Active Window SendMessage (Centered, WM_SETTEXT, 0, Builder); Console.ReadKey (); }}  

Some notes about this wait for 5 seconds before doing anything example, gives you a chance to focus on some editing windows. In my actual app I am using a hotkey to trigger it, but it will confuse this example. In addition, in the production code, you should check the return value of the Win32 call to see if they have succeeded or not.

If you know about the active window and the focused input field then it is appropriate to send keystrokes See for the API.


Comments