[ DllImport("user32.dll") ] static extern int GetForegroundWindow(); [ DllImport("user32.dll") ] static extern int GetWindowText(int hWnd, StringBuilder text, int count);   Then, write the code for the function private void GetActiveWindow()
{const int nChars = 256;
int handle = 0;
StringBuilder Buff = new StringBuilder(nChars);
   
   handle = GetForegroundWindow();   if ( GetWindowText(handle, Buff, nChars) > 0 )
   {
   this.captionWindowLabel.Text = Buff.ToString();
   this.IDWindowLabel.Text = handle.ToString();
   }}or getactivewindow API