1.添加自定义消息(Lesson16_ChatDlg.h文件中)
#define UM_SOCK WM_USER+1
2.添加消息响应函数原型的声明(Lesson16_ChatDlg.h文件)
afx_msg void OnSock(WPARAM, LPARAM);
3.添加消息映射(Lesson16_ChatDlg.cpp文件)
ON_MESSAGE(UM_SOCK, OnSock)
4.实现函数
void CLesson16_ChatDlg::OnSock(WPARAM wParam, LPARAM lParam)
{}
5.编译
error C2440: 'static_cast' : cannot convert from 'void (__thiscall CLesson16_ChatDlg::* )(WPARAM,LPARAM)' to 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)'
1> None of the functions with this name in scope match the target type
6.问题解决
修改2和4中的viod 为LRESULT 如下所示:
afx_msg LRESULT OnSock(WPARAM, LPARAM);
LRESULT CLesson16_ChatDlg::OnSock(WPARAM wParam, LPARAM lParam)
{
return TRUE;
}
7.重新编译通过
/***********************************************************************************************************************************/
补充:
不能转换void (_thiscall CMainFrame::*)(void)to LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)
开发平台由VC6.0升级至VS2010,需要将原有的项目迁移,可能碰到类似错误:
error C2440: 'static_cast' : cannot convert from 'void (__thiscall CMainFrame::* )(void)' to 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)'
VS2010对消息的检查更为严格,以前在VC6下完全正常运行的消息映射在VS2005下编译不通过
ON_MESSAGE(WM_message,OnMyMessage);
OnMyMessage返回值必须为LRESULT,其形式为:afx_msg LRESULT OnMyMessage(WPARAM, LPARAM);如果不符合,则有错误提示:
error C2440: “static_cast”: 无法从“void (__thiscall CPppView::* )(WPARAM,LPARAM)”转换为“LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)”
在匹配目标类型的范围内没有具有该名称的函数
error C2440: “static_cast”: 无法从“void (__thiscall CPppView::* )(void)”转换为“LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)”
在匹配目标类型的范围内没有具有该名称的函数
解决方法如下:
首先,把原来的消息函数返回值类型改为LRESULT,函数内可以随便写个return TRUE; 然后消息函数的参数必须改写成(WPARAM wParam,LPARAM lParam)而不论这两个参数是否用得到;最后,消息映射如ON_MESSAGE(WM_message,& OnMyMessage)