有没有办法检测用户当前在Windows操作系统中查看的程序?

问题描述 投票:0回答:1

这只是我的一个侧面项目,我尝试根据用户当前正在查看的程序更改鼠标的颜色。例如,如果是Microsoft边缘,颜色将变为蓝色;如果是Google Chrome,则颜色将变为黄色。有没有办法弄清楚用户当前正在查看哪个程序?

c# windows
1个回答
1
投票

你可以将GetForegroundWindowProcess.GetProcesses结合使用

GetForegroundWindow函数

检索前台窗口的句柄(用户当前正在使用的窗口)。系统为创建前台窗口的线程分配的优先级略高于其他线程的优先级。

Process.GetProcesses方法()

为本地计算机上的每个进程资源创建一个新的Process组件。

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetForegroundWindow();

... 

// get the active window
var activatedHandle = GetForegroundWindow();

var processes = Process.GetProcesses();

// compare with the processes  
foreach (var proc in processes)
{

    if(activatedHandle == proc.MainWindowHandle)
    {
        // you now have the process name
        string processName = proc.ProcessName;

        return processName;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.