我用类向导生成OnTimer函数,环境是vc2008,在view类里面用向导添加一个菜单响应函数,由于添加完后仍是灰色的,无法点击,在网上找了个解决方法,在CMainFrame添加了ON_COMMAND菜单可以用,但是问题来了,通过调试发现,OnTimer在没有点击菜单项时就已经运行了,再点击菜单项时发现SetTimer后不能进入OnTimer函数了,如果在OnTimer调用基类的OnTimer函数之前加个return发现可以通过SetTimer可以进入OnTimer有定时功能,但是SetTimer无论怎么设置时间,OnTimer响应时间都不会变,菜单响应函数和OnTimer函数如下,其它位置是没有SetTimer函数的。
void Cgxcw_vs2008View::OnMenucirclestart()
{
// TODO: 在此添加命令处理程序代码
//UpdateData(TRUE);
KillTimer(m_nTimer);
working=true;
m_nTimer = SetTimer(1,3000,0);
((CMainFrame *)AfxGetMainWnd())->SetMessageText(_T("正在采集..."));}void Cgxcw_vs2008View::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值

if (working)
{
for(UINT i=0;i<m_graph1.GetNumberOfBars();i++)
{
m_graph1.SetBarValue(i,rand()%m_DispTo, (i==m_graph1.GetNumberOfBars()-1));
}
}
//return;
CListView::OnTimer(nIDEvent);
}

解决方案 »

  1.   

      KillTimer(m_nTimer);//KillTimer(1);
     
      m_nTimer = SetTimer(1,3000,0);,
    BOOL KillTimer( int nIDEvent );Return ValueSpecifies the outcome of the function. The value is nonzero if the event was killed. It is 0 if the KillTimer member function could not find the specified timer event.ParametersnIDEventThe value of the timer event passed to SetTimer.ResKills the timer event identified by nIDEvent from the earlier call to SetTimer. Any pending WM_TIMER messages associated with the timer are removed from the message queue.
      

  2.   

    别的地方哪还有SetTimer,不然不会进OnTimer的
      

  3.   

    没有了,搜索过,整个工和就这一个SetTimer
      

  4.   

    执行顺序就这几个函数很清楚了,而且还设置断点时发现OnTimer是在OnCreate函数后就接着运行了的
      

  5.   

    lz  你应该是装了两个定时器吧?第一个是OnCreate中第二个是菜单函数中,(干掉了第二个,重新安装了第二个)
      

  6.   

    之前测试的时候是在OnCreate函数了中放了个SetTimer函数,后来删掉了,现在整个工程只有菜单函数中那一个SetTimer函数
      

  7.   

     KillTimer(m_nTimer);
        working=true;
        m_nTimer = SetTimer(1,3000,0);
        ((CMainFrame *)AfxGetMainWnd())->SetMessageText(_T("正在采集..."));lz你尝试一下, 我没尝试过,  你的意思是 点击菜单, 才运行这几行代码
    那么就是没有安装定时器,而KillTimer会不会出什么问题?另外:  在定时器安装之前,你working=true;  这样做, 是不会导致定时器工作的。。真正的工作,是SetTimer !!!安装后才真正运行!!1
    也就是说 ,你安装了一次定时其,,其他地方没有修改定时器的代码!!!
    所以定时器 的时间不会改变!!!
      

  8.   

    你可以直接SetTimer(1,3000,0);然后 用switch(nIDEvent){
    case 1:
    //你的代码
    break;
    }
      
      

  9.   

    前面有一个问题就是在VC2008SP1在View类中添加菜单响应函数时菜单还是灰公的需要CMainFrame添加了ON_COMMAND在可以点击,于是我断点跟踪发现正是加了这个ON_COMMAND导致运行程序初始化后就会运行OnTimer函数,我觉得是不是CMainFrame中的ON_COMMADN把这个响应函数给运行了下