/ Published in: C++
Determine if 'this' app has focus.
Using MFC.
Using MFC.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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 );