- UID
- 675574
- 积分
- 450
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2013-5-4
- 最后登录
- 1970-1-1
|
发表于 2013-7-28 01:30:35
|
显示全部楼层
应该可以吧,我是这样做的,把定时器做成类结构,再在DOC类中定义一个map容器保存当前文档的定时器类,文档被释构时同时释放已实例化的定时器类,这样做还可以同时在同一文档定义多个定时器。具体代码如下:
 - #include "StdAfx.h"
- #include "Timer.h"
- class Timer
- {
- public:
- Timer(HWND CurHandle,UINT nElapse,const ACHAR * CallbackFunction);
- static void CALLBACK TimerProc(HWND hWnd,UINT nMsg,UINT nTimerid,DWORD dwTime);
- static int LispTimer(void);
- static int KillLispTimer(void);
- public:
- ~Timer(void);
- private:
- ACHAR *m_CallbackFunction;
- UINT m_nElapse;
- static UINT TimerID;
- AcApDocument *m_doc;
- HWND m_CurHandle;
- UINT_PTR uIDEvent;
- };
- UINT Timer::TimerID = 1000;
- Timer::Timer(HWND CurHandle,UINT nElapse,const ACHAR * CallbackFunction):m_CurHandle(CurHandle)
- {
- if (nElapse > 0)
- {
- m_nElapse = nElapse;
- }
- else
- {
- m_nElapse = 500;
- }
- TimerID++;
- m_doc = acDocManager->curDocument();
- m_CallbackFunction = new ACHAR[wcslen(CallbackFunction)+ 1];
- wcscpy(m_CallbackFunction,CallbackFunction);
- //m_CurHandle = CurHandle;\\adsw_acadDocWnd();
- uIDEvent = SetTimer(m_CurHandle,TimerID,nElapse,(TIMERPROC)Timer::TimerProc);
- if (uIDEvent == 0)
- {
- --TimerID;
- }
- }
- Timer::~Timer(void)
- {
- if (m_CallbackFunction != NULL)
- {
- delete[] m_CallbackFunction;
- m_CallbackFunction = NULL;
- }
- }
- void Timer::TimerProc(HWND hWnd,UINT nMsg,UINT nTimerid,DWORD dwTime)
- {
- AcApDocument *curdoc = acDocManager->curDocument();
- Timer *curtimer = DocVars.docData().mapTimers[nTimerid];
- if (curtimer->m_doc != curdoc)
- {
- return;
- }
- struct resbuf *in_rb=NULL,*out_rb=NULL;
- in_rb = acutBuildList(RTSTR,curtimer->m_CallbackFunction,RTSHORT,hWnd,RTSHORT,nMsg,RTSHORT,nTimerid,RTSHORT,dwTime,0);
- if (in_rb == NULL)
- {
- return;
- }
- if (acedInvoke(in_rb,&out_rb) != RTNORM )
- {
- return;
- }
- acutRelRb(in_rb);
- in_rb = NULL;
- if (out_rb != NULL)
- {
- acutRelRb(out_rb);
- out_rb = NULL;
- }
- return;
- }
- int Timer::LispTimer()
- {
- struct resbuf *m_rb = acedGetArgs();
- if (m_rb == NULL || m_rb->rbnext == NULL || m_rb->restype != RTSHORT ||(m_rb->rbnext->restype != RTSHORT && m_rb->rbnext->restype != RTSTR))
- {
- return RTERROR;
- }
- HWND CurHandle ;
- if (m_rb->restype == RTSHORT && m_rb->rbnext->restype == RTSHORT)
- {
- if (m_rb->rbnext->rbnext == NULL || m_rb->rbnext->rbnext->restype != RTSTR)
- {
- return RTERROR;
- }
- CurHandle =(HWND) m_rb->resval.rint;
- m_rb = m_rb->rbnext;
- }
- else
- {
- CurHandle = adsw_acadDocWnd();
- }
- Timer *myTime = new Timer(CurHandle,m_rb->resval.rint,m_rb->rbnext->resval.rstring);
- if (myTime->uIDEvent == 0)
- {
- delete myTime;
- myTime = NULL;
- acedRetNil();
- }
- else
- {
- DocVars.docData().mapTimers[myTime->uIDEvent] = myTime;
- acedRetInt(myTime->uIDEvent);
- }
- return RSRSLT;
- }
- int Timer::KillLispTimer(void)
- {
- struct resbuf *m_rb = acedGetArgs();
- if (NULL == m_rb || m_rb->restype != RTSHORT)
- {
- acutPrintf(_T("\n参数类型错误。"));
- return RTERROR;
- }
- map<UINT,Timer*>::iterator iter;
- iter = DocVars.docData().mapTimers.find((UINT)m_rb->resval.rint);
- if (iter == DocVars.docData().mapTimers.end())
- {
- acedRetNil();
- return RSRSLT;
- }
- Timer *m_timer = DocVars.docData().mapTimers[m_rb->resval.rint];
- bool isok;
- isok = KillTimer(m_timer->m_CurHandle,m_timer->uIDEvent);
- if (isok)
- {
- DocVars.docData().mapTimers.erase(m_rb->resval.rint);
- delete m_timer;
- m_timer = NULL;
- acedRetT();
- }
- else
- {
- acutPrintf(_T("\n删除定时器时出错。"));
- return RTERROR;
- }
- return RSRSLT;
- }
|
评分
-
查看全部评分
|