16,472
社区成员
发帖
与我相关
我的任务
分享
class CTimerSampleDlg : public CDialog
{
…….
static void CALLBACK TimeProc(UINT uID,UINT uMsg,
DWORD dwUser,DWORD dw1,DWORD dw2); // 定时器回调函数
int TimerID; // 定时器ID
…….
};
void CTimerSampleDlg::OnButtonstarttimer()
{
TimerID = timeSetEvent(100,1,TimeProc,0,TIME_PERIODIC);
}
void CALLBACK CTimerSampleDlg::TimeProc(UINT uID,UINT uMsg, DWORD dwUser,DWORD dw1,DWORD dw2)
{
m_Cal=m_Cal+1;
}
void CTimerSampleDlg::OnTimer(UINT nIDEvent)
{
m_Edit2=m_Cal;
UpdateData(FALSE);
CDialog::OnTimer(nIDEvent);
}
void CTimersDlg::OnButtonBegin()
{
.
.
.
// Set resolution to the minimum supported by the system
TIMECAPS tc;
timeGetDevCaps(&tc, sizeof(TIMECAPS));
m_uResolution = min(max(tc.wPeriodMin, 0), tc.wPeriodMax);
timeBeginPeriod(resolution);
// create the timer
m_idEvent = timeSetEvent(
m_elTime,
resolution,
TimerFunction,
(DWORD)this,
TIME_PERIODIC);
}
void CTimersDlg::OnButtonStop()
{
// destroy the timer
timeKillEvent(m_idEvent);
// reset the timer
timeEndPeriod (m_uResolution);
}
void CTimersDlg::MMTimerHandler(UINT nIDEvent) // called every elTime milliseconds
{
// do what you want to do, but quickly
.
.
.
}
void CALLBACK TimerFunction(UINT wTimerID, UINT msg,
DWORD dwUser, DWORD dw1, DWORD dw2)
{
// This is used only to call MMTimerHandler
// Typically, this function is static member of CTimersDlg
CTimersDlg* obj = (CTimersDlg*) dwUser;
obj->MMTimerHandler(wTimerID);
}
看到没?TimerFunction是回调函数,在其中再调用对话框的MMTimerHandler,而不是把对话框类的静态成员函数设置为回调。[/quote]
回调函数里再调用对话框类的一个函数 和直接在回调函数里实现相应功能 有什么区别呢 比如说我想在对话框中显示一些东西 若直接在全局的回调函数里实现,则还需要传递对话框句柄什么的 现在用你这个方法是不是就不需要了呢?[/quote]
两者最大的区别在于,在对话框类的函数里,可以使用对话框类的成员变量,而在回调函数中,如果没有对话框类的指针,你是无法使用对话框类的成员变量。
[/quote]
嗯 是啊 我最近正在试图使用多媒体定时器替换原来的系统定时器ontimer()。 之前我就是简单的把所有的对话框类的成员变量都用对话框类的指针指一下,但是SetItemText()函数总是报错,之前ontimer()里这个函数里只有两个参数,我也不明白为什么,但是在多媒体定时器中编译时他就提示我参数不够,唉 那我用你得方法这个会得到解决么 是不是可以直接将ontimer里的代码复制进去就可以啦? 还有我不太明白你得MMTimerHandler(UINT nIDEvent)函数里的 nIDEvent这个参数有什么用呢? 怎么用呢[/quote]
你看我4楼的回复,照着做,先编译通过了,再考虑其他的。回调函数里先可以什么都不做,等编译通过,单步调试没问题后再尝试加其他代码。
void CTimersDlg::OnButtonBegin()
{
.
.
.
// Set resolution to the minimum supported by the system
TIMECAPS tc;
timeGetDevCaps(&tc, sizeof(TIMECAPS));
m_uResolution = min(max(tc.wPeriodMin, 0), tc.wPeriodMax);
timeBeginPeriod(resolution);
// create the timer
m_idEvent = timeSetEvent(
m_elTime,
resolution,
TimerFunction,
(DWORD)this,
TIME_PERIODIC);
}
void CTimersDlg::OnButtonStop()
{
// destroy the timer
timeKillEvent(m_idEvent);
// reset the timer
timeEndPeriod (m_uResolution);
}
void CTimersDlg::MMTimerHandler(UINT nIDEvent) // called every elTime milliseconds
{
// do what you want to do, but quickly
.
.
.
}
void CALLBACK TimerFunction(UINT wTimerID, UINT msg,
DWORD dwUser, DWORD dw1, DWORD dw2)
{
// This is used only to call MMTimerHandler
// Typically, this function is static member of CTimersDlg
CTimersDlg* obj = (CTimersDlg*) dwUser;
obj->MMTimerHandler(wTimerID);
}
看到没?TimerFunction是回调函数,在其中再调用对话框的MMTimerHandler,而不是把对话框类的静态成员函数设置为回调。[/quote]
回调函数里再调用对话框类的一个函数 和直接在回调函数里实现相应功能 有什么区别呢 比如说我想在对话框中显示一些东西 若直接在全局的回调函数里实现,则还需要传递对话框句柄什么的 现在用你这个方法是不是就不需要了呢?[/quote]
两者最大的区别在于,在对话框类的函数里,可以使用对话框类的成员变量,而在回调函数中,如果没有对话框类的指针,你是无法使用对话框类的成员变量。
[/quote]
嗯 是啊 我最近正在试图使用多媒体定时器替换原来的系统定时器ontimer()。 之前我就是简单的把所有的对话框类的成员变量都用对话框类的指针指一下,但是SetItemText()函数总是报错,之前ontimer()里这个函数里只有两个参数,我也不明白为什么,但是在多媒体定时器中编译时他就提示我参数不够,唉 那我用你得方法这个会得到解决么 是不是可以直接将ontimer里的代码复制进去就可以啦? 还有我不太明白你得MMTimerHandler(UINT nIDEvent)函数里的 nIDEvent这个参数有什么用呢? 怎么用呢
void CTimersDlg::OnButtonBegin()
{
.
.
.
// Set resolution to the minimum supported by the system
TIMECAPS tc;
timeGetDevCaps(&tc, sizeof(TIMECAPS));
m_uResolution = min(max(tc.wPeriodMin, 0), tc.wPeriodMax);
timeBeginPeriod(resolution);
// create the timer
m_idEvent = timeSetEvent(
m_elTime,
resolution,
TimerFunction,
(DWORD)this,
TIME_PERIODIC);
}
void CTimersDlg::OnButtonStop()
{
// destroy the timer
timeKillEvent(m_idEvent);
// reset the timer
timeEndPeriod (m_uResolution);
}
void CTimersDlg::MMTimerHandler(UINT nIDEvent) // called every elTime milliseconds
{
// do what you want to do, but quickly
.
.
.
}
void CALLBACK TimerFunction(UINT wTimerID, UINT msg,
DWORD dwUser, DWORD dw1, DWORD dw2)
{
// This is used only to call MMTimerHandler
// Typically, this function is static member of CTimersDlg
CTimersDlg* obj = (CTimersDlg*) dwUser;
obj->MMTimerHandler(wTimerID);
}
看到没?TimerFunction是回调函数,在其中再调用对话框的MMTimerHandler,而不是把对话框类的静态成员函数设置为回调。[/quote]
回调函数里再调用对话框类的一个函数 和直接在回调函数里实现相应功能 有什么区别呢 比如说我想在对话框中显示一些东西 若直接在全局的回调函数里实现,则还需要传递对话框句柄什么的 现在用你这个方法是不是就不需要了呢?[/quote]
两者最大的区别在于,在对话框类的函数里,可以使用对话框类的成员变量,而在回调函数中,如果没有对话框类的指针,你是无法使用对话框类的成员变量。
void CTimersDlg::OnButtonBegin()
{
.
.
.
// Set resolution to the minimum supported by the system
TIMECAPS tc;
timeGetDevCaps(&tc, sizeof(TIMECAPS));
m_uResolution = min(max(tc.wPeriodMin, 0), tc.wPeriodMax);
timeBeginPeriod(resolution);
// create the timer
m_idEvent = timeSetEvent(
m_elTime,
resolution,
TimerFunction,
(DWORD)this,
TIME_PERIODIC);
}
void CTimersDlg::OnButtonStop()
{
// destroy the timer
timeKillEvent(m_idEvent);
// reset the timer
timeEndPeriod (m_uResolution);
}
void CTimersDlg::MMTimerHandler(UINT nIDEvent) // called every elTime milliseconds
{
// do what you want to do, but quickly
.
.
.
}
void CALLBACK TimerFunction(UINT wTimerID, UINT msg,
DWORD dwUser, DWORD dw1, DWORD dw2)
{
// This is used only to call MMTimerHandler
// Typically, this function is static member of CTimersDlg
CTimersDlg* obj = (CTimersDlg*) dwUser;
obj->MMTimerHandler(wTimerID);
}
看到没?TimerFunction是回调函数,在其中再调用对话框的MMTimerHandler,而不是把对话框类的静态成员函数设置为回调。[/quote]
回调函数里再调用对话框类的一个函数 和直接在回调函数里实现相应功能 有什么区别呢 比如说我想在对话框中显示一些东西 若直接在全局的回调函数里实现,则还需要传递对话框句柄什么的 现在用你这个方法是不是就不需要了呢?
void CALLBACK TimerFunction(UINT wTimerID, UINT msg,
DWORD dwUser, DWORD dw1, DWORD dw2)
{
// This is used only to call MMTimerHandler
// Typically, this function is static member of CTimersDlg
CTimersDlg* obj = (CTimersDlg*) dwUser;
obj->MMTimerHandler(wTimerID);
}
你没看代码吗?
定时到了后,系统会自动调用TimerFunction,在这个函数中,可以得到对话框指针,然后调用对话框函数的MMTimerHandler。