全局结构体定义,可以考虑放在类CPCToolApp的前面。
typedef struct LogMsg_t
{
CString szLog;
int iWriteLogFilgFlag;
}LogMsg;
// CPCToolApp:
// See PCTool.cpp for the implementation of this class
//
class CPCToolApp : public CWinApp
// The one and only CPCToolApp object
CPCToolApp theApp;
LogMsg gLog;
extern CPCToolApp theApp;
extern LogMsg gLog;
A:
发送端
gLog.szLog = TEXT("Hello world");
gLog.iWriteLogFilgFlag = 1;
::SendMessage(AfxGetApp()->m_pMainWnd->m_hWnd, WM_LOGMSG, 0, 0);
::PostMessage(AfxGetApp()->m_pMainWnd->m_hWnd, WM_LOGMSG, 0, 0);//都对,都可以
接收端
afx_msg LRESULT LOGMSGHandler(WPARAM, LPARAM);
在.cpp中添加消息映射
ON_MESSAGE(WM_LOGMSG, &CPCToolDlg::LOGMSGHandler)
在.cpp中实现LOGMSGHandler函数添加处理
LRESULT CPCToolDlg::LOGMSGHandler(WPARAM w, LPARAM l)
{
//CPCToolDlg类中的public函数
WriteLogs(gLog.szLog, gLog.iWriteLogFilgFlag);
return 0;
}
LogMsg lm;
lm.szLog = TEXT("xxxxxxx");
lm.iWriteLogFilgFlag = 1;
::SendMessage(AfxGetApp()->m_pMainWnd->m_hWnd, WM_LOGMSG, (WPARAM)&lm, 0);//此处只能用SendMessage,不能用PostMessage
接收端
afx_msg LRESULT LOGMSGHandler(WPARAM, LPARAM);
在.cpp中添加消息映射
ON_MESSAGE(WM_LOGMSG, &CPCToolDlg::LOGMSGHandler)
在.cpp中实现LOGMSGHandler函数添加处理
LRESULT CPCToolDlg::LOGMSGHandler(WPARAM w, LPARAM l)
{
LogMsg *lm;
lm = (LogMsg *)w;
TRACE(TEXT("%s, %d\n"), lm->szLog, lm->iWriteLogFilgFlag);
WriteLogs(lm->szLog, lm->iWriteLogFilgFlag);
return 0;
}