This code will help you get focused control text in a focused window, I hope this helps:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace TextFocusedns { public partial class TextFocusedFrm : Form { #region APIs [DllImport("user32.dll")] public static extern bool GetCursorPos(out Point pt); [DllImport("user32.dll", EntryPoint = "WindowFromPoint", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern IntPtr WindowFromPoint(Point pt); [DllImport("user32.dll", EntryPoint = "SendMessageW")] public static extern int SendMessageW([InAttribute] System.IntPtr hWnd, int Msg, int wParam, IntPtr lParam); public const int WM_GETTEXT = 13; [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] internal static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] internal static extern IntPtr GetFocus(); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] internal static extern int GetWindowThreadProcessId(int handle, out int processId); [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] internal static extern int AttachThreadInput(int idAttach, int idAttachTo, bool fAttach); [DllImport("kernel32.dll")] internal static extern int GetCurrentThreadId(); [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)] internal static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount); #endregion private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer() { Interval = 100, Enabled = true }; public TextFocusedFrm() { InitializeComponent(); } private void TextFocusedFrm_Load(object sender, EventArgs e) { timer.Tick += new EventHandler(timer_Tick); timer.Start(); } void timer_Tick(object sender, EventArgs e) { try { MultiLineTextBox.Text = GetTextFromFocusedControl(); } catch (Exception exp) { MultiLineTextBox.Text += exp.Message; } }
source share