获取窗口【C#】

本文介绍了如何使用C#中的System.Diagnostics和DllImport特性,通过`GetForegroundWindow`和`GetWindowText`函数从user32.dll库获取第三方正在运行窗口的标题和句柄。
摘要由CSDN通过智能技术生成
以下是使用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`函数用于获取指定窗口的标题文本。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值