error C2440: 'static_cast' : cannot convert from 'LRESULT (__thiscall CchatDlg::* )(CPoint)' to 'UI

出现这个错误的原因可是“人力不可抗拒”之原因造成的,因为旧版本的 ON_WM_NCHITTEST 宏使用了

  UINT (__thiscall CWzButton::* )(CPoint);

  类型的类成员函数指针,其定义如下:

  #define ON_WM_NCHITTEST() /

  { WM_NCHITTEST, 0, 0, 0, AfxSig_wp, /

  (AFX_PMSG)(AFX_PMSGW)(UINT (AFX_MSG_CALL CWnd::*)(CPoint))&OnNcHitTest },

  但是新版本变成了:

  #define ON_WM_NCHITTEST() /

  { WM_NCHITTEST, 0, 0, 0, AfxSig_l_p, /

  (AFX_PMSG)(AFX_PMSGW) /

  (static_cast< LRESULT (AFX_MSG_CALL CWnd::*)(CPoint) > (&ThisClass :: OnNcHitTest)) },

  注意返回值类型由UINT改成了LRESULT,再加上static_cast的严格检查,所以就出错了。修改的方法就是将你的OnNcHitTest函数由:

  afx_msg UINT OnNcHitTest(CPoint point);

  改成:

  afx_msg LRESULT OnNcHitTest(CPoint point);

关于VS2005的自定义消息

在VC6.0里

头文件MainFrm.h:   
    
 #define   UM_PROGRESS   WM_USER+1   //定义消息   

 

afx_msg   void   OnProgress();   //   生成的消息映射函数   

 

源文件MainFrm.cpp:   

 

BEGIN_MESSAGE_MAP(CMainFrame,   CFrameWnd)   
  ON_MESSAGE(UM_PROGRESS,OnProgress)   
  END_MESSAGE_MAP()   
    
  //消息实现   
  void   CMainFrame::OnProgress()   
  {   
        //todo:   
  }  

能编译通过,但在VS2005里编译的时候在消息映射那里出错了:  
  error   C2440:   “static_cast”:   无法从“void   (__thiscall   CMainFrame::*   )(void)”转换为“LRESULT   (__thiscall   CWnd::*   )(WPARAM,LPARAM)”  

 

回答:There   is   a   bug   in   6.0   compiler   that   permits   you   to   use   afx_msg   void   OnProgress();.   You   need   to   change   your   function   to   LRESULT   OnProgress(WPARAM,LPARAM).

 

问题:但是为什么MFC在其他的消息或者事件响应里面却能这样写呢?它不也都是用的void来申明的?

 

回答:Some   message   handler   functions   such   as   the   wizard   generated   WM_CREATE   handler   are   virtual   functions   derived   from   default   MFC   implementations.   Check   out   MFC   source   code   to   see   how   MFC   handle   these   messages.

 

修改后变成:

头文件MainFrm.h:   
    
 #define   UM_PROGRESS   WM_USER+1   //定义消息   

afx_msg LRESULT  OnProgress(WPARAM wlparam,LPARAM lparam);   //   生成的消息映射函数   

源文件MainFrm.cpp:   

BEGIN_MESSAGE_MAP(CMainFrame,   CFrameWnd)   
  ON_MESSAGE(UM_PROGRESS,OnProgress)   
  END_MESSAGE_MAP()   
    
  //消息实现   

 LRESULT  CMainFrame::OnProgress(WPARAM wlparam,LPARAM lparam)   
  {   
        //todo:   
  }  

下面还有一个例子:

 

 

(1)   手工定义消息,可以这么写   
  #define   WM_MY_MESSAGE(WM_USER+100),    
  MS   推荐的至少是   WM_USER+100   
    
  (2)声明消息 
  afx_msg   LRESULT   OnMyMessage(   WPARAM   wParam,   LPARAM   lParam   ); 
  (3)消息映射在cpp文件中的消息映射中加入: 
  BEGIN_MESSAGE_MAP(CMainFrame,   CFrameWnd) 
          //{{AFX_MSG_MAP(CMainFrame)  
                  //   NOTE   -   the   ClassWizard   will   add   and   remove   mapping   macros   here.  
                  //         DO   NOT   EDIT   what   you   see   in   these   blocks   of   generated   code   !  
          ON_WM_CREATE() 
          //}}AFX_MSG_MAP  
          ON_MESSAGE(WM_MY_MESSAGE,   OnMyMessage) 
  END_MESSAGE_MAP() 
  (4)写消息处理函数,用   
  WPARAM,LPARAM返回LRESULT.   
  LRESULT   CMainFrame::OnMyMessage(WPARAM   wparam,LPARAM   lParam)   
  {   
          temp目录:   Use   "GetTempPath" 
          //加入你的处理函数   irectory"  
  }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值