Answered Alt-Tab to another window using VB.Net

  • Wednesday, August 01, 2007 7:53 AM
     
     
    Hi,

    I was wondering whether it is possible to, at a button click event, to say bring up another window that is running at the same time.

    I have a windows app, which is the ... sort of main app and there is another app which has a singular window displayed. From the main app, i want to click a button and bring the other app into focus.

    I've tried to use things like System.Diagnostics.ProcessStartInfo to get a handle on the process and tried to do a process.windowstyle = normal, but i still seem to have to alt-tab to that window.

    Is there another way of bringing up the window?

    Thanks

All Replies

  • Wednesday, August 01, 2007 11:36 AM
     
     
    Code Snippet
    Imports System.Runtime.InteropServices

    Public Class Form1

        <DllImport(
    "user32")> _
        
    Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
        End Function

        <DllImport("user32", CharSet:=CharSet.Ansi)> _
        
    Private Shared Function FindWindow( _
        <MarshalAs(UnmanagedType.LPStr)>
    ByVal className As String, _
        <MarshalAs(UnmanagedType.LPStr)>
    ByVal windowName As String) As IntPtr
        
    End Function

        Private Sub Form1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Click
            
    ' Find instances of calculator
            For Each p As Process In Process.GetProcessesByName("calc")
                
    ' Get the handle (process.handle is the process handle,
                ' we want the window handle).
                Dim hCalc As IntPtr = IntPtr.Zero
                
    ' Use Spy++ to find the Class name.
                ' Window name is what is displayed in the windows title bar.
                hCalc = FindWindow("SciCalc", "Calculator")
                
    If hCalc = IntPtr.Zero Then
                    MessageBox.Show("didn't find calculator (scientific mode)")
                    
    Exit Sub
                End If
                If SetForegroundWindow(hCalc) = False Then
                    MessageBox.Show("SetForegroundWindow failed 8(")
                
    End If
            Next
        End Sub

    End
    Class

     

     


  • Wednesday, August 01, 2007 5:07 PM
     
     

    I use the following to switch between main application and another window.

     

    this only really works if the other window is the only other application running as it is *very* basic..

     

    Code Snippet

    SendKeys.Send("%{TAB}")

     

     

  • Wednesday, August 01, 2007 11:14 PM
     
     
    Jo0ls:
    Thanks for that. it does work...but not if the other window has a window state of minimized. I do try to set the windostate to normal, using the System.Diagnostics.ProcessStartInfo class to set the ProcessWindowStyle to Normal, but that doesnt seem to get it up. It still stays minimized. Can i set it to normal using the window handle?
  • Friday, August 03, 2007 2:17 AM
     
     Answered

     

    Looks like you need 2 api calls then, one to restore it, and one to bring it to the foreground:

     


    Code Snippet
    Imports System.Runtime.InteropServices

    Public Class Form1

        
    Const SW_SHOWNORMAL = 1

        <DllImport(
    "user32")> _
        
    Private Shared Function ShowWindow(ByVal hWnd As IntPtr, ByVal showCommand As Integer) As Boolean
        End Function

        <DllImport("user32")> _
        
    Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
        End Function

        <DllImport("user32", CharSet:=CharSet.Ansi)> _
        
    Private Shared Function FindWindow( _
        <MarshalAs(UnmanagedType.LPStr)>
    ByVal className As String, _
        <MarshalAs(UnmanagedType.LPStr)>
    ByVal windowName As String) As IntPtr
        
    End Function

        Private Sub Form1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Click
            
    ' Find instances of calculator
            For Each p As Process In Process.GetProcessesByName("calc")
                
    ' Get the handle (process.handle is the process handle,
                ' we want the window handle).
                Dim hCalc As IntPtr = IntPtr.Zero
                
    ' Use Spy++ to find the Class name.
                ' Window name is what is displayed in the windows title bar.
                hCalc = FindWindow("SciCalc", "Calculator")
                
    If hCalc = IntPtr.Zero Then
                    MessageBox.Show("didn't find calculator (scientific mode)")
                    
    Exit Sub
                End If
                If ShowWindow(hCalc, SW_SHOWNORMAL) = False Then
                    MessageBox.Show("ShowWindow failed 8(")
                
    End If
                If SetForegroundWindow(hCalc) = False Then
                    MessageBox.Show("SetForegroundWindow failed 8(")
                
    End If
            Next
        End Sub

    End
    Class

     

     

  • Friday, August 03, 2007 2:38 AM
     
     
    Cool...thanks for that guys!! Really appreciated.
  • Monday, August 13, 2007 3:03 PM
     
     

    one day i was working in console application & i don't know what happened suddenly