I need to get the selected text in another app, such as Notepad, as a string but without copying it to the clipboard. Is there an API call or something I can use? The aim is for me to be able to select some text in Notepad, hit a hotkey, and the selected text is shown in my app's MessageRequester without altering the clipboard. Thanks!
[Edit] Making it work with all apps, not just Notepad, would be best.
Get selected text from other app without clipboard?
- netmaestro
- PureBasic Bullfrog
- Posts: 8433
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: Get selected text from other app without clipboard?
With Notepad it's pretty straightforward:
But the method is different for different controls. Notepad doesn't use a Richedit and so this method is all you need. A Richedit would utilize EM_GETSELTEXT and some other things (like webpages) require COM (though if you use a web gadget you can have #PB_Web_SelectedText fed to you on a spoon).
Code: Select all
; Start Notepad, enter text and select some of it
np = FindWindow_("Notepad",0)
If np
edit = GetWindow_(np, #GW_CHILD)
If edit
SendMessage_(edit, #EM_GETSEL, @start.i, @finish.i)
textlength = SendMessage_(edit, #WM_GETTEXTLENGTH, 0, 0) + SizeOf(Character)
If textlength>0
this$ = Space(textlength)
SendMessage_(edit, #WM_GETTEXT, textlength, @this$)
that$ = Mid(this$, start, finish-start+1)
MessageRequester("Selected text:", that$)
EndIf
EndIf
EndIf
BERESHEIT
Re: Get selected text from other app without clipboard?
Thanks, it's a good starting point for me to learn from. I just have to make it work with any third-party window now where text is selected.
Re: Get selected text from other app without clipboard?
Still struggling to make this work with any given window with text selected.
I found this page: http://www.c-sharpcorner.com/forums/rea ... ive-window
Which says I need to attach thread input to my app's process to the target window's, then getfocus on the target, then gettext on the getfocus result, but it doesn't seem to work. Here's what I've done:
Any help appreciated.
I found this page: http://www.c-sharpcorner.com/forums/rea ... ive-window
Which says I need to attach thread input to my app's process to the target window's, then getfocus on the target, then gettext on the getfocus result, but it doesn't seem to work. Here's what I've done:
Code: Select all
Delay(3000) ; Allow time for user to focus another window with selected text.
app=OpenWindow(0,0,0,0,0,"",#PB_Window_Invisible)
thread1=GetWindowThreadProcessId_(GetForegroundWindow_(),0)
thread2=GetWindowThreadProcessId_(app,0)
If thread1<>thread2 : AttachThreadInput_(thread1,thread2,#True) : EndIf
f=GetFocus_()
If thread1<>thread2 : AttachThreadInput_(thread1,thread2,#False) : EndIf
a$=Space(100) : r=SendMessage_(f,#WM_GETTEXT,99,a$)
MessageRequester(Str(r),a$) ; Should show selected text from other window.
- netmaestro
- PureBasic Bullfrog
- Posts: 8433
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: Get selected text from other app without clipboard?
I checked out the coding answer you referred to and from it I created the following test:
I switched to Windows Explorer and created a new text document. I wrote "Hickory dickery dock, the mouse ran up the clock" in it and selected "dickery dock".
Then I switched back to the PB code and pressed 'run'
Then I switched back to the text document and waited.
A messagerequester popped up saying "dickery dock".
Is that the result you're looking for?
I think it ought to work as long as the control having the text is a Windows control like an edit. It won't work with a Word document or a web page, each of which needs its own target-specific (and complicated) approach.
Code: Select all
Delay(5000) ; Allow time for user to focus another window with selected text.
app=OpenWindow(0,0,0,0,0,"",#PB_Window_Invisible)
hWnd = GetForegroundWindow_()
activeThreadId = GetWindowThreadProcessId_(hWnd, @processId.i)
currentThreadId = GetCurrentThreadId_()
AttachThreadInput_(activeThreadId, currentThreadId, #True)
focusedHandle = GetFocus_()
AttachThreadInput_(activeThreadId, currentThreadId, #False)
len = SendMessage_(focusedHandle, #WM_GETTEXTLENGTH, 0, #Null)
sb.s = Space(len + SizeOf(character))
numChars = SendMessage_(focusedHandle, #WM_GETTEXT, len + SizeOf(character), @sb)
Define sstart.i, nnext.i
SendMessage_(focusedHandle, #EM_GETSEL, @sstart, @nnext)
selectedText.s = Mid(sb, sstart, nnext-sstart)
MessageRequester("Selected Text:", selectedText)
Then I switched back to the PB code and pressed 'run'
Then I switched back to the text document and waited.
A messagerequester popped up saying "dickery dock".
Is that the result you're looking for?
I think it ought to work as long as the control having the text is a Windows control like an edit. It won't work with a Word document or a web page, each of which needs its own target-specific (and complicated) approach.
BERESHEIT
Re: Get selected text from other app without clipboard?
Thank you, netmaestro! That's what I'm looking for. I always appreciate your kind support and patience.