问题描述
我目前正在将旧源代码转移到 Visual Studio 2019。
原始代码来自 VC++6 或更早版本。
在Windows MFC中,有一个名为CWinThread
的类,根据旧的源代码,该类中存在m_msgCur
。但是,在 VS2019 中,它说 m_msgCur
不存在。然后我发现m_msgCur
很久以前就存在(https://github.com/dblock/msiext/blob/master/externals/WinDDK/7600.16385.1/inc/mfc42/afxwin.h,MFC 4.2),在VS2019中被删除了。 MSG m_msgCur
包含线程的当前消息,但 VS2019 中是否有替代变量?
// message pump for Run
MSG m_msgCur; // current message
已编辑:
项目链接:https://github.com/ValveSoftware/halflife/tree/master/utils/serverctrl
类 CServerCtrlDlg
(继承自 CDialog
)在 ServerCtrlDlg.h
中声明。 ServerCtrlDlg.cpp
包含它的工作原理。 ServerCtrlClg.cpp
包含 <afxpriv.h>
,而 <afxpriv.h>
包含 afxwin.h
,其中包含 CWinThread
。
使用 m_msgCur 的 ServerCtrlDlg 的一部分
int CServerCtrlDlg::RunModalLoop(DWORD dwFlags)
{
ASSERT(::IsWindow(m_hWnd)); // window must be created
ASSERT(!(m_nFlags & WF_MODALLOOP)); // window must not already be in modal state
// for tracking the idle time state
BOOL bIdle = TRUE;
LONG lIdleCount = 0;
BOOL bShowIdle = (dwFlags & MLF_SHOWONIDLE) && !(GetStyle() & WS_VISIBLE);
HWND hWndParent = ::GetParent(m_hWnd);
m_nFlags |= (WF_MODALLOOP|WF_CONTINUEMODAL);
MSG* pMsg = &AfxGetThread()->m_msgCur;
// acquire and dispatch messages until the modal state is done
for (;;)
{
ASSERT(ContinueModal());
int iRet = rmlPreIdle();
if (iRet < 0)
goto ExitModal;
else if (iRet > 0)
continue;
// phase1: check to see if we can do idle work
while (bIdle &&
!::PeekMessage(pMsg,NULL,PM_norEMOVE))
{
ASSERT(ContinueModal());
// show the dialog when the message queue goes idle
if (bShowIdle)
{
ShowWindow(SW_SHOWnorMAL);
UpdateWindow();
bShowIdle = FALSE;
}
// call OnIdle while in bIdle state
if (!(dwFlags & MLF_NOIDLEMSG) && hWndParent != NULL && lIdleCount == 0)
{
// send WM_ENTERIDLE to the parent
::SendMessage(hWndParent,WM_ENTERIDLE,MSGF_DIALOGBox,(LParaM)m_hWnd);
}
if ((dwFlags & MLF_NOKICKIDLE) ||
!SendMessage(WM_KICKIDLE,lIdleCount++))
{
// stop idle processing next time
bIdle = FALSE;
}
}
// phase2: pump messages while available
do
{
BOOL ShouldPump = TRUE;
ASSERT(ContinueModal());
// See if we are requiring messages to be in queue?
if ( m_bOnlyPumpIfMessageInQueue )
{
// If there isn't a message,don't turn over control to PumpMessage
// since it will block
if ( !::PeekMessage( pMsg,PM_norEMOVE ) )
{
ShouldPump = FALSE;
}
}
// pump message,but quit on WM_QUIT
if ( ShouldPump )
{
if (!AfxGetThread()->PumpMessage())
{
AfxPostQuitMessage(0);
return -1;
}
// show the window when certain special messages rec'd
if (bShowIdle &&
(pMsg->message == 0x118 || pMsg->message == WM_SYSKEYDOWN))
{
ShowWindow(SW_SHOWnorMAL);
UpdateWindow();
bShowIdle = FALSE;
}
if (!ContinueModal())
goto ExitModal;
// reset "no idle" state after pumping "normal" message
if (AfxGetThread()->IsIdleMessage(pMsg))
{
bIdle = TRUE;
lIdleCount = 0;
}
}
} while (::PeekMessage(pMsg,PM_norEMOVE));
}
ExitModal:
m_nFlags &= ~(WF_MODALLOOP|WF_CONTINUEMODAL);
return m_nModalResult;
}
解决方法
简短的回答是替换:
MSG* pMsg = &AfxGetThread()->m_msgCur; // vc++ 6
与:
MSG *pMsg = AfxGetCurrentMessage(); // vc++ 2019
然而,更好的答案是,如果您必须 hack CWnd::RunModalLoop
,那么您应该做对,并更新 CServerCtrlDlg::RunModalLoop
以基于当前的 {{1 }} 来自 VC++ 2019 的代码,而不是从 VC++ 6 借来的旧代码。
将 CWnd::RunModalLoop
与来自 CServerCtrlDlg::RunModalLoop
的 VC++ 6 中的 CWnd::RunModalLoop
实现进行比较显示以下标有 mfc\src\wincore.cpp
的行已添加。
//++
int CServerCtrlDlg::RunModalLoop(DWORD dwFlags)
{
//...
// acquire and dispatch messages until the modal state is done
for (;;)
{
ASSERT(ContinueModal());
int iRet = RMLPreIdle(); //++
//++
if (iRet < 0) //++
goto ExitModal; //++
else if (iRet > 0) //++
continue; //++
// phase1: check to see if we can do idle work
while (bIdle &&
!::PeekMessage(pMsg,NULL,PM_NOREMOVE))
{
ASSERT(ContinueModal());
//...
//...
// phase2: pump messages while available
do
{
ASSERT(ContinueModal());
BOOL ShouldPump = TRUE; //++
//++
// See if we are requiring messages to be in queue? //++
if ( m_bOnlyPumpIfMessageInQueue ) //++
{ //++
// If there isn't a message,don't turn over control to PumpMessage //++
// since it will block //++
if ( !::PeekMessage( pMsg,PM_NOREMOVE ) ) //++
{ //++
ShouldPump = FALSE; //++
} //++
} //++
if ( ShouldPump ) //++
{
/* mfc code executed conditionally */
}
//...
}
应更新为使用 VC++ 2019 CServerCtrlDlg::RunModalLoop
代码,然后将这些 CWnd::RunModalLoop
行合并到其中。 //++
中的 MFC 更改在两个版本之间足够小,合并很简单。