Win32 :: SetForegroundWindow () is not working all the time

I am working on a messenging tool. The message box is part of the whole application. I need a window to go to the front when some messages appear. I am using this code:

    if( m_hwnd == NULL || !::IsWindow(m_hwnd) )
        return E_UNEXPECTED;

    if(::IsIconic(m_hwnd))
    {
        ::ShowWindowAsync( m_hwnd, SW_RESTORE );
    }
    ::SetWindowPos(m_hwnd, HWND_TOP, 0, 0, 0, 0, SWP_ASYNCWINDOWPOS | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
    ::SetForegroundWindow(m_hwnd);
    if( pvbProcessed != NULL )
        *pvbProcessed = VARIANT_TRUE;

    return S_OK;

I even tried to make TOPMOST, but in some cases it does not work. I also tried :: BringToFront ().

Can anyone help or explain why it is not working? This is a known limitation for Microsoft.

+6
source share
1 answer

  The system limits which processes can set the foreground window. A process can set a foreground window only if one of the following conditions is true:

  • .
  • .
  • .
  • .
  • .
  • (. LockSetForegroundWindow).
  • (. SPI_GETFOREGROUNDLOCKTIMEOUT SystemParametersInfo).
  • .

SetForegroundWindow() .

+8

All Articles