How can I tell if a window has focus? (Win32 API)

GetActiveWindow will return the top-level window that is associated with the input focus. GetFocus will return the handle of the window that has the input focus.

This article might help:
http://www.microsoft.com/msj/0397/Win32/Win320397.aspx


Besides gkrogers answer using GetActiveWindow, you can also maintain a boolean variable for the window you want to know if it has focus or not by trapping the WM_SETFOCUS and WM_KILLFOCUS events, or WM_ACTIVATE:

WndProc() ..
case WM_SETFOCUS:
  puts( "Got the focus" ) ;
  break ;

case WM_KILLFOCUS:
  puts( "Lost the focus" ) ;
  break;

case WM_ACTIVATE:
  if( LOWORD(wparam) == WA_INACTIVE )
    puts( "I AM NOW INACTIVE." ) ;
  else // WA_ACTIVE or WA_CLICKACTIVE
    puts( "MEGAZORD ACTIVATED kew kew kew (flashy-eyes)" ) ;
  break ;