views:

434

answers:

1

How to read the highlighted/Selected Text from any window using c#.

i tried 2 approaches.

  1. Send "^c" whenever user selects some thing. But in this case my clipboard is flooded with lots of unnecessary data. Sometime it copied passwords also.

so i switched my approach to 2nd method, send message method.

see this sample code

 [DllImport("user32.dll")]
    static extern int GetFocus();

    [DllImport("user32.dll")]
    static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);

    [DllImport("kernel32.dll")]
    static extern uint GetCurrentThreadId();

    [DllImport("user32.dll")]
    static extern uint GetWindowThreadProcessId(int hWnd, int ProcessId);    

    [DllImport("user32.dll") ]
    static extern int GetForegroundWindow();

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam);     

   // second overload of SendMessage

    [DllImport("user32.dll")]
    private static extern int SendMessage(IntPtr hWnd, uint Msg, out int wParam, out int lParam);

    const int WM_SETTEXT = 12;
    const int WM_GETTEXT = 13;     

private string PerformCopy()
    {
        try
        {
            //Wait 5 seconds to give us a chance to give focus to some edit window,
            //notepad for example
            System.Threading.Thread.Sleep(5000);
            StringBuilder builder = new StringBuilder(500);

            int foregroundWindowHandle = GetForegroundWindow();
            uint remoteThreadId = GetWindowThreadProcessId(foregroundWindowHandle, 0);
            uint currentThreadId = GetCurrentThreadId();

            //AttachTrheadInput is needed so we can get the handle of a focused window in another app
            AttachThreadInput(remoteThreadId, currentThreadId, true);
            //Get the handle of a focused window
            int focused = GetFocus();
            //Now detach since we got the focused handle
            AttachThreadInput(remoteThreadId, currentThreadId, false);

            //Get the text from the active window into the stringbuilder
            SendMessage(focused, WM_GETTEXT, builder.Capacity, builder);

            return builder.ToString();
        }
        catch (System.Exception oException)
        {
            throw oException;
        }
    }

this code working fine in Notepad. But if i try to capture from another applications like Mozilla firefox, or Visual Studio IDE, it's not returning the text.

Can anybody please help me, where i am doing wrong? First of all, i have chosen the right approach?

+2  A: 

That's because both Firefox and Visual Studio don't use the built-in Win32 controls for displaying/editing text.

It is not possible in general to be able to get the value of "any" selected text, because of the fact that programs can re-implement their own version of the Win32 controls any way they see fit, and your program cannot possibly expect to work with all of them.

However, you can use the UI Automation APIs which will allow you to interact with the majority of 3rd-party controls (at least, all the good ones - such as Visual Studio and Firefox - will likely work with the UI Automation APIs since it's a requirement for accessibility)

Dean Harding
Firefox does not implement UI Automation, I had tried version 3.0 and it didnt work, hope it may support in future.
Akash Kava
Can we do something to improve my first approach. That is working very good, except unnecessary copyings........
Dinesh