locked
How to minimize Our Program When Other program minimize?? RRS feed

  • Question

  • Hi Friends, Let say I have Program A and B... and I want my A program to be minimize when minimize program B.... Is it can be done??... I need help Thanks In advance
    "Have a Nice Day" __________________ Regards, BFY_VB Beginner
    Thursday, March 24, 2011 5:11 AM

Answers

  • You can minimize any window related to any program (in this case, program A) with P/Invoke's ShowWindow API and SW_MINIMIZE value in SHOW_WINDOW enum. So, in your Program B, you must catch "SizeChanged" event to determine when B is going to be minimized, then if B is going to be minimized, also minimize A like this:

    ' This is program B, when B is minimized, it will minimize A
    Public Class Form1
        Private Declare Function ShowWindow Lib "user32.dll" ( _
            ByVal hWnd As IntPtr, _
            ByVal nCmdShow As SHOW_WINDOW _
            ) As Boolean

        <Flags()> _
        Private Enum SHOW_WINDOW As Integer
            SW_HIDE = 0
            SW_SHOWNORMAL = 1
            SW_NORMAL = 1
            SW_SHOWMINIMIZED = 2
            SW_SHOWMAXIMIZED = 3
            SW_MAXIMIZE = 3
            SW_SHOWNOACTIVATE = 4
            SW_SHOW = 5
            SW_MINIMIZE = 6
            SW_SHOWMINNOACTIVE = 7
            SW_SHOWNA = 8
            SW_RESTORE = 9
            SW_SHOWDEFAULT = 10
            SW_FORCEMINIMIZE = 11
            SW_MAX = 11
        End Enum

        Private Sub minimize_programA()
            For Each p As Process In _
            Process.GetProcessesByName("programA")
                ShowWindow(p.MainWindowHandle, SHOW_WINDOW.SW_MINIMIZE)
            Next p
        End Sub


        Private Sub Form1_sizechanged _
        (ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.SizeChanged

            If Me.WindowState = FormWindowState.Minimized Then
                minimize_programA()
            End If

        End Sub
    End Class

    ...where "programA" as argument GetProcessesByName method is the process name visible in your task manager without. exe extension. It should work fine.

    HTH.


    Best regards, Saygılarımla, Onur Güzel

    Yazgeliştir Forumları VB.NET / C# Moderatorü.

    Microsoft Haber Grupları Profilim (VB.NET)


    • Marked as answer by Han_BFY Friday, March 25, 2011 3:43 AM
    Thursday, March 24, 2011 10:36 AM
  • Well, in this case, you must periodically track window status of Program B from your program A, by a timer or by user-demand, then if Program B's window state is minimized, minimize your program A:

    ' Eg: Minimize your program (program A) if program B is minimzed:

    ' Place a timer on your form, set Enabled = True, set Interval to 1000 to check every 1 sec.

    ' This is code of Program A, your program
    Public Class Form1
        Private Declare Auto Function IsIconic Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean

        Private Sub Timer1_Tick _
        (ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim hWnd As IntPtr = Process.GetProcessesByName("programB")(0).MainWindowHandle

            If IsIconic(hWnd) Then
                Me.WindowState = FormWindowState.Minimized

                ' window is minimized here
                ' you can stop timer if you wish
                Timer1.Stop()
            Else
                ' program B hasn't been minimized yet

            End If
        End Sub
    End Class

    ..where IsIconic Win32 function determines if the window is iconic (minimized).

    http://msdn.microsoft.com/en-us/library/ms633527(v=vs.85).aspx

    HTH.


    Best regards, Saygılarımla, Onur Güzel

    Yazgeliştir Forumları VB.NET / C# Moderatorü.

    Microsoft Haber Grupları Profilim (VB.NET)

    • Marked as answer by Han_BFY Friday, March 25, 2011 1:54 AM
    Thursday, March 24, 2011 12:45 PM
  • Done

    Private Sub TimerSet1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerSet1.Tick

    Dim hWnd As IntPtr = Process.GetProcessesByName("dra")(0).MainWindowHandle

    If IsIconic(hWnd) Then

    Me.WindowState = FormWindowState.Minimized

    Else

    Me.WindowState = FormWindowState.Normal

    End If

    End Sub

    • Edited by Han_BFY Friday, March 25, 2011 3:42 AM change
    • Marked as answer by Han_BFY Friday, March 25, 2011 4:22 AM
    Friday, March 25, 2011 3:36 AM

All replies

  • You can minimize any window related to any program (in this case, program A) with P/Invoke's ShowWindow API and SW_MINIMIZE value in SHOW_WINDOW enum. So, in your Program B, you must catch "SizeChanged" event to determine when B is going to be minimized, then if B is going to be minimized, also minimize A like this:

    ' This is program B, when B is minimized, it will minimize A
    Public Class Form1
        Private Declare Function ShowWindow Lib "user32.dll" ( _
            ByVal hWnd As IntPtr, _
            ByVal nCmdShow As SHOW_WINDOW _
            ) As Boolean

        <Flags()> _
        Private Enum SHOW_WINDOW As Integer
            SW_HIDE = 0
            SW_SHOWNORMAL = 1
            SW_NORMAL = 1
            SW_SHOWMINIMIZED = 2
            SW_SHOWMAXIMIZED = 3
            SW_MAXIMIZE = 3
            SW_SHOWNOACTIVATE = 4
            SW_SHOW = 5
            SW_MINIMIZE = 6
            SW_SHOWMINNOACTIVE = 7
            SW_SHOWNA = 8
            SW_RESTORE = 9
            SW_SHOWDEFAULT = 10
            SW_FORCEMINIMIZE = 11
            SW_MAX = 11
        End Enum

        Private Sub minimize_programA()
            For Each p As Process In _
            Process.GetProcessesByName("programA")
                ShowWindow(p.MainWindowHandle, SHOW_WINDOW.SW_MINIMIZE)
            Next p
        End Sub


        Private Sub Form1_sizechanged _
        (ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.SizeChanged

            If Me.WindowState = FormWindowState.Minimized Then
                minimize_programA()
            End If

        End Sub
    End Class

    ...where "programA" as argument GetProcessesByName method is the process name visible in your task manager without. exe extension. It should work fine.

    HTH.


    Best regards, Saygılarımla, Onur Güzel

    Yazgeliştir Forumları VB.NET / C# Moderatorü.

    Microsoft Haber Grupları Profilim (VB.NET)


    • Marked as answer by Han_BFY Friday, March 25, 2011 3:43 AM
    Thursday, March 24, 2011 10:36 AM
  • HI Onur,

    In my case program B is Commercial program which can't be update.

    Program A is develope by me...

    Does this can be done??

    Sorry for lack info.

    Thanks in advance


    "Have a Nice Day" __________________ Regards, BFY_VB Beginner
    Thursday, March 24, 2011 12:22 PM
  • Well, in this case, you must periodically track window status of Program B from your program A, by a timer or by user-demand, then if Program B's window state is minimized, minimize your program A:

    ' Eg: Minimize your program (program A) if program B is minimzed:

    ' Place a timer on your form, set Enabled = True, set Interval to 1000 to check every 1 sec.

    ' This is code of Program A, your program
    Public Class Form1
        Private Declare Auto Function IsIconic Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean

        Private Sub Timer1_Tick _
        (ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim hWnd As IntPtr = Process.GetProcessesByName("programB")(0).MainWindowHandle

            If IsIconic(hWnd) Then
                Me.WindowState = FormWindowState.Minimized

                ' window is minimized here
                ' you can stop timer if you wish
                Timer1.Stop()
            Else
                ' program B hasn't been minimized yet

            End If
        End Sub
    End Class

    ..where IsIconic Win32 function determines if the window is iconic (minimized).

    http://msdn.microsoft.com/en-us/library/ms633527(v=vs.85).aspx

    HTH.


    Best regards, Saygılarımla, Onur Güzel

    Yazgeliştir Forumları VB.NET / C# Moderatorü.

    Microsoft Haber Grupları Profilim (VB.NET)

    • Marked as answer by Han_BFY Friday, March 25, 2011 1:54 AM
    Thursday, March 24, 2011 12:45 PM
  • Onur, How If I want A back to Normal when Normal the B Commercial program?? Can please help me... Thanks In Advance.
    Have a Nice Day Regards BFY-Malaysia
    • Edited by Han_BFY Friday, March 25, 2011 3:03 AM Change
    Friday, March 25, 2011 1:57 AM
  • Done

    Private Sub TimerSet1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerSet1.Tick

    Dim hWnd As IntPtr = Process.GetProcessesByName("dra")(0).MainWindowHandle

    If IsIconic(hWnd) Then

    Me.WindowState = FormWindowState.Minimized

    Else

    Me.WindowState = FormWindowState.Normal

    End If

    End Sub

    • Edited by Han_BFY Friday, March 25, 2011 3:42 AM change
    • Marked as answer by Han_BFY Friday, March 25, 2011 4:22 AM
    Friday, March 25, 2011 3:36 AM