如题。
查msdn,知道SetTimer有一个参数是hWnd。如果设为NULL的话,则会忽略参数nIDEvent。
但是我需要使用几个SetTimer函数,所以不能忽略参数nIDEvent,必须设置hWnd。那hWnd应设为什么?

解决方案 »

  1.   

    Main Window's handle needed handle this message!!
      

  2.   

    如果你的类不是从CWnd或者其他窗口类继承的,必须自己找一个接受窗口消息的窗口,得到窗口句柄。另外,MSDN提到,该窗口必须是调用线程的。
      

  3.   

    我做了一个Timer,你用用看
    #pragma once
    #include <windows.h>
    #include <afxtempl.h>
    //2003.01.22
    //For VC++.Net/*/
    CTimer:这是一个定时器
    模板T参数:属主类的名称。例如,你可能要在CMainFrame的类定中声明一个
    定时器,可以这样定义:
    CTimer<CMainFrame> Timer;
    另外,你必须在CMainFrame中写一个OnTimer(CTimer<CMainFrame>*pThis)的
    处理函数,然后把这个OnTimer赋值给CTimer的成员变量OnTimer方法:
    1.  CTimer(void):构造函数。如果使用这个构造函数,则以后还必须调用CreateTimer(...),来创建并
    启动定时器
    2.  CTimer(int timeout):构造函数,如果使用这个构造函数,则以后不必调用CreateTimer(...),
    3.  CreateTimer(...):创建并启动一个定时器,timeout是定时间隔
    4.  DestroyTimer(...):销毁定时器
    5.  SetOwner(...):设置定时器的属主属性:
    1.  TimeOut:定时间隔
    2.  Enabled:定时器开关事件:
    1.  OnTimer(...):pThis:类实例的this指针,注意OnTimer必须在属主类中编写,然后赋值给这个成员函数指针//*/template<class T>
    class CTimer
    {
    public:
      CTimer(void);
      CTimer(int timeout);
      virtual ~CTimer(void);
      
      void (T::*OnTimer)(CTimer<T>*pThis);//OnTimer事件处理器
      
      void CreateTimer(int timeout);
      void DestroyTimer();
      void SetOwner(T*pOwner);
      
      __declspec(property(get=GetTimeOut,put=PutTimeOut)) int  TimeOut;
      __declspec(property(get=GetEnabled,put=PutEnabled)) bool Enabled;
    public :
      int  GetTimeOut();
      void PutTimeOut(int timeout);
      bool GetEnabled();
      void PutEnabled(bool enable);
    protected:
      int ID;
      int __TimeOut;
      T*pT;
      int Lookup(CTimer<T>* This);
      static CArray<CTimer<T>*,CTimer<T>*> ThisArray;
      static void CALLBACK TimerProc(HWND hWnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime);
    };//实现
    template<class T>
    void CTimer<T>::PutEnabled(bool enable)
    {
      if((!GetEnabled())&&(enable))
      {
        DestroyTimer();
        CreateTimer(TimeOut);
      }
      else if(GetEnabled()&&(!enable))
      {
        DestroyTimer();
      }
    }template<class T>
    bool CTimer<T>::GetEnabled()
    {
      if(ID!=0)
        return true;
      else return false;
    }template<class T>
    int CTimer<T>::Lookup(CTimer<T>* This)
    {
      int index=-1;
      int count =ThisArray.GetUpperBound()+1;
      for(int i=0;i<count;++i)
      {
        if(ThisArray[i]==This)
        {
          index=i;
          break;
        }
      }
      return index;
    }template<class T>
    CArray<CTimer<T>*,CTimer<T>*> CTimer<T>::ThisArray;template<class T>
    void CTimer<T>::TimerProc(HWND hWnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)
    {
      int count=ThisArray.GetUpperBound()+1;
      for(int i=0;i<count;++i)
      {
        if(ThisArray[i]->ID==idEvent)  
          break;
      }
      if(i>=count)
        return;
      int index=i;
      CTimer<T>*pThis=ThisArray[index];
      if((pThis->pT!=NULL)&&(pThis->OnTimer!=NULL))
        (pThis->pT->*(pThis->OnTimer))(pThis);//*/  
    }template<class T>
    void CTimer<T>::DestroyTimer()
    {
      if(ID!=0)
      {
        KillTimer(NULL,ID);
        ID=0;
      }
    }template<class T>
    void CTimer<T>::SetOwner(T*pOwner)
    {
      pT=pOwner;
    }template<class T>
    int CTimer<T>::GetTimeOut()
    {
      return __TimeOut;
    }template<class T>
    void CTimer<T>::PutTimeOut(int timeout)
    {
      __TimeOut=timeout;
    }
    //构造函数
    template<class T>
    CTimer<T>::CTimer(void)
    {
      ID=0;
      __TimeOut=0;
      pT=NULL;
      OnTimer=NULL;
      ThisArray.Add(this);
    }template<class T>
    CTimer<T>::CTimer(int timeout)
    {
      ID=0;
      __TimeOut=1000;
      pT=NULL;
      OnTimer=NULL;
      ThisArray.Add(this);
      CreateTimer(timeout);
    }//析构函数
    template<class T>
    CTimer<T>::~CTimer(void)
    {
      DestroyTimer();
      int index=Lookup(this);
      if(index!=-1)
      {
        ThisArray.RemoveAt(index);
      }
    }template<class T>
    void CTimer<T>::CreateTimer(int timeout)
    {
      DestroyTimer();  
      ID=SetTimer(NULL,0,timeout,TimerProc); 
      __TimeOut=timeout;
    }