1

I have a windows application which should constantly keep track of the active application window.

I am yet to try it but I searched around and found that this could be done by using the windows API (user32.dll) - GetForegroundWindow()

But, my requirement also needs the application to look for any selected text or image in the active window (It could be a document, pdf, email, browser, mspaint etc). I am not sure how to do this. Is it possible to look for selected text or an image in the active window, and if found, then can it be retrieved to my application and used?

I am guessing if the selected text/image is obtained then I can copy it to the clip board and use it from my application because all applications have access to the clipboard.

Any thoughts? Any alternative/better ways to do this?

pavanred
  • 12,717
  • 14
  • 53
  • 59
  • Duplicates: http://stackoverflow.com/questions/1007185/how-to-retrieve-the-selected-text-from-the-active-window http://stackoverflow.com/questions/235972/copy-and-modify-selected-text-in-different-application – Greg Hewgill Dec 13 '09 at 19:13
  • I did look through these threads. In fact this is where I learnt that I have to use user32.dll - GetForegroundWindow() for obtaining the active window etc. Besides one of these is a Python thread and I tried to understand as much as I could from it. The other c# thread did have some answers which I am trying to implement now.But, they speak only about selected text. I need to know if it's possible to work on similar lines with images too. – pavanred Dec 13 '09 at 19:27

2 Answers2

0

Yes GetForegroundWindow() is how you get the active window.

I think you may have difficulty with what you want to do though. Each window is composed of many subwindows. You need to get the text of any of those subwindows.

Typically this type of work is done using a tool called spy++. You can discover the window hierarchy and then obtain child windows with the Win32 API FindWindowEx.

You can for example obtain a windows text using WM_GETTEXT messages.

    [DllImport("user32.dll", EntryPoint="SendMessage")]
    public static extern int SendMessageForGetText(int hwnd, int msg, int wParam, StringBuilder sb);
    const int WM_GETTEXT = 0x000D; 

I'm not sure if you will be able to find a general solution to your problem though. You probably need per application handling.


Depending on what you want to do, you may just be able to send messages to send Ctrl + C. And then check the clipboard for what you want.


You also may be able to find a solution, or partial solution using the Active Accessibility SDK.

Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
  • So I might have to handle these separately for each application. One application that has I use and has a similar implementation(but only text) is wordweb. And wordweb works very well. Does any one have an idea how it might have been done in wordweb. I am not sure but I feel handling these separately for each application sounds very exhaustive. – pavanred Dec 13 '09 at 19:37
  • I don't know of that program, sorry. I think your best bet will be to investigate the active accessibility SDK. – Brian R. Bondy Dec 14 '09 at 01:02
0

Different programs (or even different parts of the same program) internally represent and handle selection differently. In many applications multiple controls can have something selected in them. Most textboxes are usually pretty standard, but many applications with more advanced text editing features use a custom textbox that won't work the same. Different PDF viewers will work differently, different graphics applications will work differently, etc. ect.

snarf
  • 2,684
  • 1
  • 23
  • 26