Results 1 to 10 of 10

Thread: AppActivate and FindWindow

  1. #1
    Lively Member
    Join Date
    Oct 09
    Posts
    71

    AppActivate and FindWindow

    I have used AppActivate in the past with VB6 Projects and am able to do same successfully in VB.Net.

    When using AppActivate I just use AppActivate("My Form Name Here") and a Try/Catch Block

    I reading some forums I see that there is a better way to do same in VB.Net using code similar to the following:

    Question is - What do I use for lpszParentClass and lpszParentWindow?
    I assume lpszParentWindow="My Form Name Here" - that's what I see in Task Manager 'Applications'


    Code:
           ParenthWnd = FindWindow(lpszParentClass, lpszParentWindow)
    
            If ParenthWnd.Equals(IntPtr.Zero) Then
                MsgBox("Program Not Running!")
            Else
                ' Found it, so echo that we found it to debug window
                ' Then set it to foreground
                MsgBox("Program Found")
                SetForegroundWindow(ParenthWnd)
            End If

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,512

    Re: AppActivate and FindWindow

    vb.net uses an entirely different protocol

    Dim p As Process = Process.Start("notepad.exe")

    You can then access a whole bunch of properties including p.MainWindowHandle although you shouldn't need API functions for most purposes.

  3. #3
    Lively Member
    Join Date
    Oct 09
    Posts
    71

    Re: AppActivate and FindWindow

    Don't want to start a second instance of the program - just want to bring it to foreground if running or start the program if not running.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,512

    Re: AppActivate and FindWindow

    Yes, I know. You use Process.Start instead of AppActivate and then you have access to all the properties you need. Bringing the process to the top is default behaviour using this method so that cuts out half the problem. If you want to wait for the program to be fully activated you use .WaitForInputIdle or check it's responding with .Responding

    If you're going to use VB.Net, use it! It's not just a bigger version of VB6.

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,512

    Re: AppActivate and FindWindow

    Yes, I know. You use Process.Start instead of AppActivate and then you have access to all the properties you need. Bringing the process to the top is default behaviour using this method so that cuts out half the problem. If you want to wait for the program to be fully activated you use .WaitForInputIdle or check it's responding with .Responding

    If you're going to use VB.Net, use it! It's not just a bigger version of VB6.

  6. #6
    Lively Member
    Join Date
    Oct 09
    Posts
    71

    Re: AppActivate and FindWindow

    OK then please show me some code to set focus to an existing instance of MyProgram.exe or if it's not running then do Process.start("MyProgram.exe")

    Thanks

  7. #7
    Lively Member
    Join Date
    Oct 09
    Posts
    71

    Re: AppActivate and FindWindow

    OK then please show me some code to set focus to an existing instance of MyProgram.exe or if it's not running then do Process.start("MyProgram.exe")

    Thanks

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,512

    Re: AppActivate and FindWindow

    vb.net Code:
    1. Dim p As Process() = Process.GetProcessesByName("notepad")
    2.         If p.Length = 0 Then
    3.             Process.Start("notepad.exe")
    4.         End If

  9. #9
    PowerPoster Edgemeal's Avatar
    Join Date
    Sep 06
    Location
    WindowFromPoint(x,y)
    Posts
    3,135

    Re: AppActivate and FindWindow

    Code:
    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
        Public Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
        End Function
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim hWnd As IntPtr = FindWindowByExe("notepad.exe")
            If Not hWnd = IntPtr.Zero Then ' found
                SetForegroundWindow(hWnd) ' bring to foreground
            Else
                Process.Start("notepad.exe")
            End If
        End Sub
    
        Private Function FindWindowByExe(ByVal Filename As String) As IntPtr
            Filename = System.IO.Path.GetFileNameWithoutExtension(Filename)
            Dim procList() As Process = Process.GetProcesses
            For Each p As Process In procList
                If p.ProcessName.ToLower = Filename.ToLower Then
                    Return p.MainWindowHandle
                End If
            Next
            Return IntPtr.Zero
        End Function
    
    End Class

  10. #10
    Lively Member
    Join Date
    Oct 09
    Posts
    71

    Re: AppActivate and FindWindow

    Edgemeal:
    Exactly what I was looking for. The FindWindowByExe was part I was missing.

    Thanks.

    Lee

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •