Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get selected text of any application into a windows form application

This is what am trying to do,

When user select any word(text) of any running application by double clicking the mouse particular highlighted word should be inserted into a windows application which is already running.

So far I have implemented the logic using Global Keystroke where user has to trigger CRT+ C keyboard key combination to copy the selected word into win form application.

What i want to know is there any way to get those selected text into the application without having any button key press of the keyboard?

like image 323
Thilina H Avatar asked Jan 30 '14 15:01

Thilina H


1 Answers

After some reading, I have found the way:

  1. Hook the double click event using something like globalmousekeyhook.codeplex.com
  2. (Optional) Save the current state of the clipboard
  3. Get The current mouse position with GetCursorPos from user32.dll
  4. Get windows based on cursor position with WindowFromPoint from user32.dll

    [DllImport("user32.dll")]
    public static extern IntPtr WindowFromPoint(Point lpPoint);
    
    [DllImport("user32.dll")]
    public static extern bool GetCursorPos(out Point lpPoint);
    
    public static IntPtr GetWindowUnderCursor()
    {
       Point ptCursor = new Point();
    
       if (!(PInvoke.GetCursorPos(out ptCursor)))
          return IntPtr.Zero;
    
       return WindowFromPoint(ptCursor);
    }
    
  5. Send copy command with SendMessage form user32.dll (see Using User32.dll SendMessage To Send Keys With ALT Modifier)

  6. Your Code
  7. (Optional) Restore the clipboard content saved in step 2
like image 90
Juan Rada Avatar answered Nov 15 '22 17:11

Juan Rada