code:if(ChatWnd.Handle == GetForegroundWindow()) { // Window is on top. } function GetForegroundWindow() { return Interop.Call("user32.dll", "GetForegroundWindow"); }