CAUTION: Noob using Win32 API ahead
I want to display a list of open windows on my computer in a rich text box. So, I'm trying to store the names of all windows in myString, and then set RichTextBox1.Text = myString.
Here's the code
So, Display function above doesn't loop through the Collection of window titles. I don't know why. I just need to know why for educational purposes. I managed to display the list of window titles in the rich text box using another method...shown below.Code:Imports System Imports System.Collections.Generic Imports System.Collections.ObjectModel Imports System.Text Public Class Form1 Private Function Display(ByVal cs As Collection(Of String)) Dim builder As New StringBuilder For Each item As String In cs builder.Append(item & vbCrLf) Next item Return builder.ToString End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click RichTextBox1.Text = Display(GetActiveWindows) End Sub ' CallBack delegate Public Delegate Function CallBack(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean ' Imports Public Declare Function EnumWindows Lib "user32" (ByVal Adress As CallBack, ByVal y As Integer) As Integer Public Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As IntPtr) As Boolean Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpWindowText As String, ByVal cch As Integer) As Integer ' /Imports Private ActiveWindows As New Collection(Of String) Public Function GetActiveWindows() As Collection(Of String) ActiveWindows.Clear() EnumWindows(AddressOf Enumerator, 0) Return ActiveWindows End Function Private Function Enumerator(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean Dim text As String = Space(Int16.MaxValue) GetWindowText(hwnd, text, Int16.MaxValue) If IsWindowVisible(hwnd) Then ActiveWindows.Add(text) End If Return True End Function End Class
Thanks,
Code:Imports System Imports System.Collections.Generic Imports System.Collections.ObjectModel Imports System.Text Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click For Each item As String In GetActiveWindows() RichTextBox1.AppendText(item) RichTextBox1.AppendText(vbCrLf) Next 'RichTextBox1.Text End Sub ' CallBack delegate Public Delegate Function CallBack(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean ' Imports Public Declare Function EnumWindows Lib "user32" (ByVal Adress As CallBack, ByVal y As Integer) As Integer Public Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As IntPtr) As Boolean Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpWindowText As String, ByVal cch As Integer) As Integer ' /Imports Private ActiveWindows As New Collection(Of String) Public Function GetActiveWindows() As Collection(Of String) ActiveWindows.Clear() EnumWindows(AddressOf Enumerator, 0) Return ActiveWindows End Function Private Function Enumerator(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean Dim text As String = Space(Int16.MaxValue) GetWindowText(hwnd, text, Int16.MaxValue) If IsWindowVisible(hwnd) Then ActiveWindows.Add(text) End If Return True End Function End Class




Reply With Quote