简体   繁体   中英

MFC/C++ Bring window to top no longer works

I have a 32-bit application built on MFC/C++. It was ported from Visual C++ (6.0) to Studio 2015.

The application needs to come to the top when new data appears. On a couple of my customer systems the window does not come to the front as another application seems to want to stay on top. This code (in MainFrm.cpp) was working fine prior to the 2015 port (same "topmost" source code).

...

HWND hCurrWnd;
int iMyTID;
int iCurrTID;

hCurrWnd = ::GetForegroundWindow();
iMyTID   = GetCurrentThreadId();
iCurrTID = GetWindowThreadProcessId(hCurrWnd,0);

::AttachThreadInput(iCurrTID, iMyTID, TRUE);
::SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
::SetWindowPos(m_hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
::SetForegroundWindow(m_hWnd);
::SetFocus(m_hWnd);
::SetActiveWindow(m_hWnd);
::AttachThreadInput(iCurrTID, iMyTID, FALSE);

...

Is there another, more robust method for forcing a window to the top in MFC?

Use MFC functions direct, not the underlying WinApi. I tested this in Win10 with VS2019.

RESULT CMainFrame::OnNewDataReceived_UM( WPARAM wParam, LPARAM lParam )
{
    :  
    if (IsIconic())
        ShowWindow(SW_RESTORE);
    SetForegroundWindow();
    :
  } 

   returen TRUE;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM