【发布时间】:2016-04-24 08:21:17
【问题描述】:
我试图让计时器每 250 毫秒检查一次特定程序是否处于焦点,但我就是不知道如何...
当前代码:
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Hearthstone_Test
{
public partial class Main : Form
{
private void timer1_Tick(object sender, EventArgs e)
{
var activatedHandle = GetForegroundWindow();
if (GetForegroundWindow() == Process.GetProcessesByName("Hearthstone"));
{
Console.WriteLine("Not Focused"); // No window is currently activated
}
else
{
Console.WriteLine("Focused");
}
var procId = Process.GetCurrentProcess().Id;
int activeProcId;
GetWindowThreadProcessId(activatedHandle, out activeProcId);
}
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);
}
}
错误在第 11 行:
Operator '==' cannot be applied to operands of type 'Process[]' and 'IntPtr'
我做错了什么?
【问题讨论】:
-
该错误实际上是在告诉您该行有什么问题。还有其他许多问题,如果您像您所说的那样搜索了几个小时,您就会知道如何检查前台进程。 SO 不是代码编写服务。发布stackoverflow.com/help/mcve
-
@Grant 对不起,我已经修好了。
-
@Clivens 很抱歉,但对你来说这个解释可能有道理,但我今天第一次开始学习 C#,英语不是我的主要语言,所以理解这些困难的概念真的很难如果我在网上找不到一个可行的例子。我不知道你为什么会评判或怀疑我,如果我寻求帮助,显然我无法在互联网上找到我想要的东西?