It is currently Tue May 26, 2020 5:23 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Get selected text from other app without clipboard?
PostPosted: Sat Apr 25, 2015 3:11 am 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1907
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. :)


Top
 Profile  
Reply with quote  
 Post subject: Re: Get selected text from other app without clipboard?
PostPosted: Sat Apr 25, 2015 6:47 am 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 8111
Location: Fort Nelson, BC, Canada
With Notepad it's pretty straightforward:
Code:
; 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

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).

_________________
Veni, vidi, vici.


Top
 Profile  
Reply with quote  
 Post subject: Re: Get selected text from other app without clipboard?
PostPosted: Sat Apr 25, 2015 8:23 am 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1907
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Get selected text from other app without clipboard?
PostPosted: Fri Sep 08, 2017 5:48 am 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1907
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:

Code:
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.

Any help appreciated.


Top
 Profile  
Reply with quote  
 Post subject: Re: Get selected text from other app without clipboard?
PostPosted: Fri Sep 08, 2017 6:29 am 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 8111
Location: Fort Nelson, BC, Canada
I checked out the coding answer you referred to and from it I created the following test:
Code:
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)

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.

_________________
Veni, vidi, vici.


Top
 Profile  
Reply with quote  
 Post subject: Re: Get selected text from other app without clipboard?
PostPosted: Fri Sep 08, 2017 7:49 am 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1907
Thank you, netmaestro! :D That's what I'm looking for. I always appreciate your kind support and patience.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 18 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye