Position a Window

  • Thread starter Thread starter Gary''s Student
  • Start date Start date
G

Gary''s Student

I open a folder with:

Sub folderopen()
x = Shell("Explorer.exe ""C:\Temp""", vbNormalFocus)
End Sub

Once opened, I would like to position the folder window. Neither:

ActiveWindow.Top = 0
x.Top = 0

work. Any suggestions??
 
Hey there..

Gary''s Student said:
Once opened, I would like to position the folder window. Neither:

I just looked into this because I was curious myself, and once again I
got shown that VBA is absolute shit for even half-serious programming.

Below is your solution (make sure you get the linebreaks right ;)
Just copypaste to a new module and call the sub test from anywhere for a
proof of concept.
Then just use the code from sub test in your own code:

hInstance = Shell("cmd.exe", vbNormalFocus)
hWindow = WaitForWindow(hInstance)
MoveWindow hWindow, 50, 300, 800, 400, True

Best Regards,

Lars

'''''''''''''''''''''
Option Explicit

Public Const GW_HWNDNEXT = 2

Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
ByVal wCmd As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, lpdwprocessid As Long) As Long
Public Declare Function MoveWindow Lib "user32.dll" (ByVal hwnd As Long,
ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As
Long, ByVal bRepaint As Long) As Boolean
'Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Public Function WaitAndDoEvents(ByVal waitSeconds As Double)
Dim startTime As Double
startTime = Timer()
While (Timer() - startTime < waitSeconds)
DoEvents
Wend
End Function


Function ProcIDFromWnd(ByVal hwnd As Long) As Long
Dim idProc As Long

' Get PID for this HWnd
GetWindowThreadProcessId hwnd, idProc

' Return PID
ProcIDFromWnd = idProc
End Function


Function GetWinHandle(ByVal hInstance As Long) As Long
Dim tempHwnd As Long

' Grab the first window handle that Windows finds:
tempHwnd = FindWindow(vbNullString, vbNullString)

' Loop until you find a match or there are no more window handles:
Do Until tempHwnd = 0
' Check if no parent for this window
If GetParent(tempHwnd) = 0 Then
' Check for PID match
If hInstance = ProcIDFromWnd(tempHwnd) Then
' Return found handle
GetWinHandle = tempHwnd
' Exit search loop
Exit Do
End If
End If

' Get the next window handle
tempHwnd = GetWindow(tempHwnd, GW_HWNDNEXT)
Loop
End Function


' returns window handle or 0 if timed out
Function WaitForWindow(ByVal hInstance As Long, Optional timeout As
Double = 5) As Long
Dim hWindow As Long
Dim startTime As Double

startTime = Timer()
Do ' wait for process to start
hWindow = GetWinHandle(hInstance) ' 0 if hInstance not found
DoEvents
Loop Until hWindow <> 0 Or (Timer() - startTime > timeout)

WaitForWindow = hWindow ' 0 if timed out & hInstance not found
End Function


Public Sub test()
Dim hInstance As Double
Dim hWindow As Long

hInstance = Shell("cmd.exe", vbNormalFocus)
hWindow = WaitForWindow(hInstance)
MoveWindow hWindow, 50, 300, 800, 400, True
End Sub
 
I tried everything with API's etc and got nothing, it just opens the last
place you put it. What I did get to work very well was placing a webbrowser
control on a form and on the activate event:
Me.WebBrowser1.Navigate2 smURL & "c:\temp"
Me.Top = 0
Me.Left = 0

host the folder browser in the form then move the form. Hope that works for
you.
 
Thank you.

I have adopted a variation of what you reccomended. It is a little painful.
 

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

Back
Top