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

Windows specific forum
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

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

Post 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
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

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

Post 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.
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

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

Post 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)
Egypt my love
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

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

Post by Kukulkan »

Thank you! Will try both... :)

Kukulkan
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

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

Post 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".
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

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

Post 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...
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

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

Post 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.
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

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

Post 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)
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

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

Post 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
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

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

Post 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
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

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

Post 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.
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

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

Post 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
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

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

Post 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)
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

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

Post 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
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

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

Post 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
Post Reply