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;
}

猜你喜欢

转载自www.cnblogs.com/xuejianxiyang/p/9238165.html