这只是我的一个侧面项目,我尝试根据用户当前正在查看的程序更改鼠标的颜色。例如,如果是Microsoft边缘,颜色将变为蓝色;如果是Google Chrome,则颜色将变为黄色。有没有办法弄清楚用户当前正在查看哪个程序?
你可以将GetForegroundWindow
与Process.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;
}
}