下面的代码可以实现子线程向窗体主线程发消息,
如何实现主线程向子线程发消息呢?
请熟悉这块的朋友帮忙介绍下,谢谢//////////////////////////////////////////////
////// thrdTestDlg.h : header fileclass CThrdTestDlg : public CDialog
{
public:  //AfxBeginThread相关参数
CWinThread   *       m_pThread; 
static   UINT     ThreadProc(LPVOID   pObj);
LRESULT OnMessage(WPARAM wParam, LPARAM lParam);
}
//////////////////////////////////////////////
////// thrdTestDlg.cpp : implementation file#define  UM_MESSAGE  2010 //接受消息用的消息值BEGIN_MESSAGE_MAP(CThrdTestDlg, CDialog)
//{{AFX_MSG_MAP(CThrdTestDlg)
ON_MESSAGE(UM_MESSAGE,OnMessage) //消息值和回调函数绑定
//}}AFX_MSG_MAP
END_MESSAGE_MAP()//启动线程
void CThrdTestDlg::OnButton1() 
{ m_pThread = AfxBeginThread(ThreadProc, this->m_hWnd); 
}//子线程入口函数
UINT   CThrdTestDlg::ThreadProc(LPVOID   pObj) 

CThrdTestDlg*   pThis   =   (CThrdTestDlg*)pObj; 
HWND hWnd = (HWND) pObj;
    ASSERT(hWnd);
CString str = "Hello,World,下午好";
    LPARAM  sendMsg =  (LPARAM)((LPCTSTR)str);  //从子线程向主窗体发消息
::SendMessage(hWnd,UM_MESSAGE,(WPARAM)0,sendMsg); 
return   0; 
} //主线程接收消息的函数
LRESULT CThrdTestDlg::OnMessage(WPARAM wParam, LPARAM lParam)
{   
LPCTSTR lpszText = (LPCTSTR)lParam;
AfxMessageBox(lpszText);
return 0;
}
//////////////////////////////////////////////

解决方案 »

  1.   

    子线程既然能SendMessage,那么它也能GetMessage吧
      

  2.   

    PostMessage 发给子线程创建的窗口,默认情况下是发给窗口所在的线程的,然后由该线程去取消息,去派送
      

  3.   


    --------------------
    主线程向子线程发消息,要用PostThreadMessage么?
      

  4.   

    1、mythread.h: 
    afx_msg   void   OnDestroy(WPARAM   wParam,   LPARAM   lParam   ); 
    2、mythread.cpp (1)   ON_THREAD_MESSAGE(   WM_DESTROY,   OnDestroy) 
    (2) 
    void   MyThread::OnDestroy(WPARAM   wParam,   LPARAM   lParam   ) 

    PostQuitMessage(0); 
    }; 
    wm_destroy消息可以处理!