none
..如何枚举所有窗口句柄。 RRS feed

答案

  • 在窗体上放置一个button和listBox1控件

    using System.Runtime.InteropServices;
    
    
     [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int GetWindowText(HandleRef hWnd, StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int GetWindowTextLength(HandleRef hWnd);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);
        [DllImport("user32.dll", ExactSpelling = true)]
        private static extern bool EnumChildWindows(HandleRef hwndParent, EnumChildrenCallback lpEnumFunc, HandleRef lParam);
        private delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);
        private delegate bool EnumChildrenCallback(IntPtr hwnd, IntPtr lParam);
    
        private void button1_Click(object sender, EventArgs e)
        {
          EnumThreadWindowsCallback callback1 = new EnumThreadWindowsCallback(this.EnumWindowsCallback);
          EnumWindows(callback1, IntPtr.Zero);
        }
        private bool EnumWindowsCallback(IntPtr handle, IntPtr extraParameter)
        {
          int num1 = GetWindowTextLength(new HandleRef(this, handle)) * 2;
          StringBuilder builder1 = new StringBuilder(num1);
          GetWindowText(new HandleRef(this, handle), builder1, builder1.Capacity);
          System.Console.WriteLine(string.Format("Wnd:{0} Title: {1}", handle, builder1.ToString()));
          Application.DoEvents();
          listBox1.Items.Add(string.Format("Wnd:{0} Title: {1}", handle, builder1.ToString()));
          EnumChildWindows(new HandleRef(this, handle), new EnumChildrenCallback(EnumChildWindowsCallback), new HandleRef(null, IntPtr.Zero));
          return true;
        }
        private bool EnumChildWindowsCallback(IntPtr handle, IntPtr lparam)
        {
          int num1 = GetWindowTextLength(new HandleRef(this, handle)) * 2;
          StringBuilder builder1 = new StringBuilder(num1);
          GetWindowText(new HandleRef(this, handle), builder1, builder1.Capacity);
          System.Console.WriteLine(string.Format("\tSubWnd:{0} Title: {1}", handle, builder1.ToString()));
          return true;
        }
    

    http://blog.csdn.net/zx13525079024
    • 已建议为答案 caozhyMVP 2010年10月19日 13:38
    • 已标记为答案 limljh 2010年10月19日 15:40
    2010年10月19日 11:06

全部回复

  • 利用 EnumWindows 函数,请参考相应的 Windows API 文档。
    Mark Zhou
    2010年10月19日 10:12
  • 在窗体上放置一个button和listBox1控件

    using System.Runtime.InteropServices;
    
    
     [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int GetWindowText(HandleRef hWnd, StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int GetWindowTextLength(HandleRef hWnd);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);
        [DllImport("user32.dll", ExactSpelling = true)]
        private static extern bool EnumChildWindows(HandleRef hwndParent, EnumChildrenCallback lpEnumFunc, HandleRef lParam);
        private delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);
        private delegate bool EnumChildrenCallback(IntPtr hwnd, IntPtr lParam);
    
        private void button1_Click(object sender, EventArgs e)
        {
          EnumThreadWindowsCallback callback1 = new EnumThreadWindowsCallback(this.EnumWindowsCallback);
          EnumWindows(callback1, IntPtr.Zero);
        }
        private bool EnumWindowsCallback(IntPtr handle, IntPtr extraParameter)
        {
          int num1 = GetWindowTextLength(new HandleRef(this, handle)) * 2;
          StringBuilder builder1 = new StringBuilder(num1);
          GetWindowText(new HandleRef(this, handle), builder1, builder1.Capacity);
          System.Console.WriteLine(string.Format("Wnd:{0} Title: {1}", handle, builder1.ToString()));
          Application.DoEvents();
          listBox1.Items.Add(string.Format("Wnd:{0} Title: {1}", handle, builder1.ToString()));
          EnumChildWindows(new HandleRef(this, handle), new EnumChildrenCallback(EnumChildWindowsCallback), new HandleRef(null, IntPtr.Zero));
          return true;
        }
        private bool EnumChildWindowsCallback(IntPtr handle, IntPtr lparam)
        {
          int num1 = GetWindowTextLength(new HandleRef(this, handle)) * 2;
          StringBuilder builder1 = new StringBuilder(num1);
          GetWindowText(new HandleRef(this, handle), builder1, builder1.Capacity);
          System.Console.WriteLine(string.Format("\tSubWnd:{0} Title: {1}", handle, builder1.ToString()));
          return true;
        }
    

    http://blog.csdn.net/zx13525079024
    • 已建议为答案 caozhyMVP 2010年10月19日 13:38
    • 已标记为答案 limljh 2010年10月19日 15:40
    2010年10月19日 11:06
  • 谢谢您的回答,不过我直接把你的代码运行了下,点按钮没有反应。。。

    2010年10月19日 15:20
  • ...那个我直接拖按钮上去了,但是单击事件自己创建了个后就行了。谢谢

    2010年10月19日 15:40