zoukankan      html  css  js  c++  java
  • c# 列举所有窗口和子窗口

    private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam);
    
            [DllImport("user32.dll", ExactSpelling = true)]
            private static extern bool EnumChildWindows(IntPtr hwndParent, WNDENUMPROC lpEnumFunc, int lParam); 
    
            [DllImport("user32.dll")]
            private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);
            //[DllImport("user32.dll")] 
            //private static extern IntPtr FindWindowW(string lpClassName, string lpWindowName); 
            [DllImport("user32.dll")]
            private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
            [DllImport("user32.dll")]
            private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
            public struct WindowInfo
            {
                public IntPtr hWnd;
                public string szWindowName;
                public string szClassName;
            }
    
    
    
    
            private List<WindowInfo> EnumChildWindowsCallback(IntPtr handle, string name, string classname)
            {
                List<WindowInfo> wndList = new List<WindowInfo>();
                EnumChildWindows(handle,delegate(IntPtr hWnd, int lParam)
                {
                    WindowInfo wnd = new WindowInfo();
                    StringBuilder sb = new StringBuilder(256);
                    //get hwnd 
                    wnd.hWnd = hWnd;
                    //get window name 
                    GetWindowTextW(hWnd, sb, sb.Capacity);
                    wnd.szWindowName = sb.ToString();
                    //get window class 
                    GetClassNameW(hWnd, sb, sb.Capacity);
                    wnd.szClassName = sb.ToString();
                    //add it into list 
                    wndList.Add(wnd);
                    return true;
                },0);
                return wndList.Where(it => it.szWindowName == name && it.szClassName == classname).ToList();
            } 
    
            public List<WindowInfo> GetAllDesktopWindows(string name,string classname)
            {
                List<WindowInfo> wndList = new List<WindowInfo>();
    
                //enum all desktop windows 
                EnumWindows(delegate(IntPtr hWnd, int lParam)
                {
                    WindowInfo wnd = new WindowInfo();
                    StringBuilder sb = new StringBuilder(256);
                    //get hwnd 
                    wnd.hWnd = hWnd;
                    //get window name 
                    GetWindowTextW(hWnd, sb, sb.Capacity);
                    wnd.szWindowName = sb.ToString();
                    //get window class 
                    GetClassNameW(hWnd, sb, sb.Capacity);
                    wnd.szClassName = sb.ToString();
                    //add it into list 
                    wndList.Add(wnd);
                    return true;
                }, 0);
    
                return wndList.Where(it => it.szWindowName == name && it.szClassName ==classname ).ToList();
            }
  • 相关阅读:
    mybatis中mysql转义讲解
    mybatis结合mysql批量操作及查询sql
    转载:避免重复插入,更新的sql
    maven下载jar包下载不下来的解决方法
    catalina.home与 catalina.base区别
    ON DUPLICATE KEY UPDATE单个增加更新及批量增加更新的sql
    普通索引的建立及普通索引的排序
    复合索引的优点和注意事项
    com.mysql.jdbc.PacketTooBigException,及mysql 设置 max_allow_packet
    ./和../和/三种路径的区别
  • 原文地址:https://www.cnblogs.com/mutuan/p/3867943.html
Copyright © 2011-2022 走看看