在C#中,您可以使用FindWindow函数来查找特定的窗口。这个函数是Windows API的一部分,它允许您根据窗口类名和窗口标题来查找窗口。以下是一个简单的示例,展示了如何使用FindWindow来查找一个具有特定类名和窗口标题的窗口:
using System;
using System.Runtime.InteropServices;
class Program
{
    [DllImport("user32.dll")]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    static void Main()
    {
        // 窗口类名(类名是区分大小写的)
        string className = "Notepad";
        // 窗口标题(标题是区分大小写的)
        string windowTitle = "无标题 - 记事本";
        // 使用FindWindow函数查找窗口
        IntPtr hwnd = FindWindow(className, windowTitle);
        if (hwnd != IntPtr.Zero)
        {
            Console.WriteLine($"找到了窗口!句柄:{hwnd}");
        }
        else
        {
            Console.WriteLine("未找到窗口。");
        }
    }
}
在这个示例中,我们首先导入了System.Runtime.InteropServices命名空间,以便我们可以使用DllImport属性来导入Windows API函数。然后,我们定义了一个名为FindWindow的静态方法,它接受两个字符串参数:lpClassName和lpWindowName,分别表示窗口类名和窗口标题。
在Main方法中,我们设置了要查找的窗口的类名和窗口标题,然后调用FindWindow函数来查找该窗口。如果找到了窗口,FindWindow将返回一个非零的句柄值;否则,它将返回IntPtr.Zero。最后,我们根据FindWindow的返回值输出相应的结果。

 便宜VPS测评
便宜VPS测评












