Search code examples
c#regexwinformssetfocusfindwindow

How to use Regular Expression with FindWindow?


I am ultimately trying to write something that will check if a specific window exists, and set it as active in the event it is. I was able to use FindWindow to find a literal windows name.

int hWnd = FindWindow(null, "121226-000377 - company -  Oracle RightNow CX Cloud Service");
            if (hWnd > 0) //If found
            {
                SetForegroundWindow(hWnd); //Activate it

            }
            else
            {
                MessageBox.Show("Window Not Found!");
            }

The number at the front of the title changes and will never be the same twice, therefor I was trying to use Regular Expressions to find if any active window has the name structure as shown above but the numbers can change. I have a regular expresion that works for this, but I don't know how to implement it. I tried:

int hWnd = FindWindow(null, @"^\d+-\d+\s.*?RightNow CX");
            if (hWnd > 0) //If found
            {
                SetForegroundWindow(hWnd); //Activate it

            }
            else
            {
                MessageBox.Show("Window Not Found!");
            }

But it continually fails. So how do I use the FindWindow/SetForegroundWindow commands while making them use the regular expression to check with?

UPDATE~~~~ I selected a best answer, but here is the actual code for how I got this to work in case anyone is interested.

   protected static bool EnumTheWindows(IntPtr hWnd, IntPtr lParam)
        {
            int size = GetWindowTextLength(hWnd);
            if (size++ > 0 && IsWindowVisible(hWnd))
            {
                StringBuilder sb = new StringBuilder(size);
                GetWindowText(hWnd, sb, size);

                Match match = Regex.Match(sb.ToString(), @"^\d+-\d+\s.*?RightNow CX",
               RegexOptions.IgnoreCase);


                // Here we check the Match instance.
                if (match.Success)
                {
                   ActivateRNT(sb.ToString());

                }
                else
                {
                    //this gets triggered for every single failure
                }
                //do nothing


            }
            return true;
        }

private static void ActivateRNT(string rnt)
        {
            //Find the window, using the CORRECT Window Title, for example, Notepad

            int hWnd = FindWindow(null, rnt);
            if (hWnd > 0) //If found
            {
                SetForegroundWindow(hWnd); //Activate it

            }
            else
            {
                MessageBox.Show("Window Not Found!");
            }

        }

I still need to figure out how to test in the EnumWindows method to throw an alert if the window needed doesn't exist, but I'll worry about that at a later time.


Solution

  • I guess EnumWindows() is what you're looking for, although I'm not 100% sure how you'd use it in C# as you'll need a callback.

    Edit: pinvoke.net got some code including an example callback. Edit 2: The linked [MSDN sample][3] has more details on why/how to do it that way.