Page 1 of 2

Open file-explorer on specific folder + set window position?

Posted: Mon Jan 26, 2015 12:14 pm
by Kukulkan
Hello,

I need to open a file explorer window at a given folder. Currently I do this by using

Code: Select all

RunProgram("file://c:\git\")
But now I need to move the opened file explorer window to a given position. I like it to open on the upper right half of the current desktop while taking 50% of the desktop height. I need this as I like to place my app to the left to allow easy drag&drop actions.

Any idea how to position the opened file explorer window?

Kukulkan

Re: Open file-explorer on specific folder + set window posit

Posted: Mon Jan 26, 2015 1:02 pm
by bbanelli
Maybe something like this will help? You can set position and window size through SetWndowPos()

Code: Select all

EnableExplicit

Define.MODULEENTRY32 Entry
Define.i handle, res, PID, hWnd
Define *Buffer
Define rect.RECT
Define.i w, h

*Buffer = AllocateMemory(256)

Entry\dwSize = SizeOf(MODULEENTRY32)

RunProgram("file://c:\windows\")
Repeat
  hWnd = GetForegroundWindow_()
  If hWnd
    GetWindowThreadProcessId_(hWnd, @PID)
    GetWindowText_(hWnd, *Buffer, 255)
    handle = CreateToolhelp32Snapshot_(#TH32CS_SNAPMODULE, PID)
    If handle
      res = Module32First_(handle, Entry)
      If res
      EndIf
      CloseHandle_(handle)
    EndIf
  EndIf
Until LCase(PeekS(@Entry\szModule,-1)) = "explorer.exe"
If GetWindowRect_(hWnd, @rect)
  w = rect\right - rect\left
  h = rect\bottom - rect\top
EndIf
SetWindowPos_(hWnd, #Null, 0, 0, w, h, #Null)
End
http://youtu.be/-xVxqtjVDYk?hd=1

Basically, it will wait until your Windows Explorer is successfully started and will grab his handle and do things to him.

Hope that helps.

Re: Open file-explorer on specific folder + set window posit

Posted: Mon Jan 26, 2015 1:44 pm
by RASHAD
Hi

Code: Select all

RunProgram("file://c:\windows\")
Repeat
If OSVersion() > #PB_OS_Windows_Server_2003    
    hwnd = FindWindow_(0,"c:\windows")
 Else
    hwnd = FindWindow_(0,"WINDOWS")
 EndIf
Until hWnd
GetWindowRect_(hWnd,r.RECT)
MoveWindow_(hWnd,50,50,r\right-r\left,r\bottom-r\top,1)

Re: Open file-explorer on specific folder + set window posit

Posted: Mon Jan 26, 2015 3:41 pm
by Kukulkan
Thank you! Will try both... :)

Kukulkan

Re: Open file-explorer on specific folder + set window posit

Posted: Mon Jan 26, 2015 8:06 pm
by Danilo
RASHAD's code works also with Directory Opus for me (Win8.1).
The code by bbanelli loops here without time-out because it opens an Opus window and waits for "explorer.exe".

Re: Open file-explorer on specific folder + set window posit

Posted: Mon Jan 26, 2015 10:19 pm
by bbanelli
Danilo wrote:RASHAD's code works also with Directory Opus for me (Win8.1).
The code by bbanelli loops here without time-out because it opens an Opus window and waits for "explorer.exe".
Well, you can change it to whatever you wish, in addition of having window title which can make a bit more sure you choose the right window...

Re: Open file-explorer on specific folder + set window posit

Posted: Tue Jan 27, 2015 6:24 am
by Danilo
@bbanelli:
I just wanted to say that this codes are not safe to use in the outside world. Many people replace the
minimalistic programs that come with Windows (Paint, Wordpad, Notepad, Calc, Explorer, ..)
with better programs.
Both codes can fail and loop infinitely. For safe coding, I would at least add a time-out to the loops,
otherwise applications just hang at this point, probably on some million computers out there.
It is also a more general thing to consider, if you loop and wait for an (external) condition (f.e. network answer),
a time-out or another way for aborting the operation should be considered.

Re: Open file-explorer on specific folder + set window posit

Posted: Tue Jan 27, 2015 6:57 am
by mestnyi
Windows 8 works like this

Code: Select all

RunProgram("file://c:\windows\")
Repeat
If OSVersion() > #PB_OS_Windows_Server_2003    
    hwnd = FindWindow_(0,"WINDOWS")
 Else
    hwnd = FindWindow_(0,"c:\windows")
 EndIf
Until hWnd
GetWindowRect_(hWnd,r.RECT)
MoveWindow_(hWnd,550,50,r\right-r\left,r\bottom-r\top,1)

Re: Open file-explorer on specific folder + set window posit

Posted: Tue Jan 27, 2015 7:26 am
by Danilo
mestnyi wrote:Windows 8 works like this
No. Windows Explorer (and other file managers) has an option, so the window title could
be "C:\Windows" or just "Windows".

Image

Re: Open file-explorer on specific folder + set window posit

Posted: Tue Jan 27, 2015 8:25 am
by Kukulkan
Hello,

I agree with Danilo that both codes are not complete. It works somehow, but users customization may break it. Adding some timeout is not a big deal, but in such case the goal is not reached :(

Are there any other ideas for a working way? For example, I'm ok if the Windows file explorer opens up even if some other product is installed (eg Directory Opus etc). Can I invoke a file explorer window by using some Windows API? And would this give me full control (eg returning the window handle)?

Kukulkan

Re: Open file-explorer on specific folder + set window posit

Posted: Tue Jan 27, 2015 8:59 am
by bbanelli
Kukulkan wrote:Hello,

I agree with Danilo that both codes are not complete. It works somehow, but users customization may break it. Adding some timeout is not a big deal, but in such case the goal is not reached :(

Are there any other ideas for a working way? For example, I'm ok if the Windows file explorer opens up even if some other product is installed (eg Directory Opus etc). Can I invoke a file explorer window by using some Windows API? And would this give me full control (eg returning the window handle)?

Kukulkan
Try this

Code: Select all

RunProgram("explorer.exe", "/n,/root,c:\windows", "")
Or you can keep it "inhouse" with ExplorerListGadget and some custom coding to gain all Windows Explorer abilities.

Re: Open file-explorer on specific folder + set window posit

Posted: Tue Jan 27, 2015 10:36 am
by Kukulkan
Hello,

looks like this is doing the trick in most cases (Windows only, timeout of 5 seconds):

Code: Select all

; Folder to open explorer in
; does not work with drive only (like "c:" or "f:")
Folder.s = "c:\windows"

FolderName.s = ""
c.s = ""
x.i = Len(Folder.s)
Repeat
  c.s = Mid(Folder.s, x.i, 1)
  If c.s <> "/" And c.s <> "\"
    FolderName.s = c.s + FolderName.s
  Else
    Break
  EndIf
  x.i = x.i - 1
Until x.i < 1

StartTime = ElapsedMilliseconds()

RunProgram("explorer.exe", "/n,/root,"+Folder.s, "")
Repeat
  hwnd = FindWindow_(0, Folder.s)
  If hwnd = 0
    ; try FolderName if Windows explorer does not show full path in header
    hwnd = FindWindow_(0, FolderName.s)
  EndIf
Until hWnd <> 0 Or ElapsedMilliseconds() > (StartTime.i + 5000)

GetWindowRect_(hWnd,r.RECT)
MoveWindow_(hWnd,550,50,r\right-r\left,r\bottom-r\top,1)

End
Kukulkan

Re: Open file-explorer on specific folder + set window posit

Posted: Tue Jan 27, 2015 3:30 pm
by mestnyi
That's the way that it is impossible?

Code: Select all

RunProgram("file://c:\windows\")

 hwnd = FindWindow_(0,"c:\windows")
If Not hWnd
  hwnd = FindWindow_(0,"WINDOWS")
EndIf

GetWindowRect_(hWnd,r.RECT)
MoveWindow_(hWnd,550,50,r\right-r\left,r\bottom-r\top,1)

Re: Open file-explorer on specific folder + set window posit

Posted: Tue Jan 27, 2015 4:05 pm
by Kukulkan
Hello mestnyi,
That's the way that it is impossible?
This does not work because of 2 reasons:
1) The explorer may need some time to create the window.
2) The file:// call does not always run explorer.exe. It opens what is defined and this may vary (eg Directory Opus etc)

I'm now looking for a Linux and Mac solution for my problem...

Kukulkan

Re: Open file-explorer on specific folder + set window posit

Posted: Wed Jan 28, 2015 9:34 pm
by mestnyi
If you do like this? :)

Code: Select all

RunProgram("file://c:\windows\")

 hwnd = FindWindowEx_(0,0,0,0)
 While text$ <> "Windows" Or text$ <> "C:\Windows"
   text$ = Space(512)
   GetWindowText_(hwnd,@text$,512)
   If text$ = "Windows" Or text$ = "C:\Windows"
     Debug text$
     GetWindowRect_(hWnd,r.RECT)
     MoveWindow_(hWnd,550,50,r\right-r\left,r\bottom-r\top,1) 
     Break
   EndIf
   hwnd = FindWindowEx_(0,hwnd,0,0)
 Wend