得到没有标题的窗口句柄。(C#) [英] Get handle of a window that has no title.. (C#)

查看:333
本文介绍了得到没有标题的窗口句柄。(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们怎样才能得到一个没有标题的窗口的句柄?有没有一种方法来枚举桌面上的所有窗口和过滤没有标题的窗口(在我的情况下,只有一个),并获得它的手柄..或通过指定那样有一个窗口的其他属性一个特定的按钮或列表框等等...

How can we get the handle of a window that doesn't have a title? Is there a way to enumerate all the windows on desktop and filter the window that don't have a title (in my case, there is only one) and getting the handle of it.. or by specifying other attributes like a window that has a specific button or listbox etc...

推荐答案

这应该这样做:

    ...
    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);
        }

    }
}

这篇关于得到没有标题的窗口句柄。(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆