Click to See Complete Forum and Search --> : Starting explorer and getting it's hwnd
seaweed
May 12th, 2000, 12:51 PM
Why am I having so much trouble with this?
Ok, I admit I'm a bit of a newbie at APIs, but not THAT new.
I want to start Microsoft Internet Explorer using VB, then position it on the screen so it works well with my program. How can I do this?
What I've done and what's happening:
I am using a program I wrote a while ago that uses Shell to start a program and then gets that program's hWnd from the processID returned by Shell.
I know it works fine because I use can use it with no problem with other programs such as Notepad. I can shell it, get the handle, and then do things like set the window text using the handle.
With explorer, it doesn't work. So I used GetWindowRect to see if I could figure out what I was getting, and each time the handle returned from shell refers to some window that has no caption and it's RECT is:
Left = 0
Right = 100
Top = 0
Bottom = 100
Also, I ran another program that lists all running window handles after shelling explorer with the first program, and the hWnd returned from the first program doesn't show up on the list of running windows.
I can only guess that shelling explorer opens one window which then starts another window?
Any help would be greatly appreciated. At the moment I am using a lame routine that uses EnumWindows and compares the last part of each window's text to the string, "Microsoft Internet Explorer" in order to get the handle. LAMENESS!
Thanks!
~seaweed
Dan Holliday
May 13th, 2000, 04:53 PM
I think this is an adaptation of what you already have but this will eliminate the problem of get the wrong handle if you have more than one explorer open.
Call the function ExecCmd and pass it your explorer paramete for example:
"C:\Program Files\Internet Explorer\IEXPLORE.EXE"
The function will load explorer using createprocess and wait for it to finish executing. When it has it then scan the active window until it find Microsoft Explorer in the caption of the main window. I hop this is OK please Let me KNOW.
Option Explicit
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function MoveWindow Lib "user32" (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 Long
Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Function ExecCmd(CmdLine As String) As Long
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim Ret As Long
Dim WindowTitle As String * 256
' Initialize the STARTUPINFO structure:
start.cb = Len(start)
' Start the shelled application:
Ret = CreateProcessA(0&, CmdLine$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
' Wait for the shelled application to finish:
Ret = WaitForSingleObject(proc.hProcess, 0)
Do While 0 = 0
ExecCmd = GetForegroundWindow
Ret = GetWindowText(ExecCmd, WindowTitle, 256)
If Right$(Left(WindowTitle, Ret), 27) = "Microsoft Internet Explorer" Then Exit Do
Loop
Ret = CloseHandle(proc.hProcess)
End Function
seaweed
May 14th, 2000, 09:06 PM
I will try your suggestion.
It is similar to what I have now, except that right now I am just waiting a set amount of time before checking for the window title, which is lame because sometimes explorer loads a page quickly, and other times it doesn't (especially when dealing with different users who may or may not already have the page cached and different connection speeds).
If anyone knows a better way to start explorer and get it's handle from VB, I would love to hear about it!
I would also like to somehow start explorer minimized until I can get it's handle. Right now I am using this to start explorer, but it doesn't start minimized...
Shell "Start.exe http://www.google.com", vbMinimizedNoFocus
Thanks for any suggestions!
~seaweed
ChillyCode
May 18th, 2000, 08:40 AM
you can also refrence it and set it properties and methods
Stevie-O
May 19th, 2000, 01:07 AM
Yeah...why do you people always try to launch IE via Shell and ShellExecute calls? Use the WebBrowser control, it gives you full control over the window produced.
ramdasv
May 19th, 2000, 01:14 AM
Dim modObjIE
Set modObjIE = CreateObject("InternetExplorer.Application")
modObjIE.Visible = True
modObjIE.Navigate2 "http://microsoft.com"
modlngWndIE = modObjIE.hwnd
ShowWindow modlngWndIE, 1
Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Show window can size the window
1-normal
2-maxmised
Ramdas
seaweed
May 22nd, 2000, 12:54 AM
Thanks, guys...you were a big help! The reason I used Shell was because I didn't know a better way. I knew you could use a web browser control on a form, but I didn't know you could actually start the "real" browser with it.
~seaweed
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.