if(::GetForegroundWindow() != GetSafeHwnd()) {
    HWND hActiveWnd = ::GetForegroundWindow();
    if(hActiveWnd != NULL) {
        DWORD ThreadID = GetWindowThreadProcessId(hActiveWnd, NULL);
        DWORD CurrentThreadID = GetCurrentThreadId();
        if(CurrentThreadID != ThreadID) {
            if(AttachThreadInput(CurrentThreadID, ThreadID, TRUE)) {
                ::BringWindowToTop(GetSafeHwnd());
                AttachThreadInput(CurrentThreadID, ThreadID, FALSE);
            }
        }
    }
}