RESTORE_DOWN Bring Window To The Front

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,060
I have the following code to bring a window to the front.

I have searched for a way to RESTOREDOWN/ RESTORE_DOWN rather than just restore, does anyone know of a script to restore down.

I use restore down with Internet Explorer, I have my window set to a specific size and location on the screen.

Code:
Option Private Module
Private Declare Function FindWindowA _
   Lib "User32.dll" _
     (ByVal lpszClass As String, _
      ByVal lpszWindow As String) As Long
 
'Checks if the window is minimized to the TaskBar
 Private Declare Function IsIconic _
   Lib "User32.dll" _
     (ByVal hWnd As Long) As Long
 
'Returns the handle to the window currently receiving input
 Private Declare Function GetForegroundWindow _
   Lib "User32.dll" () As Long
'Directs User Input to the specified Window
'Success gives a Non Zero Return Value
 Public Declare Function SetForegroundWindow _
  Lib "User32.dll" _
   (ByVal hWnd As Long) As Long
 
'Bring Window to the Top of the Z-Order
 Private Declare Function BringWindowToTop _
  Lib "User32.dll" _
   (ByVal hWnd As Long) As Long
 Private Declare Function AttachThreadInput _
   Lib "User32.dll" _
     (ByVal idAttach As Long, _
      ByVal idAttachTo As Long, _
      ByVal fAttach As Long) As Long
 
'The return value is the identifier of the thread that created the window.
 Private Declare Function GetWindowThreadProcessId _
   Lib "User32.dll" _
     (ByVal hWnd As Long, _
      ByRef lpdwProcessID As Long) As Long
'Function to Change how Window is Displayed
 Private Declare Function ShowWindow _
  Lib "User32.dll" _
   (ByVal hWnd As Long, _
    ByVal nCmdShow As Long) As Long
 
'Constants for ShowWindow (nCmdShow)
 Const SW_HIDDEN As Long = 0
 Const SW_NORMAL As Long = 1
 Const SW_MINIMIZED As Long = 2
 Const SW_MAXIMIZED As Long = 3
 Const SW_NOTACTIVE As Long = 4
 Const SW_UNHIDDEN As Long = 5
 Const SW_MINWITHFOCUS As Long = 6
 Const SW_MINNOTACTIVE As Long = 7
 Const SW_RESTORE As Long = 9
 Private Declare Function GetWindow _
   Lib "User32.dll" _
     (ByVal hWnd As Long, _
      ByVal wCmd As Long) As Long
 Private Declare Function GetWindowText _
   Lib "User32.dll" _
     Alias "GetWindowTextA" _
       (ByVal hWnd As Long, _
        ByVal lpString As String, _
        ByVal nMaxCount As Long) As Long
 Private Declare Function GetDesktopWindow _
   Lib "User32.dll" () As Long
Function GetHwnd(Window_Title As String) As Long
  Dim hWnd As Long
  Dim RetVal As Long
  Dim Title As String
 
  Const GW_HWNDNEXT As Long = 2
  Const GW_CHILD As Long = 5
 
    hWnd = GetWindow(GetDesktopWindow, GW_CHILD)
 
    While hWnd
      Title = String(512, Chr$(0))
      RetVal = GetWindowText(hWnd, Title, Len(Title))
        If LCase(Title) Like "*" & LCase(Window_Title) & "*" Then
           GetHwnd = hWnd: Exit Function
        End If
      hWnd = GetWindow(hWnd, GW_HWNDNEXT)
    Wend
End Function
Sub ActivateWindow(ByVal Window_Title As String)
  Dim AppHwnd As Long
  Dim NewPID As Long
  Dim NewAppThreadID As Long
  Dim RetVal As Long
  Dim ThisPID As Long
  Dim ThisAppThreadID As Long
  Dim WndTitle As String
 
    'Build the Window Title
     WndTitle = Window_Title
 
    'Get a handle on the Window you want to send the mouse clicks to
     AppHwnd = GetHwnd(WndTitle)
 
    'Was window found?
     If AppHwnd <> 0 Then
       'Application window found by the caption
        ThisAppThreadID = GetWindowThreadProcessId(GetForegroundWindow, ThisPID)
        NewAppThreadID = GetWindowThreadProcessId(AppHwnd, NewPID)
 
       'Attach the other Application's thread input to this one
        Call AttachThreadInput(ThisAppThreadID, NewAppThreadID, True)
 
       'Make the other Application the top window
        RetVal = SetForegroundWindow(AppHwnd)
 
       'Detach the other Application's thread input
        Call AttachThreadInput(ThisAppThreadID, NewAppThreadID, False)
 
          If RetVal <> 0 Then
            'Maximize the window if it's been minimized
            'or just show it if it's already opened.
             If IsIconic(AppHwnd) Then
               Call ShowWindow(AppHwnd, SW_RESTORE)
 
               ' was set to    Call ShowWindow(AppHwnd, SW_MAXIMIZE)
 
             'Else
               'Call ShowWindow(AppHwnd, SW_RESTORE)
             End If
          Else
            'MsgBox "Can't Bring Window to the Foreground."
            Call MyWindowActivate
          End If
     Else
       'Failed to find the window caption
       '(the app is probably closed or the wrong window name is passed)
        MsgBox "Application Window '" & WndTitle & "' Not Found."
     End If
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi Jaafar,

In Internet Explorer and Firefox on the title bar (right hand side)the middle button gives you the option to restore down, which minimizes the screen to your custom size and position, so if you manually dragged the window size to your own height, restore down reduces it from fullscreen size (with toolbars etc..) to your custom size.
 
Upvote 0
Hi Jaafar,

In Internet Explorer and Firefox on the title bar (right hand side)the middle button gives you the option to restore down, which minimizes the screen to your custom size and position, so if you manually dragged the window size to your own height, restore down reduces it from fullscreen size (with toolbars etc..) to your custom size.

Ok. The down word confused me. I believe you need to get the Hwnd of the window and Post to it a WM_SYSCOMMAND Msg with the value of SC_RESTORE in the wParam. See if you can give this a try and post back if any problems.
 
Upvote 0
Hi again Jaafar,

I am not sure how to do this, can you please provide an example.
 
Upvote 0
Hi again Jaafar,

I am not sure how to do this, can you please provide an example.


This generic Sub should Max,Min or Restore the window as required :

Code:
Declare Function PostMessage Lib "user32.dll" _
Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long

Const WM_SYSCOMMAND As Long = &H112

Enum SYSCOMMAND
    MAXIMIZE = &HF030&
    MINIMIZE = &HF020&
    Restore = &HF120&
End Enum

Sub MaxMinRes(ByVal hwnd As Long, ByVal SysCom As SYSCOMMAND)
    Call PostMessage(hwnd, WM_SYSCOMMAND, SysCom, 0)
End Sub


The test macro below shows how to use the above generic MaxMinRes to Restore the excel window : (Replace the Excel hwnd with the IE or FireFox Hwnd )

Code:
Sub Test()
    Call MaxMinRes(Application.hwnd, SYSCOMMAND.Restore)
End Sub
 
Upvote 0
Sorry to hassle your Jaafar but I am struggling with incorporating your code into my procedure.

The procedure I usually use to bring a window to the front follows.

Code:
Option Private Module
Private Declare Function FindWindowA _
   Lib "user32.dll" _
     (ByVal lpszClass As String, _
      ByVal lpszWindow As String) As Long
 
'Checks if the window is minimized to the TaskBar
 Private Declare Function IsIconic _
   Lib "user32.dll" _
     (ByVal hwnd As Long) As Long
 
'Returns the handle to the window currently receiving input
 Private Declare Function GetForegroundWindow _
   Lib "user32.dll" () As Long
'Directs User Input to the specified Window
'Success gives a Non Zero Return Value
 Public Declare Function SetForegroundWindow _
  Lib "user32.dll" _
   (ByVal hwnd As Long) As Long
 
'Bring Window to the Top of the Z-Order
 Private Declare Function BringWindowToTop _
  Lib "user32.dll" _
   (ByVal hwnd As Long) As Long
 Private Declare Function AttachThreadInput _
   Lib "user32.dll" _
     (ByVal idAttach As Long, _
      ByVal idAttachTo As Long, _
      ByVal fAttach As Long) As Long
 
'The return value is the identifier of the thread that created the window.
 Private Declare Function GetWindowThreadProcessId _
   Lib "user32.dll" _
     (ByVal hwnd As Long, _
      ByRef lpdwProcessID As Long) As Long
'Function to Change how Window is Displayed
 Private Declare Function ShowWindow _
  Lib "user32.dll" _
   (ByVal hwnd As Long, _
    ByVal nCmdShow As Long) As Long
 
'Constants for ShowWindow (nCmdShow)
 Const SW_HIDDEN As Long = 0
 Const SW_NORMAL As Long = 1
 Const SW_MINIMIZED As Long = 2
 Const SW_MAXIMIZED As Long = 3
 Const SW_NOTACTIVE As Long = 4
 Const SW_UNHIDDEN As Long = 5
 Const SW_MINWITHFOCUS As Long = 6
 Const SW_MINNOTACTIVE As Long = 7
 Const SW_RESTORE As Long = 9
 Private Declare Function GetWindow _
   Lib "user32.dll" _
     (ByVal hwnd As Long, _
      ByVal wCmd As Long) As Long
 Private Declare Function GetWindowText _
   Lib "user32.dll" _
     Alias "GetWindowTextA" _
       (ByVal hwnd As Long, _
        ByVal lpString As String, _
        ByVal nMaxCount As Long) As Long
 Private Declare Function GetDesktopWindow _
   Lib "user32.dll" () As Long
Function GetHwnd(Window_Title As String) As Long
  Dim hwnd As Long
  Dim RetVal As Long
  Dim Title As String
 
  Const GW_HWNDNEXT As Long = 2
  Const GW_CHILD As Long = 5
 
    hwnd = GetWindow(GetDesktopWindow, GW_CHILD)
 
    While hwnd
      Title = String(512, Chr$(0))
      RetVal = GetWindowText(hwnd, Title, Len(Title))
        If LCase(Title) Like "*" & LCase(Window_Title) & "*" Then
           GetHwnd = hwnd: Exit Function
        End If
      hwnd = GetWindow(hwnd, GW_HWNDNEXT)
    Wend
End Function
Sub ActivateWindow(ByVal Window_Title As String)
  Dim AppHwnd As Long
  Dim NewPID As Long
  Dim NewAppThreadID As Long
  Dim RetVal As Long
  Dim ThisPID As Long
  Dim ThisAppThreadID As Long
  Dim WndTitle As String
 
    'Build the Window Title
     WndTitle = Window_Title
 
    'Get a handle on the Window you want to send the mouse clicks to
     AppHwnd = GetHwnd(WndTitle)
 
    'Was window found?
     If AppHwnd <> 0 Then
       'Application window found by the caption
        ThisAppThreadID = GetWindowThreadProcessId(GetForegroundWindow, ThisPID)
        NewAppThreadID = GetWindowThreadProcessId(AppHwnd, NewPID)
 
       'Attach the other Application's thread input to this one
        Call AttachThreadInput(ThisAppThreadID, NewAppThreadID, True)
 
       'Make the other Application the top window
        RetVal = SetForegroundWindow(AppHwnd)
 
       'Detach the other Application's thread input
        Call AttachThreadInput(ThisAppThreadID, NewAppThreadID, False)
 
          If RetVal <> 0 Then
            'Maximize the window if it's been minimized
            'or just show it if it's already opened.
             If IsIconic(AppHwnd) Then
               Call ShowWindow(AppHwnd, SW_RESTORE)
 
               ' was set to    Call ShowWindow(AppHwnd, SW_MAXIMIZE)
 
             'Else
               'Call ShowWindow(AppHwnd, SW_RESTORE)
             End If
          Else
            'MsgBox "Can't Bring Window to the Foreground."
            Call MyWindowActivate
          End If
     Else
       'Failed to find the window caption
       '(the app is probably closed or the wrong window name is passed)
        MsgBox "Application Window '" & WndTitle & "' Not Found."
     End If
End Sub
 
 
Sub MyWindowActivate()
If UserForm1.TextBox7.Value = "Explore" Then
res = IsWindowOpen("Internet Explorer")
End If
If UserForm1.TextBox7.Value = "Fire" Then
res = IsWindowOpen("Firefox")
End If
If UserForm1.TextBox7.Value = "Hide" Then
res = IsWindowOpen("Hotkey")
End If
If UserForm1.TextBox7.Value = "IBS" Then
res = IsWindowOpen("ibs")
End If
 
'res = IsWindowOpen("movie")
End Sub
 
Upvote 0
Jaye7,

Just retrieve the handle to the target window and pass it to the MaxMinRes Procedure as its first argument.

Where are you calling the code from ?
 
Upvote 0
Hi Jaafar,

I test it using the following.

Internet explorer window comes to the front and after the 3 seconds the script works but instead of restoring down Internet Explorer it restores down Excel.

Code:
Sub Restore_Window_Down()
UserForm1.TextBox7.Value = "Explore"
res = IsWindowOpen("Internet Explorer")
ActivateWindow "Internet Explorer"
KeyPlusKey 17, Asc(Workbooks("Personal.xls").Worksheets("LINKS").Range("G20").Value + 1) 'SELECT SPECIFIC TAB
Application.Wait Now + TimeValue("00:00:03")
    Call MaxMinRes(Application.hwnd, SYSCOMMAND.Restore)
End Sub
 
Upvote 0
Try adding th line below at the end of the ActivateWindow routine

Code:
Call MaxMinRes(AppHwnd, Restore)
 
Upvote 0

Forum statistics

Threads
1,216,073
Messages
6,128,644
Members
449,461
Latest member
kokoanutt

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top