1 Replies - 208 Views - Last Post: 24 December 2017 - 05:57 AM Rate Topic: -----

#1 JoeBobJr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 23-December 17

Code that will search all cmd windows and minimize them

Posted 23 December 2017 - 11:04 PM

I'm looking for a code to search all cmd windows and will minimize them if any are found. I'm using

Dim iHwnd As IntPtr = FindWindow(vbNullString, "C:\Windows\system32\cmd.exe")
      ShowWindow(iHwnd, SW_SHOWMINIMIZED)

but its only minimizing one window I'm not too sure how to put that code in a loop any help would be greatly appreciated thanks!
Is This A Good Question/Topic? 0
  • +

Replies To: Code that will search all cmd windows and minimize them

#2 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1505
  • View blog
  • Posts: 3,809
  • Joined: 01-February 13

Re: Code that will search all cmd windows and minimize them

Posted 24 December 2017 - 05:57 AM

You would not want to use the FindWindow api function for finding several windows, you would want to use the FindWindowEx api function. The FindWindow function would only keep finding the first window over and over and would only minimize the first window even if you put it in a loop. The FindWindowEx function is made for a situation like what you want.

If you look at the msdn documents at the link i gave above, you will see it has 2 more parameters. The first is t specify the parent window you want to search in. In your case it would be the Desktop window which can be specified by using (IntPtr.Zero) which is an null handle. The second parameter is to specify where to start the search from, in the parent window's controls.

For finding the first window in the desktop that has the title you specified, the second parameter would also be set to (IntPtr.Zero).

Use the handle that is returned from the first call to FindWindowEx to control a While Loop. If the handle it returns is not (IntPtr.Zero), then that means it has found a window that meets your specifications. Make the loop keep iterating While iHwnd is not IntPtr.Zero.

Inside the loop, the first line can Show the window minimized. The second line in the loop will be another call to FindWindowEx. This time, the second parameter will be iHwnd, the window handle that was just found. That tells FindWindowEx to search for the next window after that one that meets your specifications.

Sounds complicated, but it really is pretty simple when you look at it. Below is a short commented example you can experiment with. It requires 2 Buttons added to the form.
Imports System.Runtime.InteropServices

Public Class Form1
    <DllImport("user32.dll", EntryPoint:="FindWindowExW")>
    Public Shared Function FindWindowExW(ByVal hWndParent As IntPtr, ByVal hWndChildAfter As IntPtr, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpszClass As String, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpszWindow As String) As IntPtr
    End Function

    Public Shared Function ShowWindow(ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

    Public Const SW_SHOWMINIMIZED As Integer = 2

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        'find the first window on the desktop that has the caption title "C:\Windows\system32\cmd.exe"
        Dim iHwnd As IntPtr = FindWindowExW(IntPtr.Zero, IntPtr.Zero, Nothing, "C:\Windows\system32\cmd.exe")

        'if a window was found, iHwnd will be other than IntPtr.Zero and the loop will be entered
        While iHwnd <> IntPtr.Zero

            ShowWindow(iHwnd, SW_SHOWMINIMIZED) 'minimize the window

            'find the next window after the last one that has the caption title "C:\Windows\system32\cmd.exe".
            'If the last one was found, iHwnd will be IntPtr.Zero which will cause the loop to stop.
            iHwnd = FindWindowExW(IntPtr.Zero, iHwnd, Nothing, "C:\Windows\system32\cmd.exe")

        End While

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'this is just a loop to open a few cmd windows for testing
        For i As Integer = 0 To 3
    End Sub
End Class

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1