|
-
May 7th, 2001, 06:48 AM
#1
Thread Starter
New Member
Get the selected text of an non-VB-window
Hi,
how can I get the currently selected text of an non-VB-window like an Word Document Window?
I can get the handle of that window with GetActiveWindow and GetForeroundWindow.
But how can I get the selected text in that window?
Thanks!
Joerg
-
May 7th, 2001, 09:49 AM
#2
Addicted Member
for a word document you don't.
you have two choices:
1. send a Ctrl-C to the window using the keybd_event API which will put the infomation into the clipboard which you can then go get
2. get the word object using getobject (a VB function, not an API) and use the word object model to get the selected text eg. oWord.activedocument.selectedtext
-
May 8th, 2001, 12:08 AM
#3
Thread Starter
New Member
Hi,
thanks for your reply.
To send ctrl+c is what I'm doing, but it does not work always properly.
To get the selected text should work in every kind of windows, that' why I can' t use the GetObject-method to use the word object model.
I know it's possible to this with API and WM_GETTEXT, but I don't know how?
Any suggestions?
Greetings
Joerg
-
May 8th, 2001, 04:27 AM
#4
Addicted Member
Nothing will work with every window period.
WM_GETTEXT doesn't get selected text, it gets the text of the window given to it. This is the title of the window, with some controls like buttons this can be the caption. but it can never get the selected text.
there is NO single method that will work with every window, as each window is different.
there are ways to get selected text out of Some text controls (MS applications not being one of them) using the EM_GETSELTEXT (note this isn't in the API viewer, it is decalred as.. WM_USER + 62, WM_USER can be found in the API viewer)
WParam must be 0
LParam contains the buffer, this must be large enough to contain the selected text
it returns the number of characters returned not including the Null Termiator
that will work with editboxes and RichEdit controls, it will be upto you to find out which is currently active.
you will need a different message for comboboxes, another for listboxes.
there is no easy way to do what you want to do, may i ask Why you wish to do it?
-
May 9th, 2001, 02:36 AM
#5
Thread Starter
New Member
Hi,
i created a VB-program that shows an information window every time when a special hotkey is pressed containing information related to the selected word. It' s similar to the common word feature of pressing right mouse button on a red underlined word to get the correct spelling.
To get the selected text after pressing the hotkey I use SendKeys "^c". And it works. My information window pops up. If a content of that window is double-clicked that content replaces the original text by using
Clipboard.SetText ...
Sendkeys "^v"
Thats also very similar to word where you can replace the incorrect word by clicking on the correct word in the pop up menu.
However, it works so far, but especially replacing the text very often does not work properly.
Finally I want to re-create a copy of the word feature mentioned above, but windows-wide.
Like I was saying: it works so far, but I think there must be a more reliable way of getting/replacing the selected text in any kind of window than simulating the Ctl+c and ctl+v keystrokes.
A few years ago I saw a little program which did exaclty what I want: after selecting a text containing an currency amount and pressing ctl+right mouse button it showed that amount in several other currencies. You could select a currency and replace the selected text. This program worked windows-wide and absolutely reliable. That's why I think it used some kind of subclassing, API or whatsoever. That's why I say: me too, please ;-)
Greetings
Joerg
-
May 9th, 2001, 04:04 AM
#6
Addicted Member
Theres your problem.
Trying to use SendKeys - its very unreliable.
what you wish to use the the keybd_event API
you can send a system wide Ctrl+C event, to the current window, (doesn't work on Russian thou)
you will need to send a keydown event for Ctrl
then a C or V keydown event
then a C or V KeyUp event
then a Ctrl up event.
have a look in the MSDN to see how you do.
Some Days, i just get this feeling that i'm helping to write dozens of Viruses...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|