类与定时器问题
下面是我做的一个类,我在其中实现的是启动一个定时器,定时器每1毫秒将全局变量加1,
但有一个问题,如果我用CANMODAL类生成两个或两个以上的对象,它们都共用全局变量a,于是我就想把a封装到类中,这样不同对象对a的访问就互不干涉了,请问应如何实现,
或者将VOID CALLBACK TIMERPROC函数封装到类中也可实现我所说的功能,但我不知道如何将CALLBACK函数封装到类中,
或者哪位能告诉我在现在的程序中如何在CALLBACK函数中访问CCANMODAL中的变量.
// CanModal.cpp : implementation file
//
#include "stdafx.h"
#include "CanModal.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCanModal
long a;
VOID CALLBACK TimerProc (HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
{
a++;
if(a>10000000)
{
a=0;
}
}
CCanModal::CCanModal()
{
}
CCanModal::~CCanModal()
{
}
unsigned char CCanModal::TestCan(unsigned char x)
{
unsigned char retval;
unsigned char r;
if(x==0)
{
retval=0;
r=::KillTimer(NULL,RR);
}
else
{
retval=1;
RR=::SetTimer(NULL,1,1,TimerProc);
}
return retval;
}
long CCanModal::GetValue()
{
return a;
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CCanModal, CAsyncSocket)
//{{AFX_MSG_MAP(CCanModal)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CCanModal member functions
问题点数:20、回复次数:3Top
1 楼broccoli(-_-||)回复于 2006-07-03 09:55:41 得分 0
这个好像不好封装啊
就用OnTimer函数不行吗Top
2 楼yonglijiao(yonglijiao)回复于 2006-07-03 09:58:30 得分 0
可以呀,只要能实现怎么都行,但我不会呀,大侠能不能教教我,最好详细点Top
3 楼fisker0303(天塌了,地陷了,小花狗不见了.)回复于 2006-07-03 10:12:32 得分 0
你可以为每个实例都创建一个窗口,但这样做可能效率较低。
你也可以尝试使用timeSetEvent,他可以接受一个回调数据(例如this指针)。Top




