首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何使用c#获取当前活动窗口的标题?

在 C# 中,您可以使用 System.Windows.Forms.Cursor 类中的 Hide() 方法隐藏光标或鼠标指针,以及使用 WindowFromPoint 方法获取鼠标当前位置的窗口。但需要注意的是,Hide() 方法是隐藏光标,而不是光标所在的窗口。

为了获取活动窗口的标题,您可以使用 WindowFromPoint 方法,将其结合使用 System.Windows.Forms.Cursor 类中的 Show() 方法将隐藏的光标重新显示出来。

代码语言:txt
复制
using System.Windows.Forms;

public class clsWindowInfo
{
    public string windowTitle { get; set; }
    public clsWindowInfo(IntPtr hwnd)
    {
        Win32.GetWindowText(hwnd, out windowTitle);
    }
}

public static void Main()
{
    Win32.POINT lpPoint = new Win32.POINT();
    Win32.GetCursorPos(out lpPoint);
    IntPtr hWnd = Win32.WindowFromPoint(lpPoint);
    Win32.GetClassName(hWnd, out Win32.clsWindowInfo info);
    windowTitle = info.windowTitle;
}

public static class Win32
{
    static Win32()
    {
        WNDCLASSEX wcex = new WNDCLASSEX();
        // ... 添加 WNDCLASSEX 成员变量。
    }

    [DllImport("user32.dll")]
    public static extern IntPtr GetWindowText(IntPtr hWnd, StringBuilder lpBuffer, int nMaxCount);

    [DllImport("user32.dll")]
    public static extern bool GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
    }
    public struct POINT
    {
        public int X;
        public int Y;
    }
}

上面的代码将显示当前活动窗口的标题,同时该标题将始终保持在活动窗口上,直到窗口活动状态变化。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

4分31秒

016_如何在vim里直接运行python程序

589
6分9秒

Elastic 5分钟教程:使用EQL获取威胁情报并搜索攻击行为

2分13秒

MySQL系列十之【监控管理】

3分7秒

MySQL系列九之【文件管理】

1分6秒

小米手机忘记WiFi密码不用愁,导出查看所有曾经连接过的WIFI密码

4分32秒

PS小白教程:如何在Photoshop中使用蒙版工具插入图片?

3分59秒

基于深度强化学习的机器人在多行人环境中的避障实验

1时5分

云拨测多方位主动式业务监控实战

2分29秒

基于实时模型强化学习的无人机自主导航

16分8秒

人工智能新途-用路由器集群模仿神经元集群

领券