|
-
Feb 11th, 2005, 09:30 AM
#1
Thread Starter
Junior Member
Tile Vertically
Hello,
I am trying to:
1) minimize all windows
2) open two new explorer windows
3) tile them vertically
If I comment out code, I can do each one indivually and they all work, but when I put them all together I cannot get the new windows to tile vertically. Here is my code:
Private Sub Form_Load()
Me.Hide
Dim oshell As Shell
Set oshell = New Shell
oshell.MinimizeAll
Shell "explorer.exe", vbNormalNoFocus
Shell "explorer.exe", vbNormalNoFocus
oshell.TileVertically
End
End Sub
Any suggestions? This is driving me crazy!?!?!?
-
Feb 11th, 2005, 09:56 AM
#2
Frenzied Member
Re: Tile Vertically
Never used Shell in VB, but it would seem to me that you would want to use:
oshell "explorer.exe", vbNormalNoFocus
rather than:
Shell "explorer.exe", vbNormalNoFocus
You code isn't using your new instance of Shell.
-
Feb 11th, 2005, 10:15 AM
#3
Thread Starter
Junior Member
Re: Tile Vertically
Yeah, I tried using:
oshell "explorer.exe", vbNormalFocus
but I kept getting run-time error 438: Object doesn't support this property or method. That's why I tried just using
shell "explorer.exe", vbNormalFocus
which at least doesn't give me runtime errors.
-
Feb 11th, 2005, 10:29 AM
#4
Frenzied Member
Re: Tile Vertically
After I submitted my post, I realized that there is probably some method in oshell that you have to use, just as you do in oshell.TileVertically.
How are you referencing Shell? I dropped your code into the onload sub for a new form and get a "User-defined type not defined" compile error.
-
Feb 11th, 2005, 10:40 AM
#5
Thread Starter
Junior Member
Re: Tile Vertically
I added a reference to shell32.dll through the References window, so I think I have that part of it set up properly. The code I posted is the all the code I am using.
If I comment everything out except for the oshell.tilevertically, it will properly tile the open windows on my desktop.
If I comment out everything except for oshell.minimizeall and the two commands to open the 'explore.exe', it minimizes and opens the two windows properly.
It just doesn't work when I put the two together. I'm relatively new to VB, but I really can't see why it isn't working.
-
Feb 11th, 2005, 11:26 AM
#6
Re: Tile Vertically
I would assume it isnt working because there is no delay between opening the windows.
The " Shell ... " command runs asynchronously, so the statement is run but doesnt wait for Explorer to finish opening before executing the next statement.
Unfortunately at the moment I cant think of a way to check that the Explorer windows have opened, so I would suggest that you add a "DoEvents" (or msgbox for now to see it happen) after each 'Shell "Explorer..' line, eg:
Shell "explorer.exe", vbNormalNoFocus
DoEvents
Shell "explorer.exe", vbNormalNoFocus
DoEvents
-
Feb 11th, 2005, 11:32 AM
#7
Frenzied Member
Re: Tile Vertically
I'm not clear as to what you mean by "comment everything out except for ...".
Anyway, I did some searching and found the following at MSDN:
http://msdn.microsoft.com/library/de...hell_intro.asp
Maybe you can find some help there.
-
Feb 11th, 2005, 11:43 AM
#8
Thread Starter
Junior Member
Re: Tile Vertically
Si,
I think you are on the right track! I used a Msgbox after opening the windows and then it tiled correctly after clicking "OK". So I guess I need a way to pause before going to the tile command.
I tried using a For-Next loop from 1 to 1000 but that didn't work. What should the count be to delay for about 1 second??
Thanks
-
Feb 11th, 2005, 01:03 PM
#9
Re: Tile Vertically
Unfortunately VB is clever and realises that an empty loop doesnt need to be run. There is also the problem that code runs at different speeds on different computers (and at different times). If you really want to pause, you should use the Sleep API (search this forum for examples).
However, pausing isn't the way to do it, as you cannot predict accurately how long it will take to open those windows. I had a quick search on this site and found an idea that seems to work for me (from post 26 in this thread).
What this basically does is keep track of which window is active, and waits until the active window changes (note that this method may fail if the user opens/activates a window while this code is running).
Put this into the "general declarations":
VB Code:
Private Declare Function GetActiveWindow Lib "user32" () As Long
and this in place of the two "Shell ..." lines:
VB Code:
Dim lActiveWindow As Long
DoEvents
lActiveWindow = GetActiveWindow
Shell "explorer.exe", vbNormalNoFocus
Do
DoEvents
Loop While lActiveWindow = GetActiveWindow
lActiveWindow = GetActiveWindow
Shell "explorer.exe", vbNormalNoFocus
Do
DoEvents
Loop While lActiveWindow = GetActiveWindow
It's not the ideal solution (which I don't know!), but it should hopefully do the job.
-
Feb 14th, 2005, 09:09 AM
#10
Thread Starter
Junior Member
Re: Tile Vertically
Si,
After trying your suggestions I still couldn't get it to work. Eventually, this is what I added:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Sub Wait(Seconds As Single)
Dim lMilliSeconds As Long
lMilliSeconds = Seconds * 1000
Sleep lMilliSeconds
End Sub
Now it works!!
Thanks for all your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|