Return to Snippet

Revision: 46235
at May 14, 2011 07:03 by jimfred


Updated Code
static HWND hWnd2 = (HWND)-1;
   HWND hWnd = ::GetForegroundWindow();

   if ( hWnd2==hWnd )
   {
      return;
   }

   hWnd2 = hWnd;

   int slength = ::GetWindowTextLength(hWnd) + 1; // length of its title bar text
   CString wintext;
   ::GetWindowText(hWnd, wintext.GetBufferSetLength(slength), slength); // get title bar text
   wintext.ReleaseBuffer();

   DWORD dwProcessIdFocus;
   // Use Windows API GetWindowThreadProcessId to find the window's process ID.
   GetWindowThreadProcessId( hWnd, &dwProcessIdFocus );

   // Use Windows API GetCurrentProcessId to find 'this' process ID.
   DWORD dwProcessIdParent = GetCurrentProcessId();

   bool thisAppHasFocus = dwProcessIdFocus==dwProcessIdParent;

   CString msg;
   msg.Format( 
      "%s%s
"
      "HWND: 0x%X
"
      "Process ID: %d",
      wintext.GetLength() ? wintext : "(null)",
      thisAppHasFocus ? " (me)" : "",
      hWnd,
      dwProcessIdFocus
   );

Revision: 46234
at May 14, 2011 07:00 by jimfred


Initial Code
static HWND hWnd2 = (HWND)-1;
   HWND hWnd = ::GetForegroundWindow();

   if ( hWnd2==hWnd )
   {
      return;
   }

   hWnd2 = hWnd;

   int slength = ::GetWindowTextLength(hWnd) + 1; // length of its title bar text
   CString wintext;
   ::GetWindowText(hWnd, wintext.GetBufferSetLength(slength), slength); // get title bar text
   wintext.ReleaseBuffer();

   DWORD dwProcessIdFocus;
   // Use Windows API GetWindowThreadProcessId to find the window's process ID.
   GetWindowThreadProcessId( hWnd, &dwProcessIdFocus );

   // Use Windows API GetCurrentProcessId to find 'this' process ID.
   DWORD dwProcessIdParent = GetCurrentProcessId();

   bool thisAppHasFocus = dwProcessIdFocus==dwProcessIdParent;

Initial URL

                                

Initial Description
Determine if 'this' app has focus.
Using MFC.

Initial Title
MFC, Determine if \'this\' app has focus

Initial Tags

                                

Initial Language
C++