Well I'm trying to make a program that have ListBox1 which when the button1 is pressed the Listbox1 will show all the open windows of notepad, like get notepad.exe and it will show Example the name: Untitled - Notepad nothing else, i have a code right now that i found but it Shows a few things, look at this picture.

http://img37.imageshack.us/img37/1241/getn.png

It says MSCTFIME UI And Default IMe, i don't want that to show...

This is the code i have right now.


Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Lst = Me.ListBox1
        EnumWindows(AddressOf EnumWindowsCallBack, 0)
    End Sub

Module 1

Private Declare Auto Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hWnd As IntPtr, ByRef ProcessID As Integer) As Integer

    Public Lst As ListBox

    Public Delegate Function EnumWindowsProc(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Int32
    Public Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As Int32) As Int32
    Public Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As IntPtr) As Int32
    Public Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Int32) As Int32

    'Callback function to enum windows
    Public Function EnumWindowsCallBack(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Int32
        Dim sSave As String
        Dim ProcessID As Long

        GetWindowThreadProcessId(hwnd, ProcessID)

        Dim tempProc As Process = Process.GetProcessById(ProcessID)
        Dim processName As String = tempProc.ProcessName

        If (processName = "notepad") Then

            'Get the windowtext length
            sSave = Space(GetWindowTextLength(hwnd) + 1)

            'get the window text
            GetWindowText(hwnd, sSave, Len(sSave))

            'remove the last Chr(0)
            sSave = Microsoft.VisualBasic.Left(sSave, Len(sSave) - 1)

            'Error below: Reference to a non-shared member requires an Object Reference
            Lst.Items.Add(sSave)
            'Lst.Items.Add(processName)


            If sSave.Trim <> "" Then
                Debug.WriteLine(sSave)
            End If
        End If
        Return 1 'continue enumeration 
    End Function
Dim p() As Process = Process.GetProcessesByName("notepad")
For Each proc As Process In p
	Console.WriteLine(proc.MainWindowTitle)
Next

Will print all notepad window names.
The EnumWindows is mostly used for getting the child windows of a process (which is not the case for notepad anyway)

Edited 6 Years Ago by GeekByChoiCe: n/a

It worked perfectly, now i have a question, when i select it on ListBox1 how can i do that when the title of the notepad is selected and the button2 is pressed it close the process of that notepad but not all notepads

like you have 5 Notepad open, you select 1 in listbox1, you click button2 and it closes notepad's process for the selected one, i tried but when i do it it kills all the processes...

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		Dim p() As Process = Process.GetProcessesByName("notepad")
		Dim listBoxItems As New Dictionary(Of String, Integer)
		For Each proc As Process In p
			ListBox1.Items.Add(New KeyValuePair(Of String, Integer)(proc.MainWindowTitle, proc.Id))
		Next
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		Dim listboxItem As KeyValuePair(Of String, Integer) = CType(ListBox1.SelectedItem, KeyValuePair(Of String, Integer))
		Process.GetProcessById(listboxItem.Value).CloseMainWindow()
		ListBox1.Items.Remove(listboxItem)
End Sub

select your listbox in your project,and change its properties DisplayMember to "Key" and ValueMember to "Value" (ofc without the quotes).

Edited 6 Years Ago by GeekByChoiCe: n/a

how would you then close the window that you have a windows handle for?

This article has been dead for over six months. Start a new discussion instead.