开发者

In a windows application, how to check and retrieve(if any) selected text/image from any window?

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 fo开发者_开发问答r 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜