views:

414

answers:

3

Is there a way to know which file is being selected in windows explorer? I've been looking at the tutorial posted here Idiots guide to ... but the actions described are:

hover

context

menu properties

drag

drag and drop

I wonder if is there a method that get invoked when a file is selected. For instance to create a thumbnail view of the file.

Thanks.

A: 

I've just found something called DDE, is this what I'm looking for? I have no clue.

OscarRyz
DDE is Dynamic Data Exchange (http://en.wikipedia.org/wiki/Dynamic_Data_Exchange), and is used for communication between apps and the shell. Unfortunately it's probably not what you need here.
Charlie
Thanks for the comment
OscarRyz
A: 

I came across this python script.

from win32com.client.gencache import EnsureDispatch 

for w in EnsureDispatch("Shell.Application").Windows(): 
    print w.LocationName + "=" + w.LocationURL

But I only get the folder that is open and not the currently selected item in that folder.

Anyone has more info?

OscarRyz
A: 

Here is how I do it in AutoHotkey:

GetWindowsExplorerSelectedFile(_hWnd)
{
    local selectedFiles, file

    ; I can send ^C and parse Clipboard, but this way don't mess with clipboard at all, seems nicer.
    ; Warning: with this, you get only what is displayed in Explorer!
    ; If you kept the default Windows setting of not displaying file extensions (bad idea...),
    ; you will get partial file names...
    ControlGet, selectedFiles, List, Selected Col1, SysListView321, ahk_id %_hWnd%
    Loop, Parse, selectedFiles, `n  ; Rows are delimited by linefeeds (`n).
    {
     If (A_Index = 1)
     {
      file := A_LoopField
     }
     Else
     {
      ; Indicate that several files are selected, we return only the first one
      ; but count the total number of selected files, to indicate we return a partial result
      ErrorLevel := A_Index
     }
    }
    Return file
}

And I get the path from the edit field of the Explorer (which is prone to problems! Can be absent or can be set up not to display full path).

The core idea is to ask the SysListView32 control of Explorer what are the selected items, and get them.

Now, that's a hack, there are probably cleaner ways...

PS.: Also found this: Getting ListView items in C# from SysListView32 using SendMessage
Need some voodoo to get it working on another process...

Real code at a French site!

PhiLho