MFC Project has its own wm_time timer, but the accuracy is low
The method of timeSetEvent the function of multimedia timer is also a general method in C language.
Multimedia timers need to be used for header files and static library files that have
#include <windows.h>
#pragma comment (lib, "Winmm.lib")
#include <MMSystem.h>
The left is a child dialog box, and the right is the parent dialog box
Control mode of the parent pair see previous article controlling the create Close and Clear Child dialog box with a dialog pointer
Under sub-CPP, create a global function to be called
Void PASCAL Fun (uint Wtimerid, uint Msg,dword dwuser,dword dwl,dword dw2) // PASCAL equates to callback
{
cmy0611adlg* fatherdlg= (cmy0611adlg*):: Theapp.getmainwnd (); //Bring dialog class functions into this global function This is the parent dialog pointer
cson* sondlg= (cson*) fatherdlg->m_son; //This is a child dialog pointer
if (Sondlg->control_ key==123) //control_key is a member variable of the child box used to control the stop timer
{
:: Timekillevent (Sondlg->m_timehandle);
}
CString str= (char*) Dwuser; //change the parameter type
sondlg->addstr (str); Span style= "White-space:pre" > //a Sub-box class function member function, no need to follow
}
Under sub-CPP
function of the Multimedia timer button being pressed
void Cson::onbnclickedbutton3 ()
{
Char buf[]= "SDFFADFA";
if (This->m_timehandle = timeSetEvent (1,fun, (dword_ptr) buf,time_periodic) = = 0)//Periodic call Timer Handler This line must understand the core
{
CString str= "error!\n";
This->setdlgitemtexta (IDC_STATIC2,STR);
}
Else
{
CString str;
Str. Format ("%d", this->m_timehandle);
Str= "ok!\ntimerid=" +str+ "\ n";
This->setdlgitemtexta (IDC_STATIC2,STR);
}
}
The sub-destructor is used to ensure that the timer is closed before the sub-frame is deleted.
Cson::~cson ()
{
:: Timekillevent (This->m_timehandle);
}
The parent box closes the timer indirectly by controlling the member variable of the sub-box (in fact, it can also be closed directly with the global function).
void Cmy0611adlg::onbnclickedbutton3 ()
{
this->m_son->control_key=123;
}
How to use MFC Multimedia timer function timesetevent