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]
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>
you could also use the SetForegroundWindow API, I think they both do the same job
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.
ahmedilyas wrote: |
|
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.
Sven De Bont wrote: |
|
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
If your code is working, could you mark the reply as 'answer' so the thread appears as solved to other users?
Thanks,
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.
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.
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.
@iTunes:
Just a joke, but shouldn't you be a bit more patriotic on a Microsoft forum? 
plz dont flame