Does .Net provide any routines to list open windows?

Hey,

I was just wondering if .NET provided any routines that can enumerate open windows.  I know it can be done using some windows API calls, but I wasn't sure if .NET had any such features.  Thanks in advance for any help.



Answer this question

Does .Net provide any routines to list open windows?

  • Bruce VB

    It does as from .NET 2.0. Use Application.OpenForms.

  • Surya Suluh

    Thanks jelle, I'm learning while answering Smile

    I tried your approach and can't make it show all the windows in my System; My bad, I haven't checked the documentation, it says, it's only to enumerate Windows owned by Application, not all windows:
    http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.openforms.aspx

    Ok, I still used API calls to enumerate windows (I attached it to button1_Click event):



    private delegate bool EnumWindowsCallBack(IntPtr hwnd, IntPtr lparam);

    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    private static extern bool EnumWindows(EnumWindowsCallBack callback, IntPtr lparam);

    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    private static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int cch);

    private void button1_Click(object sender, System.EventArgs e)
    {
     EnumWindowsCallBack enumwinproc = new EnumWindowsCallBack(EnumarateWindows);
     EnumWindows(enumwinproc, IntPtr.Zero);
     GC.KeepAlive(enumwinproc);
    }
    private bool EnumarateWindows(IntPtr hwnd, IntPtr lparam)
    {
         StringBuilder sbWindowText = new StringBuilder(1024);
         GetWindowText(hwnd, sbWindowText, 1024);
         MessageBox.Show(hwnd.ToInt32().ToString("X") + ": Text = " + sbWindowText.ToString());
         return true;
    }

     


    Once you've clicked the button, it will display all top-level windows Smile

    Hope this helps,

    -chris



  • Embedded Developer

    As far as I know, there's none in .NET that you can use to list windows, you still have to use P/Invoke API calls.

    Anyway, EnumWindows and/or FindWindow API are fairly easy to implement in .NET.

    Regards,

    -chris

  • Does .Net provide any routines to list open windows?