none
Open Minimized (external) Application RRS feed

All replies

  • For example =>

    Dim MyProcess As Process = System.Diagnostics.Process.Start("notepad")
    MyProcess.WaitForInputIdle()
    Dim hWndTarget As IntPtr = MyProcess.MainWindowHandle
    ShowWindow(hWndTarget, SW_SHOWMINIMIZED)
    

    Declarations :

    Public Const SW_HIDE As Integer = 0
    Public Const SW_SHOWNORMAL As Integer = 1
    Public Const SW_SHOWMINIMIZED As Integer = 2
    Public Const SW_SHOWMAXIMIZED As Integer = 3
    Public Const SW_SHOWNOACTIVATE As Integer = 4
    Public Const SW_SHOW As Integer = 5
    
    <DllImport("User32.dll", EntryPoint:="ShowWindow", SetLastError:=True)>
    Public Shared Function ShowWindow(ByVal hWnd As IntPtr, nShowCmd As Integer) As Boolean
    End Function

    Thursday, December 14, 2017 7:29 AM
  • I just get these errors. I assume I have placed it correctly? :/

    (link to image on OneDrive)

    https://1drv.ms/i/s!AmsbJL1fRBaekN8ALAVjgs2LnJ55Rg

    Thursday, December 14, 2017 1:19 PM
  • I just get these errors. I assume I have placed it correctly? :/

    (link to image on OneDrive)

    https://1drv.ms/i/s!AmsbJL1fRBaekN8ALAVjgs2LnJ55Rg

    You certainly forgot :

    Imports System.Runtime.InteropServices
    at beginning...

    Thursday, December 14, 2017 1:23 PM
  • I still have errors. :/

    It basically created a new error.

    • Edited by rossyboi Thursday, December 14, 2017 5:19 PM
    Thursday, December 14, 2017 5:19 PM
  • Could you show an example of which the above would be applied in the application?
    Thursday, December 14, 2017 5:20 PM
  • Imports System.Runtime.InteropServices
    Public Class Form1
        Public Const SW_HIDE As Integer = 0
        Public Const SW_SHOWNORMAL As Integer = 1
        Public Const SW_SHOWMINIMIZED As Integer = 2
        Public Const SW_SHOWMAXIMIZED As Integer = 3
        Public Const SW_SHOWNOACTIVATE As Integer = 4
        Public Const SW_SHOW As Integer = 5
    
        <DllImport("User32.dll", EntryPoint:="ShowWindow", SetLastError:=True)>
        Public Shared Function ShowWindow(ByVal hWnd As IntPtr, nShowCmd As Integer) As Boolean
        End Function
    
        WithEvents Button1 As New Button
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With Button1
                .Size = New System.Drawing.Size(75, 23)
                .Location = New System.Drawing.Point(104, 108)
                .Name = "Button1"
                .Text = "Button 1"
            End With
            Me.Controls.Add(Button1)
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim MyProcess As Process = System.Diagnostics.Process.Start("notepad")
            MyProcess.WaitForInputIdle()
            Dim hWndTarget As IntPtr = MyProcess.MainWindowHandle
            ShowWindow(hWndTarget, SW_SHOWMINIMIZED)
        End Sub
    End Class

    Thursday, December 14, 2017 5:56 PM
  • That is fixed but I now have a new issue... Sorry.

    "System.InvalidOperationException: 'Process has exited, so the requested information is not available.'"

    • Edited by rossyboi Thursday, December 14, 2017 6:12 PM
    Thursday, December 14, 2017 6:10 PM
  • That is fixed but I now have a new issue... Sorry.

    "System.InvalidOperationException: 'Process has exited, so the requested information is not available.'"

    At which line ? WaitForInputIdle ?

    With Notepad ?

    if it is at WaitForInputIdle, you can replace it by

    System.Threading.Thread.Sleep(100)

    Thursday, December 14, 2017 8:51 PM
  • That is fixed but I now have a new issue... Sorry.

    "System.InvalidOperationException: 'Process has exited, so the requested information is not available.'"

    At which line ? WaitForInputIdle ?

    With Notepad ?

    if it is at WaitForInputIdle, you can replace it by

    System.Threading.Thread.Sleep(100)

    (Onedrive Link to Image)

    https://1drv.ms/i/s!AmsbJL1fRBaekN8BI4FXxSXXQAA5Rw

    Thursday, December 14, 2017 9:19 PM
  • I cannot reproduce it on my OS (Windows 10)

    But if I wait 2 seconds until Firefox has loaded the current page, it works too :

    Dim MyProcess As Process = System.Diagnostics.Process.Start("C:\Program Files\Mozilla Firefox\firefox.exe")
    System.Threading.Thread.Sleep(2000)
    Dim hWndTarget As IntPtr = MyProcess.MainWindowHandle
    ShowWindow(hWndTarget, SW_SHOWMINIMIZED)

    or with FindWindow() :

    Dim MyProcess As Process = System.Diagnostics.Process.Start("C:\Program Files\Mozilla Firefox\firefox.exe")
    System.Threading.Thread.Sleep(2000)
    Dim hWndTarget As IntPtr = FindWindow("MozillaWindowClass", "Mozilla Firefox")
    ShowWindow(hWndTarget, SW_SHOWMINIMIZED)
    <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
        Public Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
        End Function



    • Edited by Castorix31 Thursday, December 14, 2017 10:30 PM
    Thursday, December 14, 2017 10:21 PM
  • I tried FindWindow() or <g class="gr_ gr_33 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" data-gr-id="33" id="33">what ever</g> you meant :/

    You are really going to hate me now... Got a new error! :(

    (Image on Onedrive)

    https://1drv.ms/i/s!AmsbJL1fRBaekN8CWelXIsylqvx2VA

    I removed the conflict, everything worked fine but when I minimized the application and clicked the button again, it opened a new window, not the minimized window.

    Thanks btw. Really appreciate your help.



    • Edited by rossyboi Thursday, December 14, 2017 11:23 PM
    Thursday, December 14, 2017 11:02 PM
  • Hi rossyboi,

    Welcome to the MSDN forum.

    Refer to your description, your issue is about VB development. Since our forum is to discuss the VS IDE, I will help you move this thread to the appropriate forum to seek for a better support, thank you for your understanding.

    Best regards,

    Sara


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Friday, December 15, 2017 5:47 AM
  • Hi rossyboi,

    According to your description, you want to minmized external application from your vb.net application? If yes, please take a look the following code:

    Private Const SW_SHOWMAXIMIZED As Integer = 3
        Private Declare Function ShowWindow Lib "user32" (ByVal handle As IntPtr, ByVal nCmdShow As Integer) As Integer
        Private Declare Function SetForegroundWindow Lib "user32" (ByVal handle As IntPtr) As Integer
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            MaximizeExeByName("notepad")
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
        Private Sub MaximizeExeByName(ByVal ExeNameWithoutPathOrExtension As String) '
            Dim ps() As Process = Process.GetProcessesByName(ExeNameWithoutPathOrExtension)
            If ps.Length > 0 Then
                ShowWindow(ps(0).MainWindowHandle, SW_SHOWMAXIMIZED)
                SetForegroundWindow(ps(0).MainWindowHandle)
            End If
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 15, 2017 7:50 AM
    Moderator
  •  If the program is already opened and it is minimized,  you don't need to create a Process, you can just get the process.  Then  you can use the SendMessage win32 api function to send a WM_SYSCOMMAND message to it.  The wParam can be set to SC_RESTORE or SC_MAXIMIZE to make it restore or maximize.  You can also use the SC_MINIMIZE or SC_CLOSE commands to minimize or close a window too.

     However, if the window is already restored or maximized and you just want to bring it to the front of all other windows,  you can use the old AppActivate method to do that.  Here is an example...

    Imports System.Runtime.InteropServices

    Public Class Form1
        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")>
        Public Shared Function SendMessageW(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As UInteger, ByVal lParam As Integer) As Integer
        End Function

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim p As Process = Process.GetProcessesByName("firefox").FirstOrDefault
            If p IsNot Nothing Then
                SendMessageW(p.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0) 'restore the window from it's minimized state
                AppActivate(p.Id) 'activate it to bring it to the front of all other windows
            End If
        End Sub
    End Class
     

     The ShowWindow api function will work too.  Just figured i would show an alternative to it.  8)

     *** EDIT ***

     This would be a better way to do it.  Use the IsIconic api function to check if the window is minimized before restoring it.  This is because, if the window is Maximized but is in the background behind other window and you restore it,  it will change from the maximized state back to it's restored state.

     When it is minimized,  you only need to restore it and it will activate itself.  If it is already restored or maximized,  you only need to activate it to bring it to the front (in it's current state).

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Private Const WM_SYSCOMMAND As Integer = &H112
        Private Const SC_RESTORE As Integer = &HF120
    
        <DllImport("user32.dll", EntryPoint:="SendMessageW")>
        Private Shared Function SendMessageW(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As UInteger, ByVal lParam As Integer) As Integer
        End Function
    
        <DllImport("user32.dll")> Private Shared Function IsIconic(ByVal hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim p As Process = Process.GetProcessesByName("firefox").FirstOrDefault
            If p IsNot Nothing Then
                If IsIconic(p.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(p.MainWindowHandle, WM_SYSCOMMAND, SC_RESTORE, 0) 'restore the window from it's minimized state
                Else
                    AppActivate(p.Id) 'activate it to bring it to the front of all other windows
                End If
            End If
        End Sub
    End Class
    
     

     There are several ways you can accomplish this so,  this is just one of several ways.  8)


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Friday, December 15, 2017 3:20 PM
    Friday, December 15, 2017 1:10 PM
  • Hi Rossyboi,

    This forum is to help each other. Your question made me curious. Where would you use this as for me every user simple goes to the toolbar and clicks on that? The effect is than the same?


    Success
    Cor

    Friday, December 15, 2017 1:17 PM
  • Hi Rossyboi,

    Would something like this solve your problem perhaps?

    Imports System.Runtime.InteropServices
    Public Class Form1
        Public Const SW_HIDE As Integer = 0
        Public Const SW_SHOWNORMAL As Integer = 1
        Public Const SW_SHOWMINIMIZED As Integer = 2
        Public Const SW_SHOWMAXIMIZED As Integer = 3
        Public Const SW_SHOWNOACTIVATE As Integer = 4
        Public Const SW_SHOW As Integer = 5
    
        <DllImport("user32.dll")> Private Shared Function IsWindow(ByVal hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function
    
        <DllImport("User32.dll", EntryPoint:="ShowWindow", SetLastError:=True)>
        Public Shared Function ShowWindow(ByVal hWnd As IntPtr, nShowCmd As Integer) As Boolean
        End Function
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Process.Start("notepad")
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Dim MyProcess As Process = Process.GetProcessesByName("notepad").LastOrDefault
            Dim hWndTarget As IntPtr = MyProcess.MainWindowHandle
            If MyProcess IsNot Nothing AndAlso IsWindow(MyProcess.MainWindowHandle) Then
                Threading.Thread.Sleep(1000)
                ShowWindow(hWndTarget, SW_SHOWMINIMIZED)
            End If
        End Sub
    End Class

    Probably not, but maybe...

    Friday, December 15, 2017 3:16 PM