c# - How do i return a false or true each process so i can monitor if the process is in front or not? -
in form1 have 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 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 monitor processes , if move process front show me in label4 in form1.
applicationisactivated not written good. first i'm not sure if making foreach each time , second i'm not sure how return activeprocid == procid; per process.
instead of checking if each process activated one, can find activated process directly:
process findactivatedprocess() { var activatedhandle = getforegroundwindow(); if (activatedhandle == intptr.zero) return null; // no window has focus int activeprocid; getwindowthreadprocessid(activatedhandle, out activeprocid); return process.getprocessbyid(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);
to determine when activated application changes, can polling (though suggest larger interval -- 50ms seems overkill me), or can ask windows notify when happens.
Comments
Post a Comment