How to use MFC Multimedia timer function timesetevent

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.