Using the following code below I am attempting to close the current program and re-open a minimized program. If the initial program was closed by accident or on purpose, it will reopen it upon closing the current program.
FACTS:
There are multiple .exe files within the compiled program. There is a menu program that opens at start up and the user starts their chosen sub program from that menu.
EXAMPLE:
GOAL:
SUCCESS:
When choosing a sub program from the menu (Program 'A') Program 'B' opens and Program 'A' minimizes.
When closing Program 'B' it fully closes.
FAILURES:
After electing to close Program 'B', Program 'B' is always opening a second incident of Program 'A' in a minimized state resulting in multiple program 'A's running in a minimized state. Program 'B' does fully close leaving me with multiple and minimized Program 'A's.
What I have tried:
FACTS:
There are multiple .exe files within the compiled program. There is a menu program that opens at start up and the user starts their chosen sub program from that menu.
EXAMPLE:
- Program 'A' is opened (This is a menu program).
- Once a selection is made, Program 'A' should minimize and open Program 'B'.
- Once Program 'B' is closed Program 'A' should come out of the minimized state ready for another user input.
GOAL:
- Before fully closing Program 'B' check 1st to see if Program 'A' is open and in a minimized state, if true then bring Program 'A' back to the forefront and open in it's normal open state.
- Before fully closing Program 'B' check to ensure Program 'A' was not fully closed for any reason and if it was then re-start Program 'A'.
- Whichever is true of the first 2 statements, once Program 'A' opens from a minimized state or is re-opened then close or kill Program 'B'.
SUCCESS:
When choosing a sub program from the menu (Program 'A') Program 'B' opens and Program 'A' minimizes.
When closing Program 'B' it fully closes.
FAILURES:
After electing to close Program 'B', Program 'B' is always opening a second incident of Program 'A' in a minimized state resulting in multiple program 'A's running in a minimized state. Program 'B' does fully close leaving me with multiple and minimized Program 'A's.
What I have tried:
Code:
Option Strict On
Imports System.IO
Imports System.Runtime.InteropServices
Public Class DataEntry
Private Const WM_SYSCOMMAND As Integer = &H112
Private Const SC_MINIMIZE As Integer = &HF020
Private Const SC_MAXIMIZE As Integer = &HF030
Private Const SC_CLOSE As Integer = &HF060
Private Const SC_RESTORE As Integer = &HF120
<DllImport("user32.dll", EntryPoint:="SendMessageW")>
Private Shared Function SendMessageW(hWnd As IntPtr, Msg As UInteger, wParam As UInteger, lParam As Integer) As Integer
End Function
<DllImport("user32.dll")> Private Shared Function IsIconic(hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
btnExit.BackgroundImage = My.Resources.ButtonClk_Red_Trans
Dim Console As Process = Process.GetProcessesByName("PETS Console").FirstOrDefault
If Console IsNot Nothing Then
If IsIconic(Console.MainWindowHandle) Then 'check if the window is minimized. If it is not, then you don't want to restore it, you only need to activate it.
SendMessageW(Console.MainWindowHandle, WM_SYSCOMMAND, SC_RESTORE, 0) 'restore the window from it's minimized state
Else
AppActivate(Console.Id) 'activate it to bring it to the front of all other windows
End If
Else
'If Console is not open due to accidental closing, start the program on close.
Process.Start("C:\MyProgramPath\PETS Console.exe")
End If
Close()
End Sub
End Class