在网上找几遍,都写得好长,写得好复杂~~
求指教:
如果我现在在系统中打开了三个标题为“测试窗口”的窗口,那么如何精确得到他们的句柄?尽量要短,这样看起来可以很快吸收,我在网上找了一天,就会了个模糊擦找的,达不到要求~~写成一个类吧,太谢谢你了~~

解决方案 »

  1.   

    补充一下,我这里只是距离三个窗口,大哥千万不要for 3来写,呵呵,窗口数量不一定的
      

  2.   

    不知这样是否符合你的要求
    // 调用
    List<IntPtr> list = GetHandles("测试窗口");
    // 方法
    private List<IntPtr> GetHandles(string title)
    {
        List<IntPtr> handles = new List<IntPtr>();
        foreach (Form item in Application.OpenForms)
        {
            if (item.Text == title)
            {
                handles.Add(item.Handle);
            }
        }
        return handles;
    }
      

  3.   

    调用enumchildwindows api做递归
      

  4.   


    [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern IntPtr FindWindowEx(
    IntPtr hwndParent,
    IntPtr hwndChildAfter,
    string lpszClass,
    string lpszWindow
    );List<IntPtr> result = new List<IntPtr>();
    IntPtr hWnd = IntPtr.Zero;
    do
    {
    hWnd = FindWindowEx(IntPtr.Zero, hWnd, null, "测试窗口");
    if (hWnd != IntPtr.Zero)
    {
    result.Add(hWnd);
    }
    }
    while (hWnd != IntPtr.Zero);
    return result;
      

  5.   

    4L 正解 用api findwindow
    或者 用Process 列出所有进程 然后查看每个进程的标题..