回 帖 发 新 帖 刷新版面

主题:自定义消息出错

最近学习VC6.0,书上有个自定义消息例程
创建一个单文档应用程序,添加WM_RBUTTONDOWN消息映射
第一步:
打开头文件CP2_3.View.h,在其开始位置添加如下代码,定义一个自定义消息WM_MYMESSAGE。
#define  WM_MYMESSAGE WM_USER+1;
第二步:
在头文件CP2_3View.h的末尾声明自定义消息处理函数OnMyMessage()。
afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);

// Generated message map functions
protected:
    //{{AFX_MSG(CP2_3View)
    afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
    afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};


第三步:
打开实现文件CP2_3.View.cpp, 在消息映射表中添加自定义消息映射宏。
ON_MESSAGE(WM_MYMESSAGE, OnMyMessage);

代码:
/////////////////////////////////////////////////////////////////////////////
// CP2_3View

IMPLEMENT_DYNCREATE(CP2_3View, CView)

BEGIN_MESSAGE_MAP(CP2_3View, CView)
    //{{AFX_MSG_MAP(CP2_3View)
    ON_WM_RBUTTONDOWN()//这是第23行
    ON_MESSAGE(WM_MYMESSAGE, OnMyMessage)//第24行
    //}}AFX_MSG_MAP
    // Standard printing commands
    ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
第四步:
在CP2_3View.cpp中添加OnMyMessage()函数实现代码

LRESULT CP2_3View::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
    MessageBox("自定义消息 WM_MYMESSAGE 的处理函数被调用!");
    return 0;
}

第五步:
打开WM_RBUTTONDOWN消息的处理函数OnRButtonDown(),在该函数中添加如下代码,发送自定义消息。
SendMessage(WM_MYMESSAGE);

void CP2_3View::OnRButtonDown(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default
    SendMessage(WM_MYMESSAGE);
    CView::OnRButtonDown(nFlags, point);
}

但是编译错误
P2_3View.cpp
D:\C++\code\vc\P2_3\P2_3View.cpp(24) : error C2059: syntax error : ';'
D:\C++\code\vc\P2_3\P2_3View.cpp(24) : error C2143: syntax error : missing ';' before ','
D:\C++\code\vc\P2_3\P2_3View.cpp(24) : error C2143: syntax error : missing ';' before '}'
D:\C++\code\vc\P2_3\P2_3View.cpp(24) : error C2143: syntax error : missing ';' before '}'
D:\C++\code\vc\P2_3\P2_3View.cpp(27) : error C2143: syntax error : missing ';' before '{'
D:\C++\code\vc\P2_3\P2_3View.cpp(27) : error C2447: missing function header (old-style formal list?)
D:\C++\code\vc\P2_3\P2_3View.cpp(27) : error C2143: syntax error : missing ';' before ','
D:\C++\code\vc\P2_3\P2_3View.cpp(28) : error C2143: syntax error : missing ';' before '{'
D:\C++\code\vc\P2_3\P2_3View.cpp(28) : error C2447: missing function header (old-style formal list?)
D:\C++\code\vc\P2_3\P2_3View.cpp(28) : error C2143: syntax error : missing ';' before ','
D:\C++\code\vc\P2_3\P2_3View.cpp(29) : error C2143: syntax error : missing ';' before '{'
D:\C++\code\vc\P2_3\P2_3View.cpp(29) : error C2447: missing function header (old-style formal list?)
D:\C++\code\vc\P2_3\P2_3View.cpp(29) : error C2143: syntax error : missing ';' before ','
D:\C++\code\vc\P2_3\P2_3View.cpp(30) : error C2143: syntax error : missing ';' before '{'
D:\C++\code\vc\P2_3\P2_3View.cpp(30) : error C2447: missing function header (old-style formal list?)
D:\C++\code\vc\P2_3\P2_3View.cpp(30) : error C2143: syntax error : missing ';' before '}'
D:\C++\code\vc\P2_3\P2_3View.cpp(109) : error C2143: syntax error : missing ')' before ';'
D:\C++\code\vc\P2_3\P2_3View.cpp(109) : error C2059: syntax error : ')'
Error executing cl.exe.

P2_3.exe - 18 error(s), 0 warning(s)

回复列表 (共4个回复)

沙发

先不管代码的问题,可见你的调试能力还需加强。。。

板凳


自定义消息时出错了,仔细检查一下!
去掉“#define  WM_MYMESSAGE   WM_USER+1;”后的分号,#define定义宏时后边不加分号。

3 楼

谢谢ChRise,平时都是用const,没发现这个错误。
另外编译时并没说 #define 那行错误,这如何知道呢?

4 楼


在MSDN中查出错的编号C2059,大概就能知道在哪错了。

我来回复

您尚未登录,请登录后再回复。点此登录或注册