C#获取窗口句柄并修改窗口标题

时间:2022-11-19 15:59:45


using System.Runtime.InteropServices;  //添加引用,才可以使用[DllImport("User32.dll")]
namespace 窗口句柄操作
{
internal class Program
{
[DllImport("User32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
//static extern固定搭配,表示添加外部方法的引用
[DllImport("User32.dll")]
public static extern void SetWindowText(IntPtr hwnd, string lpString);
//IntPtr窗口句柄类型
static void Main(string[] args)
{
IntPtr Whandle = FindWindow("HwndWrapper[DefaultDomain;;a37acf9b-7f58-4583-aeb1-f6494814b557]", null);
SetWindowText(Whandle, "DeapanSQL");
Console.ReadKey();
}
}
}

//本例子使用SQL Server 2014 Management Studio进行测试
//lpClassName和lpWindowName通过Spy++获取