private static void getActiveWindow(bool flag = false)
{
AutomationElement focusedElement = null;
try
{
IntPtr foregroundWindow = Windows.GetForegroundWindow();
var title = Windows.GetWindowTitle(foregroundWindow);
Log.Debug("fgHwnd = " + ((foregroundWindow != IntPtr.Zero) ? foregroundWindow.ToString() : "null") + ", title: " + title);
focusedElement = AutomationElement.FocusedElement;
Log.Debug("focusedElement is " + ((focusedElement != null) ? "not null" : "null"));
Log.Debug("_currentfocusedElement is " + ((_currentFocusedElement != null) ? "not null" : "null"));
bool elementChanged = true;
var process = GetProcessForWindow(foregroundWindow);
// check if anything changed. did the window focus change?
// did focus change within the window?
if (focusedElement != null &&
(_forceGetActiveWindow || flag || foregroundWindow != _currentHwnd || _currentFocusedElement == null ||
(elementChanged = IsDifferent(focusedElement, _currentFocusedElement))))
{
//Log.Debug("Reason: _forceGetActiveWindow: " + _forceGetActiveWindow);
//Log.Debug("Reason: flag: " + flag);
//Log.Debug("Reason: fgHwnd != _currentHwnd : " + (fgHwnd != _currentHwnd));
//Log.Debug("Reason: _currentFocusedElement == null : " + (_currentFocusedElement == null));
//Log.Debug("Reason: elementChanged : " + elementChanged);
_forceGetActiveWindow = false;
if (EvtFocusChanged != null)
{
var monitorInfo = new WindowActivityMonitorInfo
{
FgHwnd = foregroundWindow,
Title = title,
FgProcess = process,
FocusedElement = focusedElement,
IsNewWindow = _currentHwnd != foregroundWindow
};
if (flag)
{
monitorInfo.IsNewWindow = true;
}
if (monitorInfo.IsNewWindow || _currentFocusedElement == null || elementChanged)
{
monitorInfo.IsNewFocusedElement = true;
}
#if VERBOSE
Log.Debug("#$#>>>>>>>>>>>>>>>> Triggering FOCUS changed event");
Log.Debug("#$# title: " + title);
Log.Debug("#$# fgHwnd " + monitorInfo.FgHwnd);
Log.Debug("#$# nativewinhandle: " + focusedElement.Current.NativeWindowHandle);
Log.Debug("#$# Process: " + process.ProcessName);
Log.Debug("#$# class: " + focusedElement.Current.ClassName);
Log.Debug("#$# controltype: " + focusedElement.Current.ControlType.ProgrammaticName);
Log.Debug("#$# automationid: " + focusedElement.Current.AutomationId);
Log.Debug("#$# newWindow: " + monitorInfo.IsNewWindow);
Log.Debug("#$# newFocusElement: " + monitorInfo.IsNewFocusedElement);
Log.Debug("#$# IsMinimized : " + Windows.IsMinimized(monitorInfo.FgHwnd));
#endif
if (monitorInfo.IsNewWindow)
{
AuditLog.Audit(new AuditEventActiveWindowChange(process.ProcessName, title));
}
if (EvtFocusChanged != null)
{
EvtFocusChanged(monitorInfo);
}
_currentFocusedElement = focusedElement;
}
else
{
Log.Debug("EvtFocusChanged is null");
}
}
_currentHwnd = foregroundWindow;
// raise the heartbeat event
if (EvtWindowMonitorHeartbeat != null && focusedElement != null && _heartbeatToggle)
{
var monitorInfo = new WindowActivityMonitorInfo
{
FgHwnd = foregroundWindow,
FocusedElement = focusedElement,
Title = title,
FgProcess = process
};
EvtWindowMonitorHeartbeat(monitorInfo);
}
_heartbeatToggle = !_heartbeatToggle;
}
catch (Exception e)
{
Log.Debug("exception: " + e);
_currentFocusedElement = null;
}
}