在文档视图程序里面,如何向Doc发送消息
在单文档视图程序中,在Doc类中添加了消息:
.h:
#define WM_CONTRALLINEWORK WM_USER+1
public:
//WM_CONTRALLINEWORK消息处里函数原型
LRESULT OnContralLineWork(WPARAM wParam,LPARAM lParam);
.cpp中:
//消息响应函数
BEGIN_MESSAGE_MAP(CHowTestDoc, CDocument)
//{{AFX_MSG_MAP(CHowTestDoc)
// NOTE - the ClassWizard will add and remove mapping macros here.
//    DO NOT EDIT what you see in these blocks of generated code!
ON_MESSAGE(WM_CONTRALLINEWORK,OnContralLineWork)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
LRESULT CHowTestDoc::OnContralLineWork(WPARAM wParam,LPARAM lParam)
{
CString show;
show.Format("收到消息,参数为:%d,%d",wParam,lParam);
AfxMessageBox(show);
return 0;
}
这样,在View里面或别的地方怎么向它发送这个消息呢?试了几个函数都不行,多谢各位指点!!

解决方案 »

  1.   

    1、试试:
    SendMessage(NULL, WM_CONTRALLINEWORK,....)2、以WM开头的消息一般是窗口消息,可你该消息的响应又不在窗口类中,所以你的设计是否合适?
      

  2.   

    BEGIN_MESSAGE_MAP(CHowTestDoc, CDocument)
    //{{AFX_MSG_MAP(CHowTestDoc)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    //    DO NOT EDIT what you see in these blocks of generated code!
    ON_COMMAND(WM_CONTRALLINEWORK,OnContralLineWork)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    在视图里:
    this->SendMessage(WM_COMMAND, WM_CONTRALLINEWORK, 0);文档不是窗口,不能接收消息,但是它能接收WM_COMMAND,原因是:
    BOOL CView::OnCmdMsg(UINT nID, int nCode, void* pExtra,
    AFX_CMDHANDLERINFO* pHandlerInfo)
    {
    // first pump through pane
    if (CWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
    return TRUE; // then pump through document
    if (m_pDocument != NULL)
    {
    // 注意这里!
    CPushRoutingView push(this);
    return m_pDocument->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
    } return FALSE;
    }
    所以,你把你的自定义消息命名成自定义命令更好!
      

  3.   

    BEGIN_MESSAGE_MAP(CHowTestDoc, CDocument)
    //{{AFX_MSG_MAP(CHowTestDoc)
    //    这里改成:
    ON_COMMAND(WM_CONTRALLINEWORK,OnContralLineWork)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    在视图里:
    this->SendMessage(WM_COMMAND, WM_CONTRALLINEWORK, 0);文档不是窗口,不能接收消息,但是它能接收WM_COMMAND,原因是:
    BOOL CView::OnCmdMsg(UINT nID, int nCode, void* pExtra,
    AFX_CMDHANDLERINFO* pHandlerInfo)
    {
    // first pump through pane
    if (CWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
    return TRUE; // then pump through document
    if (m_pDocument != NULL)
    {
    // 注意这里!
    CPushRoutingView push(this);
    return m_pDocument->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
    } return FALSE;
    }
    所以,你把你的自定义消息命名成自定义命令更好!
      

  4.   

    文档视图结构中,需把自定义消息改为自定义command