以下是使用C#获取第三方正在运行的窗口的数据的示例代码:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
class Program
{
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, System.Text.StringBuilder text, int count);
static void Main(string[] args)
{
IntPtr handle = GetForegroundWindow();
const int nChars = 256;
System.Text.StringBuilder Buff = new System.Text.StringBuilder(nChars);
if (GetWindowText(handle, Buff, nChars) > 0)
{
Console.WriteLine("当前窗口标题: {0}", Buff.ToString());
}
else
{
Console.WriteLine("无法获取当前窗口标题");
}
}
}
该代码使用了`user32.dll`中
的`GetForegroundWindow`和`GetWindowText`函数来获取当前正在运行的窗口的句柄和标题。
其中,`GetForegroundWindow`函数用于获取当前活动窗口的句柄
,`GetWindowText`函数用于获取指定窗口的标题文本。
获取窗口【C#】
于 2023-11-11 23:55:47 首次发布
本文介绍了如何使用C#中的System.Diagnostics和DllImport特性,通过`GetForegroundWindow`和`GetWindowText`函数从user32.dll库获取第三方正在运行窗口的标题和句柄。
摘要由CSDN通过智能技术生成