using System;
using System.Runtime.InteropServices;
class Program
{
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
static void Main()
{
// 指定窗口类名和窗口标题
string className = null; // 如果不知道类名,可以传入null
string windowTitle = "无标题 - 记事本"; // 这里以记事本为例
// 调用FindWindow函数查找窗口句柄
IntPtr hWnd = FindWindow(className, windowTitle);
if (hWnd == IntPtr.Zero)
{
Console.WriteLine("未找到指定窗口。");
}
else
{
Console.WriteLine("找到了窗口,句柄为: " + hWnd);
}
}
}
DllImport 属性导入 Windows API 函数 FindWindow,该函数位于 user32.dll 中。IntPtr.Zero。lpClassName:窗口类名。如果不确定类名,可以传入 null。lpWindowName:窗口标题。需要确保提供的标题与目标窗口的标题完全匹配。FindWindow 查找具有特定标题的窗口(例如记事本)。希望这段代码和解释对你有帮助!
上一篇:c#去掉最后一个字符
下一篇:c# 对象转json
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站