How to switch to another application from mine

Hi, I want to be able to switch to another already running application when a user presses a button on a main form.

I can enumerate the running processes and see that the one I want is running, I can get its Window Handle.

How can I switch to it.

[263 byte] By [Rabtok] at [2008-1-7]
# 1

If you know the window handle, you can use the SetActiveWindow() SetForegroundWindow method to do this. You'll need to include it from user32.dll

Declare Function SetActiveWindow SetForegroundWindow Lib "user32" (ByVal hwnd As Integer) As Integer

From your code

SetActiveWindow SetForegroundWindow(hwnd)

I haven't tested this, but I'm pretty sure it will do the trick.

Regards,

<Corrected>

SvenDeBont at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2
you could also use the SetForegroundWindow API, I think they both do the same job
ahmedilyas at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

ahmedilyas wrote:
you could also use the SetForegroundWindow API, I think they both do the same job

Thanks for pointing that out.. you should use the SetForegroundWindow to activate another application. SetActiveWindow only works for applications in the foreground. My mistake.

SvenDeBont at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

:-) Glad we shared and learned something from each other - that's what it's about!

http://www.pinvoke.net/default.aspx/user32/SetForegroundWindow.html

Sample here on how to implement the API:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=611602&SiteID=1

ahmedilyas at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5
ahmedilyas wrote:

:-) Glad we shared and learned something from each other - that's what it's about!

http://www.pinvoke.net/default.aspx/user32/SetForegroundWindow.html

Sample here on how to implement the API:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=611602&SiteID=1

Tried the above and it works...Except...the application I am trying to bring to the front can be minimised to the system tray, when it is it will not come to the front. Anyone know how to get round this.

Thanks.

Rabtok at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 6

You can use the ShowWindow API for this

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As Int32) As Long

To restore a window, use the value 9 from your code

ShowWindow(hwnd, 9)

See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/WindowsUserInterface/Windowing/Windows/WindowReference/WindowFunctions/ShowWindow.asp for an explantion and possible values for the nCmdShow parameter.

Regards,

SvenDeBont at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 7
Sven De Bont wrote:

You can use the ShowWindow API for this

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As Int32) As Long

To restore a window, use the value 9 from your code

ShowWindow(hwnd, 9)

See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/WindowsUserInterface/Windowing/Windows/WindowReference/WindowFunctions/ShowWindow.asp for an explantion and possible values for the nCmdShow parameter.

Regards,

Many thanks. However it leaves the program so called (iTunes in this case) unable to use menus or system min/max buttons, you can only use keyboard Alt+key to select them. tried a few values other than 9.

Any one experienced this.

Final working code:

Public Enum ShowWindowConstants
SW_HIDE = 0
SW_SHOWNORMAL = 1
SW_NORMAL = 1
SW_SHOWMINIMIZED = 2
SW_SHOWMAXIMIZED = 3
SW_MAXIMIZE = 3
SW_SHOWNOACTIVATE = 4
SW_SHOW = 5
SW_MINIMIZE = 6
SW_SHOWMINNOACTIVE = 7
SW_SHOWNA = 8
SW_RESTORE = 9
SW_SHOWDEFAULT = 10
SW_FORCEMINIMIZE = 11
SW_MAX = 11
End Enum

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SetActiveWindow(ByVal hWnd As IntPtr) As IntPtr
End Function
Declare Sub SwitchToThisWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal fAltTab As Boolean)
Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal nCmdShow As Int32) As Long
<DllImport("User32.dll")> _
Public Shared Function ShowWindowAsync(ByVal hWnd As IntPtr, ByVal swCommand As Integer) As Integer
End Function
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function IsWindowVisible(ByVal hwnd As IntPtr) As Boolean
End Function

Private Sub bnSwitch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnSwitch.Click
Dim hwnd As Integer

hwnd = FindWindow(Nothing, "iTunes")

If hwnd > 0 Then
If IsWindowVisible(hwnd) = False Then
ShowWindowAsync(hwnd, ShowWindowConstants.SW_MAXIMIZE)
End If

SwitchToThisWindow(hwnd, True)
REM SetForegroundWindow(hwnd)
REM SetActiveWindow(hwnd)
End If
end sub

Rabtok at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 8

If your code is working, could you mark the reply as 'answer' so the thread appears as solved to other users?

Thanks,

SvenDeBont at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 9
Sven De Bont wrote:

If your code is working, could you mark the reply as 'answer' so the thread appears as solved to other users?

Thanks,


The problem is not yet completely resolved, a question remains, once the problem is solved I will mark it as solved. Otherwise people will think it resolved and not view or contibute to it.
Rabtok at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 10

Changing the SW_MAXIMISE to SW_NORMAL performs a little better, you can still get the mouse to use menus and it can click the min/max and the button activates (appears to depress) but no longer responds in that nothing happens. What concerns me is that it renders the called application a little helpless. Does anyone have experience of this and know a fix.

Rabtok at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 11

Answer:

In the case of iTunes as it is a single instance application it responds perfectly to:

Dim MusicProc As New Process

MusicProc.StartInfo.FileName = "C:\Program Files\iTunes\iTunes.exe"
MusicProc.StartInfo.WindowStyle = ProcessWindowStyle.Normal
MusicProc.Start()

The other methods are flawed in that they "damage" the running application. I would still like to know what is wrong and what fixes it but will concede the answer as it stands above.

Rabtok at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 12

@iTunes:

Just a joke, but shouldn't you be a bit more patriotic on a Microsoft forum?

plz dont flame
unMatChed at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...