[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int GetWindowTextW(int hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
 
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int GetClassNameW(int hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
 
public delegate bool WNDENUMPROC(int hWnd, int lParam);
 
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);
 
/// <summary>
        /// 枚举结构
        /// </summary>
        public struct WindowInfo
        {
            public int PW_Hwnd;
            public string PW_WindowName;
            public string PW_ClassName;
        }
 
        /// <summary>
        /// 枚举当前系统窗口
        /// </summary>
        /// <returns></returns>
        public static WindowInfo[] FindWindows()
        {
            try
            {
                List<WindowInfo> WindowsList = new List<WindowInfo>();
                EnumWindows(delegate (int APP_Hwnd, int lParam)
                {
                    WindowInfo _W = new WindowInfo();
                    StringBuilder sb = new StringBuilder(256);
                    _W.PW_Hwnd = APP_Hwnd;
                    GetWindowTextW(APP_Hwnd, sb, sb.Capacity);
                    _W.PW_WindowName = sb.ToString();
                    GetClassNameW(APP_Hwnd, sb, sb.Capacity);
                    _W.PW_ClassName = sb.ToString();
                    WindowsList.Add(_W);
                    return true;
                }, 0);
                return WindowsList.ToArray();
            }
            catch (Exception ex)
            {
   //处理异常
            }
             
        }
免责声明: PHPPHP所发布的一切破解补丁、注册机和注册信息及软件的解密分析文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理,邮箱:xxbbyy@88.com 如下载链接打不开了请发送文章链接到邮箱:xxbbyy@88.com,我们将尽快处理。
PHP教程资源网 » [.Net] C#获取窗口句柄和进程ID的类

发表评论

提供最优质的资源集合

立即查看 了解详情