/// <summary>
        /// 查找指定窗口
        /// </summary>
        /// <param name="appModule">应用信息</param>
        /// <param name="hWnd">窗口句柄</param>
        /// <returns>找到指定窗口返回true;否则false</returns>
        public static bool FindWindow(AppModuleInfo appModule, out IntPtr hWnd)
        {
            hWnd = IntPtr.Zero;
            var result = WindowApi.EnumWindows(new EnumWindowsProc(SearthWindowsCallback), ref appModule);

            if (!result)
            {
                if (appModule.WindowHandle != null)
                {
                    hWnd = (IntPtr)appModule.WindowHandle;
                    return(true);
                }
            }
            return(false);
 bool SearthWindowsCallback(IntPtr win, ref AppModuleInfo module)
 {
     if (WindowApi.GetParent(win) == IntPtr.Zero)
     {//无父窗口,顶级窗口
         var sb = new StringBuilder(256);
         if (!string.IsNullOrEmpty(module.ClassName))
         {//按类名查找
             if (WindowApi.GetClassName(win, sb, sb.Capacity) > 0)
             {
                 if (sb.ToString().StartsWith(module.ClassName))
                 {
                     module.WindowHandle = win;
                     return(false);
                 }
             }
         }
         if (!string.IsNullOrEmpty(module.WindowName))
         {//按窗口名查找
             if (WindowApi.GetWindowText(win, sb, sb.Capacity) > 0)
             {
                 if (sb.ToString().StartsWith(module.WindowName))
                 {
                     module.WindowHandle = win;
                     return(false);
                 }
             }
         }
         if (!string.IsNullOrEmpty(module.ProcessName))
         {//按进程名查找
             var ids = Process.GetProcessesByName(module.ProcessName).Select(p => p.Id).ToArray();
             _ = WindowApi.GetWindowThreadProcessId(win, out uint pid);
             if (ids.Contains((int)pid))
             {
                 module.WindowHandle = win;
                 return(false);
             }
         }
     }
     return(true);
 }
Beispiel #3
0
 public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, ref AppModuleInfo appModule);