Domanda

Sto creando un'applicazione in cui interagisco con ogni applicazione in esecuzione. In questo momento, ho bisogno di un modo per ottenere l'ordine z della finestra. Ad esempio, se Firefox e il blocco note sono in esecuzione, devo sapere quale è di fronte.

Qualche idea? Oltre a farlo per la finestra principale di ogni applicazione, devo anche farlo per le finestre figlio e sorella (finestre appartenenti allo stesso processo).

È stato utile?

Soluzione

È possibile utilizzare la funzione GetTopWindow per cercare in tutte le finestre figlio di una finestra padre e restituire un handle alla finestra figlio più in alto nell'ordine z. La funzione GetNextWindow recupera un handle per la finestra successiva o precedente nell'ordine z.

Finestra GetTop: http://msdn.microsoft.com /en-us/library/ms633514(VS.85).aspx
Finestra GetNext: http://msdn.microsoft.com/en- us / library / ms633509 (VS.85) aspx

Altri suggerimenti

Bello e conciso:

int GetZOrder(IntPtr hWnd)
{
    var z = 0;
    for (var h = hWnd; h != IntPtr.Zero; h = GetWindow(h, GW.HWNDPREV)) z++;
    return z;
}

Se hai bisogno di maggiore affidabilità:

/// <summary>
/// Gets the z-order for one or more windows atomically with respect to each other. In Windows, smaller z-order is higher. If the window is not top level, the z order is returned as -1. 
/// </summary>
int[] GetZOrder(params IntPtr[] hWnds)
{
    var z = new int[hWnds.Length];
    for (var i = 0; i < hWnds.Length; i++) z[i] = -1;

    var index = 0;
    var numRemaining = hWnds.Length;
    EnumWindows((wnd, param) =>
    {
        var searchIndex = Array.IndexOf(hWnds, wnd);
        if (searchIndex != -1)
        {
            z[searchIndex] = index;
            numRemaining--;
            if (numRemaining == 0) return false;
        }
        index++;
        return true;
    }, IntPtr.Zero);

    return z;
}

(Secondo la sezione Note su GetWindow , EnumChildWindows è più sicuro che chiamare GetWindow in un ciclo perché il tuo ciclo GetWindow non è atomico rispetto alle modifiche esterne. alla sezione Parametri per EnumChildWindows , chiamare con un genitore null equivale a EnumWindows .)

Quindi invece di una chiamata separata a EnumWindows per ogni finestra, che sarebbe anche non atomica e sicura da modifiche simultanee, invii ogni finestra che vuoi confrontare in un array di parametri in modo che la loro z - tutti gli ordini possono essere recuperati contemporaneamente.

            // Find z-order for window.
            Process[] procs = Process.GetProcessesByName("notepad");
            Process top = null;
            int topz = int.MaxValue;
            foreach (Process p in procs)
            {
                IntPtr handle = p.MainWindowHandle;
                int z = 0;
                do
                {
                    z++;
                    handle = GetWindow(handle, 3);
                } while(handle != IntPtr.Zero);

                if (z < topz)
                {
                    top = p;
                    topz = z;
                }
            }

            if(top != null)
                Debug.WriteLine(top.MainWindowTitle);

Ecco la mia soluzione C #: La funzione restituisce zIndex tra i fratelli di un determinato HWND, a partire da 0 per lo zOrder più basso.

using System;
using System.Runtime.InteropServices;

namespace Win32
{
    public static class HwndHelper
    {
        [DllImport("user32.dll")]
        private static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);

        public static bool GetWindowZOrder(IntPtr hwnd, out int zOrder)
        {
            const uint GW_HWNDPREV = 3;
            const uint GW_HWNDLAST = 1;

            var lowestHwnd = GetWindow(hwnd, GW_HWNDLAST);

            var z = 0;
            var hwndTmp = lowestHwnd;
            while (hwndTmp != IntPtr.Zero)
            {
                if (hwnd == hwndTmp)
                {
                    zOrder = z;
                    return true;
                }

                hwndTmp = GetWindow(hwndTmp, GW_HWNDPREV);
                z++;
            }

            zOrder = int.MinValue;
            return false;
        }
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top