✅ Get X and Y of selected text using WINAPI
Hi, I'd like to display custom small WPF window on text selection by getting X and Y coords via WinAPI, similarly to how context menus work in windows. Ive seen tons of apps do that so it should be possible.
21 Replies
can you elaborate on how the app is supposed to work
Not 100% sure how am gonna do it yet, but imagine user selects some text on any window on windows. presses some hotkey and small window appears near the selected text. So I'd need to acquire x and Y of maybe end character of the selected substring
You probably want to look into the RegisterHotKey and GetCursorPos pinvokes
yep the registering key is done, not a problem
but getcursorpos would get current cursor position, no?
yes
it'll be likely difficult to get the selection position for all edge cases
because someone can hypothetically select a text, move mouse somewhere else and then press the hotkey 😄
yeah probably :/
I don't know of any way to do it anyway, I figure most apps handle selection differently so you would need to handle all edge cases
you can use Spy++ to see how difficult this is
walking the visual tree and inspecting the wndproc messages
oh yeah good idea
i think getcuror pos might be the easiest semi solution
here you can see me capturing messages for Notepad with the RichTextBox
there is a selection message
EM_EXGETSEL message (Richedit.h) - Win32 apps
Retrieves the starting and ending character positions of the selection in a rich edit control.
yeah i tried exgetsel, but works for edit controls only
yes
that's what I meant by edge cases 😄
yep there are a lot
I would go with the mouse position route
one quick idea, probably not a very good one
how about using OpenCV 😄
to identify the region of selected text by feeding the screenshot and then u can find the x and y
sounds like overcomplication
I thought about it
and would probably be awfully slow for a background process
but you have to account for themes as well, and a lot of false positives
yeah..
edge cases...
ok ill go with cursor pos and maybe optimize later, its intended for limited users anyway. thanks! @Anchy