#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Location
    Sweden, Örebro.
    Posts
    3
    Rep Power
    0

    Question Backward button to go back to form from opened .exe program.


    Hi!

    I am currently working on a program that has several buttons in it lined up. Each button goes to different applications (.exe programs). The program I am making is suppose to be a fullscreen program and won't let you see any of Windows when it's running. Therefore I want to make a "back" button that always stays on top of any application and when I push it it will take me back to my WindowsForm (startpage of the program). Any ideas?

    I am also wondering if you can make it so all .exe programs only can run ONCE. So if I press one of the buttons saying "open notepad" I want it to work so it can't open Notepad once again making it two notepads open. Just go back to the first Notepad opened. But if I close notepad I wanna be able to open it again trough one of my buttons.

    Thank you for your time! // Kevin.
  2. #2
  3. Type Cast Exception
    Devshed Supreme Being (6500+ posts)

    Join Date
    Apr 2004
    Location
    OAKLAND CA | Adam's Point (Fairyland)
    Posts
    14,954
    Rep Power
    8617
    You're going to need a lot of API to pull off all your requirements. I frankly don't know where to begin telling you to begin, except suggesting that a lower level language (C based) would be the better tool for this job.
    medialint.com

    “Today you are You, that is truer than true. There is no one alive who is Youer than You.” - Dr. Seuss
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    44
    I am not sure what you mean by the first part, but to ensure that an application only starts once, I use the following:
    Code:
    Option Explicit
    'Module1 code
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
    Declare Function GetForegroundWindow Lib "user32" () As Long
    Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
    Declare Function GetCurrentProcessId Lib "kernel32" () As Long
    Declare Function WaitForInputIdle Lib "user32" (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long
    Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
    Declare Function GetCurrentThreadId Lib "kernel32" () As Long
    Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
    Declare Function GetDesktopWindow Lib "user32" () As Long
    Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
    Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
    'frmTest code
    Private Sub cmdNotePad_Click()
        Dim Title$, A$
        Dim TaskID As Long
        Dim ErrorCode As Long
        On Error GoTo NotePadActErr
        'Load FlashBox
        Title$ = "Untitled - Notepad"
        TaskID = CheckUnique(Title$, 0)
        If TaskID = 0 Then
    '        FlashBox.Show 0
    '        DoEvents
            Screen.MousePointer = 11
            A$ = "\windows\system32" + "\NOTEPAD.EXE"
            If Len(Command$) > 0 Then
                A$ = A$ + " " + Command$
            End If
            TaskID = Shell(A$, 1)
            Screen.MousePointer = 0
            'Unload FlashBox
        ElseIf TaskID < 0 Then
            MsgBox "Program " + Title$ + "is active but not able to take focus.", 64
        End If
        Exit Sub
    NotePadActErr:
        ErrorCode = Err
        If ErrorCode = 53 Then
            MsgBox "NotePad.exe Program could not be found!", 16
            Screen.MousePointer = 0
    '        Unload FlashBox
        Else
    '        Call LogError("^" + Str$(Err) + " Activate Acct")
    '        Call FatalError(ErrorCode)
    '        End
        End If
    End Sub
    
    Function CheckUnique(ByVal FormName As String, hIgnore As Long) As Long
    ' Purpose:
        'FormName is the caption of the desired form, hIgnore is the
        'window handle of the parent form to be ignored if already running.
    ' =====================================================
        Dim hWnd As Long
        Dim ShowW As Long, SetF As Long, Pid As Long
        CheckUnique = 0
        hWnd = FindWindow(vbNullString, FormName)
        If hWnd = 0 Then
            Exit Function
        Else
            ShowW = ShowWindow(hWnd, 9) 'Restore it in case it is minimized
        End If
        ShowW = hWnd        'Save original handle
        SetF = GetForegroundWindow()    'Does not always return current app
        If hIgnore = frmTest.hWnd Then
            SetF = SetForegroundWindow(hWnd)
            SetF = GetCurrentProcessId()
            SetF = WaitForInputIdle(SetF, 10000)
            hWnd = GetForegroundWindow()
        Else
            hWnd = SetF
        End If
        SetF = GetOwnedWindow(hWnd) 'Get owned top level window
        If SetF = 0 Then            'No owned Windows found
            CheckUnique = hWnd
            ShowW = SetForegroundWindow(ShowW)  'SetFocus to FormName
        Else
            CheckUnique = -SetF  'return neg handle for owned window
            SetF = GetWindowThreadProcessId(SetF, Pid)
            SetF = AttachThreadInput(SetF, GetCurrentThreadId(), Pid)
        End If
        Exit Function
    End Function
    
    Private Function GetOwnedWindow(hWnd As Long) As Long
        Dim OwnedHandle As Long
        OwnedHandle = GetDesktopWindow()    'get the desktop handle
        OwnedHandle = GetWindow(OwnedHandle, 5) ' get first top level window
        Do
            If GetParent(OwnedHandle) = hWnd Then
                GetOwnedWindow = OwnedHandle
                Exit Do
            End If
            OwnedHandle = GetWindow(OwnedHandle, 2) 'get next top level window
        Loop Until OwnedHandle = 0
    End Function
    Code above is for one form (frmTest) and one module (module1). Notepad is not a very good example, because each Notepad window can have a different title depending on the file it is editing. But the example above will activate an "Untitled - Notepad" window and switch focus to it. If it already exits, focus is switched to it. If it is minimized, it is restored.

    J.A. Coutts
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Location
    Sweden, Örebro.
    Posts
    3
    Rep Power
    0

    Exclamation For a non internal windows program


    Originally Posted by couttsj
    I am not sure what you mean by the first part, but to ensure that an application only starts once, I use the following:
    Code:
    Option Explicit
    'Module1 code
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
    Declare Function GetForegroundWindow Lib "user32" () As Long
    Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
    Declare Function GetCurrentProcessId Lib "kernel32" () As Long
    Declare Function WaitForInputIdle Lib "user32" (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long
    Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
    Declare Function GetCurrentThreadId Lib "kernel32" () As Long
    Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
    Declare Function GetDesktopWindow Lib "user32" () As Long
    Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
    Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
    'frmTest code
    Private Sub cmdNotePad_Click()
        Dim Title$, A$
        Dim TaskID As Long
        Dim ErrorCode As Long
        On Error GoTo NotePadActErr
        'Load FlashBox
        Title$ = "Untitled - Notepad"
        TaskID = CheckUnique(Title$, 0)
        If TaskID = 0 Then
    '        FlashBox.Show 0
    '        DoEvents
            Screen.MousePointer = 11
            A$ = "\windows\system32" + "\NOTEPAD.EXE"
            If Len(Command$) > 0 Then
                A$ = A$ + " " + Command$
            End If
            TaskID = Shell(A$, 1)
            Screen.MousePointer = 0
            'Unload FlashBox
        ElseIf TaskID < 0 Then
            MsgBox "Program " + Title$ + "is active but not able to take focus.", 64
        End If
        Exit Sub
    NotePadActErr:
        ErrorCode = Err
        If ErrorCode = 53 Then
            MsgBox "NotePad.exe Program could not be found!", 16
            Screen.MousePointer = 0
    '        Unload FlashBox
        Else
    '        Call LogError("^" + Str$(Err) + " Activate Acct")
    '        Call FatalError(ErrorCode)
    '        End
        End If
    End Sub
    
    Function CheckUnique(ByVal FormName As String, hIgnore As Long) As Long
    ' Purpose:
        'FormName is the caption of the desired form, hIgnore is the
        'window handle of the parent form to be ignored if already running.
    ' =====================================================
        Dim hWnd As Long
        Dim ShowW As Long, SetF As Long, Pid As Long
        CheckUnique = 0
        hWnd = FindWindow(vbNullString, FormName)
        If hWnd = 0 Then
            Exit Function
        Else
            ShowW = ShowWindow(hWnd, 9) 'Restore it in case it is minimized
        End If
        ShowW = hWnd        'Save original handle
        SetF = GetForegroundWindow()    'Does not always return current app
        If hIgnore = frmTest.hWnd Then
            SetF = SetForegroundWindow(hWnd)
            SetF = GetCurrentProcessId()
            SetF = WaitForInputIdle(SetF, 10000)
            hWnd = GetForegroundWindow()
        Else
            hWnd = SetF
        End If
        SetF = GetOwnedWindow(hWnd) 'Get owned top level window
        If SetF = 0 Then            'No owned Windows found
            CheckUnique = hWnd
            ShowW = SetForegroundWindow(ShowW)  'SetFocus to FormName
        Else
            CheckUnique = -SetF  'return neg handle for owned window
            SetF = GetWindowThreadProcessId(SetF, Pid)
            SetF = AttachThreadInput(SetF, GetCurrentThreadId(), Pid)
        End If
        Exit Function
    End Function
    
    Private Function GetOwnedWindow(hWnd As Long) As Long
        Dim OwnedHandle As Long
        OwnedHandle = GetDesktopWindow()    'get the desktop handle
        OwnedHandle = GetWindow(OwnedHandle, 5) ' get first top level window
        Do
            If GetParent(OwnedHandle) = hWnd Then
                GetOwnedWindow = OwnedHandle
                Exit Do
            End If
            OwnedHandle = GetWindow(OwnedHandle, 2) 'get next top level window
        Loop Until OwnedHandle = 0
    End Function
    Code above is for one form (frmTest) and one module (module1). Notepad is not a very good example, because each Notepad window can have a different title depending on the file it is editing. But the example above will activate an "Untitled - Notepad" window and switch focus to it. If it already exits, focus is switched to it. If it is minimized, it is restored.

    J.A. Coutts
    Ok! Thx man this is great really. But how would you do this example if there was a program called: trombmobil.exe and was located in C://Program Files/Tromb 3.1/trombmobil.exe

    This also a program that I only want to be able to open once, I should have this fixed, but there is something I am doing wrong, any help would be appreciated. Thx in advance.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    44
    Originally Posted by Kevax
    Ok! Thx man this is great really. But how would you do this example if there was a program called: trombmobil.exe and was located in C://Program Files/Tromb 3.1/trombmobil.exe

    This also a program that I only want to be able to open once, I should have this fixed, but there is something I am doing wrong, any help would be appreciated. Thx in advance.
    The name of the executable doesn't enter into it; it is the name in the title bar or form Caption that is used to determine if a particular window is loaded or not.

    J.A. Coutts
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Location
    Sweden, Örebro.
    Posts
    3
    Rep Power
    0

    Ok!


    Originally Posted by couttsj
    The name of the executable doesn't enter into it; it is the name in the title bar or form Caption that is used to determine if a particular window is loaded or not.

    J.A. Coutts
    Ok, now I get it! Thank you so much for your time

IMN logo majestic logo threadwatch logo seochat tools logo