Example #1
0
            /// <summary>
            /// Checks if a window is present
            /// </summary>
            /// <param name="regularExpression">The regular expression that is used to find the window</param>
            /// <param name="windowHandle">returns the handle of the window</param>
            /// <returns>True if window is present</returns>
            public static bool GetWindow(string regularExpression, out IntPtr windowHandle)
            {
                windowHandle = IntPtr.Zero;

                List <WindowStruct> windowsCollection = new List <WindowStruct>();

                EnumDelegate enumDelegate = delegate(IntPtr hWnd, int lParam)
                {
                    StringBuilder strBuilderTitle = new StringBuilder(255);
                    int           nLength         = GetWindowText(hWnd, strBuilderTitle, strBuilderTitle.Capacity + 1);
                    string        strTitle        = strBuilderTitle.ToString();

                    if (IsWindowVisible(hWnd) == true && string.IsNullOrEmpty(strTitle) == false)
                    {
                        WindowStruct windowStruct = new WindowStruct();
                        windowStruct.Title = strTitle;
                        windowStruct.Ptr   = hWnd;
                        windowsCollection.Add(windowStruct);
                    }
                    return(true);
                };

                if (EnumDesktopWindows(IntPtr.Zero, enumDelegate, IntPtr.Zero))
                {
                    foreach (WindowStruct window in windowsCollection)
                    {
                        if (Regex.IsMatch(window.Title, regularExpression))
                        {
                            windowHandle = window.Ptr;
                            return(true);
                        }
                    }
                }

                return(false);
            }
Example #2
0
            /// <summary>
            /// Checks if a window is present
            /// </summary>
            /// <param name="regularExpression">The regular expression that is used to find the window</param>
            /// <param name="windowHandle">returns the handle of the window</param>
            /// <returns>True if window is present</returns>
            public static bool GetWindow(string regularExpression, out IntPtr windowHandle)
            {
                windowHandle = IntPtr.Zero;

                List<WindowStruct> windowsCollection = new List<WindowStruct>();

                EnumDelegate enumDelegate = delegate(IntPtr hWnd, int lParam)
                {
                    StringBuilder strBuilderTitle = new StringBuilder(255);
                    int nLength = GetWindowText(hWnd, strBuilderTitle, strBuilderTitle.Capacity + 1);
                    string strTitle = strBuilderTitle.ToString();

                    if (IsWindowVisible(hWnd) == true && string.IsNullOrEmpty(strTitle) == false)
                    {
                        WindowStruct windowStruct = new WindowStruct();
                        windowStruct.Title = strTitle;
                        windowStruct.Ptr = hWnd;
                        windowsCollection.Add(windowStruct);
                    }
                    return true;
                };

                if (EnumDesktopWindows(IntPtr.Zero, enumDelegate, IntPtr.Zero))
                {
                    foreach (WindowStruct window in windowsCollection)
                    {
                        if (Regex.IsMatch(window.Title, regularExpression))
                        {
                            windowHandle = window.Ptr;
                            return true;
                        }
                    }
                }

                return false;
            }