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.

有帮助吗?

解决方案

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.

其他提示

If you know the process name of the window searched you could try with something like this:

Process[] processes = Process.GetProcessesByName("notepad");
foreach (Process p in processes)
{
    IntPtr pFoundWindow = p.MainWindowHandle;
    SetForegroundWindow(pFoundWindow);
}

MSDN on GetProcessesByName

I don't think there is a built-in function/method/API for searching for windows with a regular expression pattern. One way of accomplishing it would be to enumerate the windows such as with this example and then compare the window text in the callback function using the regular expression.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top