Pergunta

Como podemos obter o identificador de uma janela que não tem um título? Existe uma maneira de enumerar todas as janelas na área de trabalho e filtrar a janela que não tem um título (no meu caso, existe apenas uma) e obtendo o identificador ... ou especificando outros atributos como uma janela que possui um botão específico ou caixa de listagem etc ...

Foi útil?

Solução

Isso deve servir:

    ...
    using System.Runtime.InteropServices;
    using System.Diagnostics;

    ...

public class foo()
{
    ...

    [DllImport ("user32")]
    internal static extern int GetWindowText (int hWnd, String text, int nMaxCount);

    [DllImport ("user32.dll")]
    public static extern int GetWindowTextLength (int hWnd);

    [DllImport ("user32.dll")]
    public static extern int FindWindow (String text, String class_name);

    [DllImport ("user32.dll")]
    public static extern int FindWindowEx (int parent, int start, String class_name);

    [DllImport ("user32.dll")]
    public static extern int GetWindow (int parent, uint cmd);

    public List<int> FindTitlelessWindows()
    {
        List<int> titleless = new List<int> ();

        Process [] procs = Process.GetProcesses ();
        IntPtr hWnd;

        foreach (Process proc in procs)
        {
            hWnd = proc.MainWindowHandle;
            if (hWnd != IntPtr.Zero)
            {
                TraverseHierarchy (hWnd.ToInt32 (), 0, titleless);

            }
        }

        foreach (int i in titleless)
        {
            System.Console.WriteLine (i);
        }

        return titleless;
    }

    public void TraverseHierarchy (int parent, int child, List<int> titleless)
    {
        String text = "";
        GetWindowText (parent, text, GetWindowTextLength (parent));
        if (String.IsNullOrEmpty (text))
        {
            titleless.Add (parent);
        }

        TraverseChildern (parent, titleless);
        TraversePeers (parent, child, titleless);

    }

    public void TraverseChildern(int handle, List<int> titleless)
    {
        // First traverse child windows
        const uint GW_CHILD = 0x05;
        int child = GetWindow (handle, GW_CHILD);
        if (0 != child)
        {
            TraverseHierarchy (child, 0, titleless);

        }
    }

    public void TraversePeers(int parent, int start, List<int> titleless)
    {
        // Next traverse peers
        int peer = FindWindowEx(parent, start, "");
        if (0 != peer)
        {
            TraverseHierarchy (parent, peer, titleless);
        }

    }
}

Outras dicas

Dê uma olhada na função EnumchildWindows.

Eu acho que se você passar no ponteiro da janela principal (ou seja, desktop) para esta função, poderá obter uma lista de todas as janelas e janelas filhos.

Em combinação com o FindWindow, deve ser possível colocar a alça para a janela que você deseja depois de localizar um controle infantil esperado.

Algo assim deve funcionar, não testado

[DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount);

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetWindowTextLength(IntPtr hWnd); 

....
Process[] procs = Process.GetProcesses();
IntPtr hWnd;
foreach(Process proc in procs)
{
    hWnd = proc.MainWindowHandle;
    if (hWnd != IntPtr.Zero)
    {
        int length = GetWindowTextLength(hWnd);
        StringBuilder sb = new StringBuilder(length + 1);
        GetWindowText(hWnd, sb, length);
        if (String.IsNullOrEmpty(sb.ToString())
        {
            // we have a window with no title!
        }
    }
}

http://msdn.microsoft.com/en-us/library/ms633558(vs.85).aspx

O WindowFroMoint retorna uma alça ou nulo se nenhuma janela estiver embaixo do seu cursor. Isso poderia ser usado ou você está tentando automatizar o processo?

Aqui Você pode encontrar uma biblioteca para lidar com as coisas da API do Windows no código gerenciado.
Faça o download da DLL e faça referência a seu projeto e, a partir daí, é muito fácil obter qualquer informação em qualquer janela que desejar;

 void ForAllSystemWindows()
        {
            foreach (SystemWindow window in SystemWindow.AllToplevelWindows)
            {
                if (window.Title == string.Empty)
                {
                    //Found window without title

                    //Get window handle
                    IntPtr windowhandle  = window.HWnd;

                    //Do other stuff ..
                }
            }
        }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top