栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

检测使用C#更改的活动窗口而不进行轮询

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

检测使用C#更改的活动窗口而不进行轮询

创建一个新的Windows窗体项目,添加一个文本框,使其多行,并将文本框的Dock属性设置为fill,将其命名为Log并粘贴以下代码(您需要将System.Runtime.InteropServices添加到您的用法中)。
..

    WinEventDelegate dele = null;    public Form1()    {        InitializeComponent();        dele = new WinEventDelegate(WinEventProc);        IntPtr m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT);    }    delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);    [Dllimport("user32.dll")]    static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);    private const uint WINEVENT_OUTOFConTEXT = 0;    private const uint EVENT_SYSTEM_FOREGROUND = 3;    [Dllimport("user32.dll")]    static extern IntPtr GetForegroundWindow();    [Dllimport("user32.dll")]    static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);    private string GetActiveWindowTitle()    {        const int nChars = 256;        IntPtr handle = IntPtr.Zero;        StringBuilder Buff = new StringBuilder(nChars);        handle = GetForegroundWindow();        if (GetWindowText(handle, Buff, nChars) > 0)        { return Buff.ToString();        }        return null;    }    public void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)    {        Log.Text += GetActiveWindowTitle() + "rn";    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/576113.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号