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();
            } 
  • 相关阅读:
    python3从尾到头打印链表
    替换空格
    二维数组中的查找
    堪比福尔摩斯的破案新科技!快来了解一下
    好莱坞首部全亚裔主演电影上映,华人终于谈恋爱了
    《呼啸山庄》:爱之深,恨之切,死相依
    《碟中谍 6:全面瓦解》口碑爆棚,登顶北美周末票房
    让重刑犯做瑜伽:新西兰人性化监狱的故事
    python面试笔试题汇总
    centos7 重置root 密码
  • 原文地址:https://www.cnblogs.com/szyicol/p/2514733.html
Copyright © 2011-2022 走看看