[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
public string GetActiveWindowTitle()
{
const int nChars = 256;
IntPtr handle = IntPtr.Zero;
StringBuilder Buff = new StringBuilder(nChars);
handle = GetForegroundWindow();
if (GetWindowText(handle, Buff, nChars) > 0)
{
return Buff.ToString();
}
return "some_thing";
}
I got the active windo event when ever the taskbar changes it is called as ForegroundWindow event. The above code gives me the out in string format ,so I keep this method in a loop when ever the string value changes it means that another window is activated . I added some logic to that an got my work done.