In form1 I have a timer:
private void Form1_Load(object sender, EventArgs e)
{
System.Windows.Forms.Timer t1 = new System.Windows.Forms.Timer();
t1.Interval = 50;
t1.Tick += new EventHandler(timer1_Tick);
t1.Enabled = true;
}
The tick event:
private void timer1_Tick(object sender, EventArgs e)
{
label4.Text = SetProcessWindow.ApplicationIsActivated().ToString();
}
And the ApplicationIsActivated method:
public static bool ApplicationIsActivated()
{
Process[] Processes = Process.GetProcesses();
foreach (Process SingleProcess in Processes)
{
var activatedHandle = GetForegroundWindow();
if (activatedHandle == IntPtr.Zero)
{
return false;
}
var procId = SingleProcess.Id;//Process.GetCurrentProcess().Id;
int activeProcId;
GetWindowThreadProcessId(activatedHandle, out activeProcId);
return activeProcId == procId;
}
}
I want somehow to monitor the processes and if I move any process to the front then show me it in label4 in form1.
ApplicationIsActivated is not written good. First i'm not sure if making foreach each time is good and second i'm not sure how to return the activeProcId == procId; per process.