Link to home
Start Free TrialLog in
Avatar of mikeytag
mikeytag

asked on

Open Internet Explorer Window as a Popunder

Hello everyone,
I already have my VB app opening up an internet explorer window with no problems. Everything works fine, my only problem is I want to open the window as a popunder (i.e. leave the current explorer window open focused and have this one blurred) I haven't found any VB code that can actually control this. My guess is that I would have to state an hwnd for the new window that is lower than any other window currently open? Anyways I want to steer clear of opening an html page that has javascript embedded to blur the page, as this is a really choppy workaround.  Here is the current sub I am using to popup the window. Thanks

Public Sub LoadURL(URL)
    On Error Resume Next
    Set modObjIE = CreateObject("InternetExplorer.Application")
    modObjIE.ToolBar = False
    modObjIE.StatusBar = False
    modObjIE.ScrollBar = True
    modObjIE.Visible = True
    modObjIE.Navigate2 URL
    modlngWndIE = modObjIE.hwnd
End Sub
Avatar of Smallint
Smallint


Try to add:

Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOZORDER = &H8


and in LoadURL


SetWindowPos modlngWndIE, HWND_TOPMOST, 0, 0, 0, 0, _
   SWP_NOZORDER + SWP_NOMOVE + SWP_NOSIZE


Correction:

SetWindowPos modlngWndIE, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE + SWP_NOMOVE


Avatar of mikeytag

ASKER

Interesting code, tried it out and it did pop the window "under" only problem was if you clicked it the window never came up into view, it was always offscreen.

I just figured out how to do it so for all of you out there who are wondering here it is:

In a module add the following:

Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Public Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ShowWindow2 Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
'
' Constants used with APIs
'
Private Const SW_SHOW = 5
Private Const SW_RESTORE = 9


Public Function ForceForegroundWindow(ByVal hWnd As Long) As Boolean
   Dim ThreadID1 As Long
   Dim ThreadID2 As Long
   Dim nRet As Long
   '
   ' Nothing to do if already in foreground.
   '
   If hWnd = GetForegroundWindow() Then
      ForceForegroundWindow = True
   Else
      '
      ' First need to get the thread responsible for this window,
      ' and the thread for the foreground window.
      '
      ThreadID1 = GetWindowThreadProcessId(GetForegroundWindow, ByVal 0&)
      ThreadID2 = GetWindowThreadProcessId(hWnd, ByVal 0&)
      '
      ' By sharing input state, threads share their concept of
      ' the active window.
      '
      If ThreadID1 <> ThreadID2 Then
         Call AttachThreadInput(ThreadID1, ThreadID2, True)
         nRet = SetForegroundWindow(hWnd)
         Call AttachThreadInput(ThreadID1, ThreadID2, False)
      Else
         nRet = SetForegroundWindow(hWnd)
      End If
      '
      ' Restore and repaint
      '
      If IsIconic(hWnd) Then
         Call ShowWindow2(hWnd, SW_RESTORE)
      Else
         Call ShowWindow2(hWnd, SW_SHOW)
      End If
      '
      ' SetForegroundWindow return accurately reflects success.
      '
      ForceForegroundWindow = CBool(nRet)
   End If
End Function

Then add the following to your form/button/whatever to actually make the window go under (actually it makes the current window go over and forces it to the foreground):

Dim lwnd As Long
lwnd = GetForegroundWindow()
Call ForceForegroundWindow(lwnd)
Never mind, the code I provided is buggy. Sometimes it does the popunder and sometimes it doesn't.  Any other ideas would be appreciated. Thanks
Try this...
Paste this code in a module and call as:
open_ie(<your address>)

Option Explicit

''''''''''''''''''''''''''''
''declaration for opening internet address
Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" _
(ByVal hWnd As Long, _
ByVal IpOperation As String, _
ByVal IpFile As String, _
ByVal IpParameters As String, _
ByVal IpDirectory As String, _
ByVal nShowCmd As Long) As Long

Public Sub open_ie(address As String)
    Call ShellExecute(0&, vbNullString, address, vbNullString, vbNullString, vbNormalNoFocus)
End Sub

The last parameter in the ShellExecute can be:
vbNormalFocus, vbMinimizeNoFocus etc.
See "Shell Constants" in the msdn library.
Hope this solves your problem
ASKER CERTIFIED SOLUTION
Avatar of RobMe
RobMe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This question has been classified as abandoned.  I will make a recommendation to the moderators on its resolution in a week or two.  I would appreciate any comments by the experts that would help me in making a recommendation.

It is assumed that any participant not responding to this request is no longer interested in its final deposition.

If the asker does not know how to close the question, the options are here:
https://www.experts-exchange.com/help/closing.jsp

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER

GPrentice00
Cleanup Volunteer
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

 -->Accept RobMe's comment as Answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER

GPrentice00
Cleanup Volunteer