Reputation: 23489
How could I retrieve the word under the cursor, on the currently active window? My thought was to use GetCursorPos()
and WindowFromPoint()
to get the handle, and do something, but how?
Imagine a dictionary app, which reads the text under the cursor, and give an explanation of its meaning.
EDIT
I end up use the dll from stardict, with its API hooks on text drawing.
Upvotes: 2
Views: 1198
Reputation: 179779
You have to know the API needed for this; it's not obvious. You're looking for MSAA, Microsoft Active Accessibility
In short, you'll write an MSAA client. By calling AccessibleObjectFromPoint
you get an IAccessible
pointer. This pointer gives access to the properties of the object at the specified point.
Upvotes: 5
Reputation: 595367
What you are asking for is not trivial to implement.
Once you have determined which window is under the cursor (don't forget that you also need to use ChildWindowFromPoint()
to drill down through nested windows), you can use GetClassName()
to figure out what type of window it is.
For a standard RICHEDIT
window, you can translate the screen-absolute cursor coordinates into client-relative coordinates within the window using MapWindowPoints()
and then use the EM_FINDWORDBREAK
, EM_EXSETSEL
, and EM_GETSELTEXT
messages to locate, highlight, and copy the word at the cursor coordinates.
For a standard EDIT
window, once you have translated the coordinates, you can use the EM_CHARFROMPOS
message to locate the character offset nearest the cursor coordinates, then use the EM_GETTEXT
message for a single-line window, or the EM_LINEFROMCHAR
and EM_GETLINE
messages for a multi-line window (use GetWindowLong(GWL_STYLE)
to test for the ES_MULTILINE
style), to retreive the window's text, and then you would have to manually parse the text surrounding the character offset.
For other types of windows, especially custom controls, you have to do a lot more work, if it is even possible to get access to the window's text at all. Some windows respond to WM_GETTEXT
messages and/or GetWidnowText()
, while others do not.
Upvotes: 2