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

有帮助吗?

解决方案

此应该这样做:

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

    }
}

其他提示

看一看的EnumChildWindows功能。

我认为,如果你在主窗口(即桌面)这个函数的指针传递,你将能够得到所有窗口及其子窗口的列表。

在结合FindWindow函数应该可以,一旦你找到一个预期的子控件得到你想要的句柄的窗口。

像这样的东西应该有效, 未测试

[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 /库/ ms633558(VS.85)的.aspx

如果没有窗口光标下方

WindowFromPoint返回句柄,或空。难道这是使用的,或者是你想的过程自动化?

这里你可以找到一个库来应对托管代码窗口API的东西。结果 下载DLL和你的项目中引用它,并从那里它很容易 得到你想要的任何窗口上的任何信息;

 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 ..
                }
            }
        }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top