Results 1 to 6 of 6

Thread: Opening a Program from a minimized state then closing active program

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    77

    Opening a Program from a minimized state then closing active program

    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:
    • 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:
    1. 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.
    2. 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'.
    3. 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
    Last edited by K3JAE; Feb 28th, 2021 at 01:45 PM.

  2. #2
    Frenzied Member Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    2,035

    Re: Opening a Program from a minimized state then closing active program

    Hi K3JAE,

    First thing I'd do is: Program 'A' Form1.vb > Tab 'Project' > '(Program 'A') Properties' > Application

    Scroll down and put a tick in the box 'Make single instance application'.

    You will obviously need to run your application, and re-publish it.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    77

    Re: Opening a Program from a minimized state then closing active program

    Quote Originally Posted by Poppa Mintin View Post
    Hi K3JAE,

    First thing I'd do is: Program 'A' Form1.vb > Tab 'Project' > '(Program 'A') Properties' > Application

    Scroll down and put a tick in the box 'Make single instance application'.

    You will obviously need to run your application, and re-publish it.


    Poppa
    Not a possibility due to the stand-alone programs are not distributed throughout... thus why they are in multiple pieces... the request is for those with all pieces of the 'puzzle' so to speak.

  4. #4
    Frenzied Member Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    2,035

    Re: Opening a Program from a minimized state then closing active program

    I don't understand why that should stop you making your Program a single instance application.
    This just means that you can't start a new instance of an app. which is running, in other words... you can't have more than one Program A running on the same machine at the same time. (Which I understood you to say, is your problem)


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  5. #5
    Frenzied Member Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    2,035

    Re: Opening a Program from a minimized state then closing active program

    Quote Originally Posted by K3JAE View Post
    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.
    Isn't this your problem ?
    Have you solved it yet ?

    Maybe, instead of minimising Program 'A', close it altogether, then open it maximised (or normal) with Program 'B'.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  6. #6
    Frenzied Member Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    2,035

    Re: Opening a Program from a minimized state then closing active program

    Here's something I found, it uses a button, but you can see how it (supposedly) works. Obviously I've not actually tried it.

    It uses MS Word as an example, you'll have to substitute the name of your program, I suggest that you consider it to be case conscious. Oh... Do not include any extension... just the name of the program.
    Code:
        Private Declare Function ShowWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As Boolean
        
        Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim p = System.Diagnostics.Process.GetProcessesByName("WINWORD").FirstOrDefault
            If (Not (p) Is Nothing) Then
                ShowWindow(p.MainWindowHandle, SW_MAXIMIZE)
            End If
            
        End Sub

    Poppa
    Last edited by Poppa Mintin; Mar 4th, 2021 at 04:56 AM. Reason: Typo.
    Along with the sunshine there has to be a little rain sometime.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width