C
C#2y ago
Lukaa

✅ 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
Anchy
Anchy2y ago
can you elaborate on how the app is supposed to work
Lukaa
LukaaOP2y ago
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
Anchy
Anchy2y ago
You probably want to look into the RegisterHotKey and GetCursorPos pinvokes
Lukaa
LukaaOP2y ago
yep the registering key is done, not a problem but getcursorpos would get current cursor position, no?
Anchy
Anchy2y ago
yes it'll be likely difficult to get the selection position for all edge cases
Lukaa
LukaaOP2y ago
because someone can hypothetically select a text, move mouse somewhere else and then press the hotkey 😄 yeah probably :/
Anchy
Anchy2y ago
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
Lukaa
LukaaOP2y ago
oh yeah good idea
Anchy
Anchy2y ago
Lukaa
LukaaOP2y ago
i think getcuror pos might be the easiest semi solution
Anchy
Anchy2y ago
here you can see me capturing messages for Notepad with the RichTextBox there is a selection message
Anchy
Anchy2y ago
EM_EXGETSEL message (Richedit.h) - Win32 apps
Retrieves the starting and ending character positions of the selection in a rich edit control.
Lukaa
LukaaOP2y ago
yeah i tried exgetsel, but works for edit controls only
Anchy
Anchy2y ago
yes that's what I meant by edge cases 😄
Lukaa
LukaaOP2y ago
yep there are a lot
Anchy
Anchy2y ago
I would go with the mouse position route
Lukaa
LukaaOP2y ago
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
Anchy
Anchy2y ago
I thought about it
Lukaa
LukaaOP2y ago
and would probably be awfully slow for a background process
Anchy
Anchy2y ago
but you have to account for themes as well, and a lot of false positives
Lukaa
LukaaOP2y ago
yeah.. edge cases... ok ill go with cursor pos and maybe optimize later, its intended for limited users anyway. thanks! @Anchy
Want results from more Discord servers?
Add your server