SetForegroundWindows optional captionName

本文关键字:captionName optional SetForegroundWindows | 更新日期: 2023-09-27 18:13:52

我有这个代码,我想匹配任何CaptionName,我真的不知道该怎么做,以及我如何能把正则表达式或其他的东西,所以它可以检查任何CaptionName,我想使用的BringToFront方法。看一下:

class Program
{
    [DllIport("User32.dll")]
    public static extern Int32 SetForegroundWindow(int hWnd);
    [DllImport("user32.dll")]
    public static extern int FindWindow(string lpClassName, string lpWindowName);
    private static void BringToFront(string className, string CaptionName)
    {
        SetForegroundWindow(FindWindow(className, CaptionName));
    }
    static void Main(string[] args)
    {    
        BringToFront("Notepad","#*#");
    }
}

SetForegroundWindows optional captionName

试试下面的代码。你可以通过过滤所有进程从process . getprocesses()中获得的进程句柄,然后获得句柄。

调用SetForegroundWindow将它们显示到前台

class Program
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool SetForegroundWindow(IntPtr hWnd);
        private static void BringToFront(string CaptionName)
        {
            foreach (Process p in Process.GetProcesses()
                                         .ToList()
                                         .FindAll(/*Write your rule here*/p => p.MainWindowTitle.Contains(CaptionName)))
            {
                SetForegroundWindow(p.MainWindowHandle);
            }
        }
        static void Main(string[] args)
        {
            BringToFront("Notepad");
        }
    }