To know if your window has the focus, just use GetActiveWindow() from Windows API. if it returns a hwnd that's not the one in your program, then you don't have the focus.
Works great to "auto-pause" a game when the window looses focus ;)
Works great to "auto-pause" a game when the window looses focus ;)