How to get SelectedText from other applications?

優貴 森 6 Reputation points
2022-05-08T15:39:28.373+00:00

I'm looking for a way to get SelectedText from ForegroundWindow on WPF .NET.
What I want to get is the selected text within another application.
I'm trying to search, but I can't find the technique I'm looking for, only how to get SelectedText in WPF.
I got the ThreadProcessId using user32.dll, but I don't know what to do from here.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,012 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,571 Reputation points Microsoft Vendor
    2022-05-09T06:32:32.29+00:00

    This code help you to get focused control text in focused window, i hope that helps :

    1. Gain focus control. You could get the currently activated window through the GetForegroundWindow API, and then call the GetWindowThreadProcessId API to get the thread id of the current window. If it is not the current thread, the AttachThreadInput API is called to attach the thread to the current thread. After all this work, we can call the GetFocus API to get the focus control handle.
    2. Get the text of the focus control. You can do this by calling the SendMessageW API to send a WM_GETTEXT message.

    MainWindow.xaml:

    <Grid>  
            <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="123" Margin="138,20,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="280"/>  
        </Grid>  
    

    MainWindow.xaml.cs:

    using System;  
    using System.Runtime.InteropServices;  
    using System.Windows;  
    using System.Windows.Forms;  
      
    namespace GetTextSelectedFromOtherApp  
    {  
      public partial class MainWindow : Window  
      {  
        public MainWindow()  
        {  
          InitializeComponent();  
          Loaded+= new RoutedEventHandler(Window_Loaded);  
        
        }  
        private Timer _timer = new Timer();  
        private void Window_Loaded(object sender, RoutedEventArgs e)  
        {  
          _timer.Interval = 1000;  
          _timer.Tick += new EventHandler(_timer_Tick);  
        }  
         
        void _timer_Tick(object sender, EventArgs e)  
        {  
          try  
          {  
            textBox1.Text = GetTextFromFocusedControl();  
          }  
          catch (Exception exp)  
          {  
            textBox1.Text += exp.Message;  
          }  
        }  
      
        private void button1_Click(object sender, EventArgs e)  
        {  
          _timer.Start();  
        }  
      
        private string GetTextFromFocusedControl()  
        {  
          try  
          {  
            int activeWinPtr = GetForegroundWindow().ToInt32();  
            int activeThreadId = 0, processId;  
            activeThreadId = GetWindowThreadProcessId(activeWinPtr, out processId);  
            int currentThreadId = GetCurrentThreadId();  
            if (activeThreadId != currentThreadId)  
              AttachThreadInput(activeThreadId, currentThreadId, true);  
            IntPtr activeCtrlId = GetFocus();  
      
            return GetText(activeCtrlId);  
          }  
          catch (Exception exp)  
          {  
            return exp.Message;  
          }  
        }  
         
        private string GetText(IntPtr handle)  
        {  
          int maxLength = 100;  
          IntPtr buffer = Marshal.AllocHGlobal((maxLength + 1) * 2);  
          SendMessageW(handle, WM_GETTEXT, maxLength, buffer);  
          string w = Marshal.PtrToStringUni(buffer);  
          Marshal.FreeHGlobal(buffer);  
          return w;  
        }  
      
        [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();  
      
      }  
    }  
    

    The result:
    200120-5.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.