阅读量:2
要通过C#中的FindWindow函数获取窗口标题,您需要首先确保已经引用了System.Runtime.InteropServices命名空间
using System;
using System.Runtime.InteropServices;
class Program
{
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
static void Main()
{
// 替换以下字符串为要查找的窗口类名和窗口标题
string className = "Notepad";
string windowTitle = "无标题 - 记事本";
IntPtr hwnd = FindWindow(className, windowTitle);
if (hwnd != IntPtr.Zero)
{
StringBuilder text = new StringBuilder(256);
GetWindowText(hwnd, text, text.Capacity);
Console.WriteLine($"窗口标题: {text.ToString()}");
}
else
{
Console.WriteLine("未找到窗口");
}
}
}
在这个示例中,我们首先使用FindWindow函数根据类名(lpClassName)和窗口标题(lpWindowName)查找窗口。如果找到了窗口,我们使用GetWindowText函数获取窗口的文本,并将其输出到控制台。
以上就是关于“如何通过C# FindWindow获取窗口标题”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm