How do you get a Windows window list?



  • WinForms is displayed in Windows as a window. In order to verify whether the monitors are also Windows windows, some information about the annex windows and all the daughter windows should be obtained.
    How can you do that on C#?
    That is, a C# method that, on the basis of the name of the window or the window process number, will return the information on the windows and the windows. This method will help to provide an accurate answer to the question. https://ru.stackoverflow.com/questions/480924 : Form/dialogue/menu/extended window/cortic search (Win+S) is the window or not?



  • You'll have to use the window list. WinApi function EnumWindows♪ A description of this function you can find https://msdn.microsoft.com/ru-ru/library/windows/desktop/ms633497(v=vs.85).aspx ♪

    I suppose you'd like to find a specific window, as an example of:

    [DllImport("user32.dll")]
    private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);
    

    // Delegate to filter which windows to include
    public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

    /// <summary> Get the text for the window pointed to by hWnd </summary>
    public static string GetWindowText(IntPtr hWnd)
    {
    int size = GetWindowTextLength(hWnd);
    if (size > 0)
    {
    var builder = new StringBuilder(size + 1);
    GetWindowText(hWnd, builder, builder.Capacity);
    return builder.ToString();
    }

    return String.Empty;
    

    }

    /// <summary> Find all windows that match the given filter </summary>
    /// <param name="filter"> A delegate that returns true for windows
    /// that should be returned and false for windows that should
    /// not be returned </param>
    public static IEnumerable<IntPtr> FindWindows(EnumWindowsProc filter)
    {
    IntPtr found = IntPtr.Zero;
    List<IntPtr> windows = new List<IntPtr>();

    EnumWindows(delegate(IntPtr wnd, IntPtr param)
    {
    if (filter(wnd, param))
    {
    // only add the windows that pass the filter
    windows.Add(wnd);
    }

      // but return true here so that we iterate all windows
      return true;
    

    }, IntPtr.Zero);

    return windows;
    }

    /// <summary> Find all windows that contain the given title text </summary>
    /// <param name="titleText"> The text that the window title must contain. </param>
    public static IEnumerable<IntPtr> FindWindowsWithText(string titleText)
    {
    return FindWindows((wnd, param) => GetWindowText(wnd).Contains(titleText));
    }

    Application:

    var windows = FindWindowsWithText("Notepad");

    WinAPI can be used to get a position. GetCursorPos

    Use:

    POINT p;
    if (GetCursorPos(&p))
    {
    //позиция курсора: p.x p.y
    }

    To determine the position of the cadet inside the window, this function can be used:

    if (ScreenToClient(hwnd, &p))
    {
    //p.x и p.y относительны вашего окна
    }

    https://msdn.microsoft.com/en-us/library/ms648390(VS.85).aspx




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2