使用高精度定时函数timeSetEvent 周期回调类成员函数,网上说回调函数必须是静态函数,可这样怎么访问类的成员变量呢?跪求高手解答!多谢。下面是部分代码:
//类定义
class CVcsimDlg : public CDialog
{
// Construction
public:
CVcsimDlg(CWnd* pParent = NULL); // standard constructor
///tj
  int i;//自定义变量
  FILE * pFile;
  UINT timerId;  //定时器ID
...}
//部分源文件
UINT CVcsimDlg::OnButton2() 
{
// TODO: Add your control notification handler code here
///tj
timerId=timeSetEvent(1000,1,timerCall,(DWORD)this,TIME_PERIODIC);//开启计时
        return timerId;
}///tj
 static void timerCall(UINT id, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
   i++;
fprintf(pFile,"%d\n",i);
}
void CVcsimDlg::OnButton3() 
{
// TODO: Add your control notification handler code here
///TJ
timeKillEvent(timerId);//关闭定时器
}提示编译错误
error C2065: 'i' : undeclared identifier
error C2065: 'pFile' : undeclared identifier
Error executing cl.exe.

解决方案 »

  1.   

    通过指针间接访问即可。timerId=timeSetEvent(1000,1,timerCall,(DWORD)this,TIME_PERIODIC);//开启计时
    你这里不是将this指针传递进入了吗
      

  2.   

     static void timerCall(UINT id, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)
    {
      i++;
    fprintf(pFile,"%d\n",i);

    }
    timerCall是静态函数,不能直接直接操作非静态成员变量可以这样
     static void timerCall(UINT id, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)
    {
      CVcsimDlg* pdlg = (CVcsimDlg*)dwUser;
      pdlg->i++;
    fprintf(pdlg->pFile,"%d\n",pdlg->i);
    }