winform判断chrome是否正在最前端运行
/// <summary>
/// 获取系统当前活动窗口
/// </summary>
/// <returns></returns>
[DllImport("User32.DLL")]
static extern IntPtr GetForegroundWindow();
/// <summary>
/// 获取指定窗体的标题
/// </summary>
/// <param name="WinHandle">窗体句柄</param>
/// <param name="Title">缓冲区取用于存储标题</param>
/// <param name="size">缓冲区大小</param>
/// <returns></returns>
[DllImport("User32.dll")]
static extern int GetWindowText(IntPtr WinHandle, StringBuilder Title, int size);
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
bool isRunning = true;
bool IsChromeIsRunningInForeground()
{
bool isChromeRunningInForeground = false;
var foregroundHandle = GetForegroundWindow();
uint pid = 0;
GetWindowThreadProcessId(foregroundHandle, out pid); //根据句柄查找进程id
var list=Process.GetProcessesByName("chrome");
//var hands=string.Join(",", list.Select(ee => ee.Handle.ToString()).ToList());
//this.richTextBox2.AppendText(hands+"\r\n");
foreach (var p in list)
{
if (p.Id==pid)
{
isChromeRunningInForeground= true;
return isChromeRunningInForeground;
}
}
return isChromeRunningInForeground;
}
From:https://www.cnblogs.com/xuejianxiyang/p/9238165.html
作者:xuejianxiyang
出处:http://xuejianxiyang.cnblogs.com
关于作者:Heaven helps those who help themselves.
本文版权归原作者和博客园共有,欢迎转载,但未经原作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。