Retrieve selected text from any window / Get selected files fromexplorer

D

dh

Hello everyone,

i am looking for a way to get the selected text (current selection by
the user) in every app, like notepad, word, IE or Firefox. Something
like GetForegroundWindow -> GetSelectedText would be great.

I'd also like to know if there is any way i can get the selected files
and/or folders from the windows explorer.
I read that i can do this via Shell32 and SHDocVw but these don't work
in Vista anymore which is not acceptable for me.

The reason i ask this is because my app (which runs in the background)
needs this information so it can assist the user with these objects
(move files, copy files, copy the selected text to the clipboard).
That means i don't want to integrate some kind of own explorer in my
app.

Thanks very much for any kind of input.

Greets,
dh
 
D

dh

Hello everyone,

i am looking for a way to get the selected text (current selection by
the user) in every app, like notepad, word, IE or Firefox. Something
like GetForegroundWindow -> GetSelectedText would be great.

I'd also like to know if there is any way i can get the selected files
and/or folders from the windows explorer.
I read that i can do this via Shell32 and SHDocVw but these don't work
in Vista anymore which is not acceptable for me.

The reason i ask this is because my app (which runs in the background)
needs this information so it can assist the user with these objects
(move files, copy files, copy the selected text to the clipboard).
That means i don't want to integrate some kind of own explorer in my
app.

Thanks very much for any kind of input.

Greets,
dh

Anybody?
 
E

expvb

dh said:
Hello everyone,

i am looking for a way to get the selected text (current selection by
the user) in every app, like notepad, word, IE or Firefox. Something
like GetForegroundWindow -> GetSelectedText would be great.

I'd also like to know if there is any way i can get the selected files
and/or folders from the windows explorer.
I read that i can do this via Shell32 and SHDocVw but these don't work
in Vista anymore which is not acceptable for me.

The reason i ask this is because my app (which runs in the background)
needs this information so it can assist the user with these objects
(move files, copy files, copy the selected text to the clipboard).
That means i don't want to integrate some kind of own explorer in my
app.

You can send WM_GETTEXT, or WM_COPY. You may want to get the window's class
to see what type of window you are talking to.
 
M

mayayana

i am looking for a way to get the selected text (current selection by
the user) in every app, like notepad, word, IE or Firefox. Something
like GetForegroundWindow -> GetSelectedText would be great.

You might look into Active Accessibility, but I don't
know whether .Net can use that.
I'd also like to know if there is any way i can get the selected files
and/or folders from the windows explorer.
I read that i can do this via Shell32 and SHDocVw but these don't work
in Vista anymore which is not acceptable for me.

On Win95 -> XP you can get the selected items by
getting the ShellFolderView for the folder. Pre-XP
you can do that by actually walking the sub-windows
until you get the IE browser window, then get the
Document from that. On XP the "Web View" has been
removed and there is no IE browser window in folders,
but there's still a dummy Document object for backward
compatibility and I *think* the XP approach also works on
earlier versions. You can get at that from the Shell.Windows
collection of open IE/Explorer windows.

I don't know whether that works in Vista, or what you
mean when you say that "shell32 doesn't work in Vista
anymore". I also don't know the exact method in VB.Net;
only in VB. The Shell object is COM, so it might not be so
straightforward.

The basic deal is that you enumerate Shell.Windows,
which returns a collection of IE objects. (They're just
dummy IE objects on XP, but that's OK.) You can then call
QueryInterface on the Document object to get a ShellFolderView
object. ShellFolderView has a SelectedItems object that
returns a FolderItems collection.

You can also use Active Accessibility to do the
same thing. Again, I know nothing about Vista. But
it should work at least for 98 -> XP. I'm not
certain about Win95. (It requires the presence of
oleacc.dll.)

The deal is as follows: The actual file/folder list in a
folder window is a SysListView32 window. In other words,
it's just a modified ListView window. If you walk the folder
window hierarchy down until you get to the SysListView32,
you can then use AccessibleObjectFromWindow and
AccessibleChildren to get at the actual content. For each
child you can test selection state with the accState flag.
(Gobbledygook, I know. But it will make sense if you end
up getting into Active Accessibility.)
The reason i ask this is because my app (which runs in the background)
needs this information so it can assist the user with these objects
(move files, copy files, copy the selected text to the clipboard).
That means i don't want to integrate some kind of own explorer in my
app.
It's difficult. Microsoft has never fully provided
the functionality. Programs like screen readers
that need to provide that functionality use a combination
of basic API, Active Accessibility, and hacks.
 
C

C Kevin Provance

| I'd also like to know if there is any way i can get the selected files
| and/or folders from the windows explorer.
| I read that i can do this via Shell32 and SHDocVw but these don't work
| in Vista anymore which is not acceptable for me.

I don't know if it can be done with .net. It's possible with VB6 and some
specialized API...but with your ::cough:: superior managed code, there may
be limits with what you can actually do.

To get an entry in an explorer windows, you have to inject buffer space into
the appropriate windows handle, copy the data to the injected buffer and
then copy that data back to your local buffer, and then properly release all
resources used, or get an ugly crash.

Quite simply really.
 
T

Tom Shelton

| I'd also like to know if there is any way i can get the selected files
| and/or folders from the windows explorer.
| I read that i can do this via Shell32 and SHDocVw but these don't work
| in Vista anymore which is not acceptable for me.

I don't know if it can be done with .net. It's possible with VB6 and some
specialized API...but with your ::cough:: superior managed code, there may
be limits with what you can actually do.

Hmmm... Tell us what api Kevin? Show us some working code in VB6. If it's
just api or com stuff, .NET can handle it.
 
M

mayayana

An addendum to that last post in regard to
selected items in a folder: It's actually easier
than what I had explained. In a WebView folder
there's actually an embedded IE browser window
and, as I'd explained, you can get a ShellFolderView
object from the IE.Document. It also works on
XP. Even though the folder has no IE window on XP,
MS updated the Shell Object to still act as though
there were an IE window for backward compatibility.
You can access a limited set of the Document
object properties, even on XP. But it's actually
even stranger than that -
** If you treat the Document as a ShellFolderView
it works!** The QueryInterface call should not be needed.
The following simple VBScript code shows how it
works. If saved as a .VBS file and run it will show a
MsgBox listing the names of files selected in open
folder windows. (You can distinguish between open
folders by checking IE properties like LocationURL or
LocationName with the pseudo-IE objects returned in
the Shell.Windows collection.)

The code works on Win95 -> XP.
(Requires Active Desktop update on Win95/NT4.)
I don't have access to a Vista PC so I can't test
that.

'--------------------- Save to file shaptest.vbs -------

Dim SHAp, Wins, SFV, IE, SI, FI
Set SHAp = CreateObject("Shell.Application")

Set Wins = SHAp.Windows ' Shell.Windows
For Each IE in Wins
Set SFV = IE.Document ' ShellFolderView
Set SI = SFV.SelectedItems ' SelectedItems collection
For Each FI in SI ' FolderItem
s = s & FI.name & vbCrLf
Next
Set SI = Nothing
Set SFV = Nothing
Next
Set Wins = Nothing

MsgBox s

'--------- end vbs code --------------
 
T

Tom Shelton

An addendum to that last post in regard to
selected items in a folder: It's actually easier
than what I had explained. In a WebView folder
there's actually an embedded IE browser window
and, as I'd explained, you can get a ShellFolderView
object from the IE.Document. It also works on
XP. Even though the folder has no IE window on XP,
MS updated the Shell Object to still act as though
there were an IE window for backward compatibility.
You can access a limited set of the Document
object properties, even on XP. But it's actually
even stranger than that -
** If you treat the Document as a ShellFolderView
it works!** The QueryInterface call should not be needed.
The following simple VBScript code shows how it
works. If saved as a .VBS file and run it will show a
MsgBox listing the names of files selected in open
folder windows. (You can distinguish between open
folders by checking IE properties like LocationURL or
LocationName with the pseudo-IE objects returned in
the Shell.Windows collection.)

The code works on Win95 -> XP.
(Requires Active Desktop update on Win95/NT4.)
I don't have access to a Vista PC so I can't test
that.

'--------------------- Save to file shaptest.vbs -------

Dim SHAp, Wins, SFV, IE, SI, FI
Set SHAp = CreateObject("Shell.Application")

Set Wins = SHAp.Windows ' Shell.Windows
For Each IE in Wins
Set SFV = IE.Document ' ShellFolderView
Set SI = SFV.SelectedItems ' SelectedItems collection
For Each FI in SI ' FolderItem
s = s & FI.name & vbCrLf
Next
Set SI = Nothing
Set SFV = Nothing
Next
Set Wins = Nothing

MsgBox s

'--------- end vbs code --------------

And the vb.net equiv:

Option Explicit On
Option Strict Off
Option Infer On

Imports System.Text
Imports System.Runtime.InteropServices

Module Module1

Sub Main()
Dim sb As New StringBuilder()

Dim shellApp = CreateObject("Shell.Application")
Dim windows = shellApp.Windows
For Each ie In windows
Dim ieDoc = ie.Document
Dim selectedItems = ieDoc.SelectedItems
For Each fileItem In selectedItems
sb.AppendLine(fileItem.Name)
Marshal.ReleaseComObject(fileItem)
Next
Marshal.ReleaseComObject(selectedItems)
Marshal.ReleaseComObject(ieDoc)
Next
Marshal.ReleaseComObject(windows)
Marshal.ReleaseComObject(shellApp)

Console.Write(sb)
End Sub

End Module
 
M

mayayana

And the vb.net equiv:

So it works. Good. I would hope there'd be a better,
more direct way to do it in .Net, though. Your sample is
just taking late-bound script code that's highly inefficient
to begin with, and wrapping it in .Net objects. Can't .Net
access system COM objects more directly through native
..Net means? Or at least access typelibs with Interop to
get vtable binding?
 
T

Tom Shelton

So it works. Good. I would hope there'd be a better,
more direct way to do it in .Net, though. Your sample is
just taking late-bound script code that's highly inefficient
to begin with, and wrapping it in .Net objects. Can't .Net
access system COM objects more directly through native
.Net means? Or at least access typelibs with Interop to
get vtable binding?

Of course - I was simply mimicing your script. I wasn't trying to put the
effort into providing a real solution...
 
C

C Kevin Provance

| Hmmm... Tell us what api Kevin? Show us some working code in VB6. If
it's
| just api or com stuff, .NET can handle it.

Well, I was going to oblige you and the group and post it, but when I looked
it up and reviewed it I realized some of the API involved involved a third
party tool (Spyworks specifically). That was my mistake as I thought I had
stopped using that particular DLL in lieu of kernel API. I could still post
the example, but I can't post the declares for the 3rd party API as I would
be violating my license agreement. Still want it?

I imagine there must be a way to do it using straight Windows API since the
Spyworks DLL is just wrapping Windows API and making the task more simple.
They do offer .net versions of their software.
 
M

mayayana

Can't .Net
Of course - I was simply mimicing your script. I wasn't trying to put the
effort into providing a real solution...

Of course??? So then why are you posting a
..Net wrapper for VBScript? What good is that to
the OP? I don't need to know how I can add 88
MB worth of .Net dependencies to my script while
at the same time slowing it down.
(VBScript is already plenty slow, thanks. :)
The OP needs to know how to translate what
the script can do into decent .Net code.

You seem to think you're having some sort of
argument here. I offered my sample because
the OP has been waiting for a week, no one has
answered, and I know how to do what he/she needs
via VB/COM or via AA. But I don't know how to do
it via .Net. Since Windows runs on
COM to a great extent, I would have thought
that .Net would have some sort of equivalent to
the Shell interfaces, without needing to access them
through 3rd- or 4th-hand wrappers.... and that
if I explained the COM objects involved that might
help to work out the appropriate .Net equivalent. Or
at worst, if .Net doesn't have access to Explorer's Shell
interfaces, then my sample should at least provide the
necessary roadmap to create some sort of decent
Interop wrapper.
 
M

mayayana

Well, I was going to oblige you and the group and post it, but when I
looked
it up and reviewed it I realized some of the API involved involved a third
party tool (Spyworks specifically).

Oh, so you're the guy who bought that stuff. :)

I have a bone to pick with Dan Appleman. I paid
$60 for his VB API book and ended up with little
more than a giant book of unedited code dumps
and a lot of ads for his overpriced DLLs. ("Subclassing?
Don't try that yourself. Buy my DLL." "Networking
code? Sorry, I didn't have any room for that in the
1,500 pages of my book. But you might want to
buy my unnecessary winsock wrapper DLL.")
 
C

C Kevin Provance

| Oh, so you're the guy who bought that stuff. :)
|
| I have a bone to pick with Dan Appleman. I paid
| $60 for his VB API book and ended up with little
| more than a giant book of unedited code dumps
| and a lot of ads for his overpriced DLLs. ("Subclassing?
| Don't try that yourself. Buy my DLL." "Networking
| code? Sorry, I didn't have any room for that in the
| 1,500 pages of my book. But you might want to
| buy my unnecessary winsock wrapper DLL.")

Guilty. I don't think I want to get in the middle of Dan's advertising
policy. What I do claim is that in terms of *cross task* subclassing, not
something VB can do on its own, nothing comes close. It's saved my butt on
more than one occasion...so I can't really complain.

You also have to remember a lot of what Dan was pushing was back in the days
when VB was not able to do subclassing and callbacks on it's own, at least
with any stability. That has changed obviously, but the API book has not
(my copy, all ripped and battered was released right after VB5).

And for what it's worth, I've never used all the networking/dialog/etc
wrappers he wrote. I think that was more for folks who didn't know how to
do it and wanted an instant solution. I grant that for the seasoned coder,
such things can be accomplished with pure API alone.

- Kev
 
T

Tom Shelton

Of course??? So then why are you posting a
.Net wrapper for VBScript? What good is that to
the OP? I don't need to know how I can add 88
MB worth of .Net dependencies to my script while
at the same time slowing it down.
(VBScript is already plenty slow, thanks. :)
The OP needs to know how to translate what
the script can do into decent .Net code.

You seem to think you're having some sort of
argument here. I offered my sample because
the OP has been waiting for a week, no one has
answered, and I know how to do what he/she needs
via VB/COM or via AA. But I don't know how to do

If you know how to do it via VB/COM - then post the VB6 code and tell us about
the references you use, and then I'll translate it to VB.NET. I just don't
have time to research this right now. If, as you say you know how to do it in
VB (not VBA), then show us that please :)
 
M

mayayana

If you know how to do it via VB/COM - then post the VB6 code and tell us
about
the references you use, and then I'll translate it to VB.NET. I just don't
have time to research this right now. If, as you say you know how to do it in
VB (not VBA), then show us that please :)

The VBScript sample IS the code. I explained how
it works. The VBS version is the simplest demo of that.
If you think that a full-blown sample of VB Shell
and/or AA code might also be helpful then see here:

www.jsware.net/jsware/vbcode.php5#shlop
 
M

mayayana

You also have to remember a lot of what Dan was pushing was back in the
days
when VB was not able to do subclassing and callbacks on it's own, at least
with any stability. That has changed obviously, but the API book has not
(my copy, all ripped and battered was released right after VB5).

I guess that's a good point. He had code back to
VB4, I think, in his book. Maybe he just didn't bother
much with updating it all when VB5/6 came along.
 
D

dh

Sorry for my late response, but somehow i haven't been noticed about
your answers...

I was aware that i could do it via Active Accessibility but i didn't
find any helpful examples on Google or MSDN.
I also tried to do it via Shell but the only thing i got was the
foldername, not the selected files or folders (and i read somewhere
that this won't work under Vista / Windows 7).

Anyway, i will try out your suggestions and post the results here as
soon as i have something that works.

Thank you guys very much for your help!

Greets,
dh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top