⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mfctimer.shtml.htm

📁 Using Timers in MFC Applications在MFC应用程序中使用时间
💻 HTM
📖 第 1 页 / 共 2 页
字号:
  <P>the <B>ontimer</B> method is passed an unsigned integer (uint) to identify 
  the timer which is interrupting. the value of this timer may be used to stop 
  the associated timer. it can also be used in a multiple timer environment to 
  determine what type of processing must occur. </P></UL><U><B>
<P>example:</B></U> 
<UL>
  <P>assumptions: 
  <OL>
    <LI>the timers are started using the example in the section on <A 
    href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#install">installing 
    a timer</A>. 
    <LI>the timer values are placed sequentially into the array 
    <B><I>atimer</I></B>, as follows: 
    <UL><PRE>      atimer [0] = starttimer (200);  // a 200 msec timer
      atimer [1] = starttimer (500);  // a 500 msec timer
      </PRE></UL></LI></OL><FONT color=#990000><TT><PRE>  void ontimer (uint timerval)
  {
      int      index;

      /////////////////////////////////////////////////////////////////////////////
      //
      //      check timer number 0
      //
      ////////////////////////////////////////////////////////////////////////////

      if (timerval == atimer[0])
      {
          //
          //      process timer 0 message!
          //

          return;
      }

      ////////////////////////////////////////////////////////////////////////////
      //
      //      check timer number 1
      //
      ////////////////////////////////////////////////////////////////////////////

      if (timerval == atimer[1])
      {
          //
          //      process timer 1 message!
          //

          return;
      }

      ////////////////////////////////////////////////////////////////////////////
      //
      //      not timer number 0 or 1
      //
      ////////////////////////////////////////////////////////////////////////////

      if (!killtimer (timerval))
      {

      }

      cstring message;
      message.format("unkown timer interrupt: %u", timerval);
      messagebox (message,"ontimer",mb_ok|mb_systemmodal);
      
  } // end ontimer

</PRE></TT></FONT></UL>
<P><B><U>iconmover - timer application</U></B> </P>
<P align=center><IMG height=265 src="MFCTimer.shtml.files/404.htm" width=284 
tppabs="http://www.codeguru.com/misc/mfctimer.jpg"> </P>
<P><B>iconmover</B> is a sample application embodying a timer which signals the 
program at regular intervals. when the program is signaled, the icon being 
displayed is changed, leading to a crude animation of the icon. 
<UL><A 
  href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#programflow"><B><U>
  <P>program flow</U></B></A> <BR><A 
  href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#methodsvars"><B><U>methods 
  and variables</U></B></A> <BR>
  <UL><A 
    href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#methods"><B><U>
    <P>methods</U></B></A> <BR><A 
    href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#variables"><B><U>variables</U></B></A> 
    <BR></P></UL></UL>
<HR align=center width="50%">

<P><A name=programflow><B><U>program flow</U></B></A> </P>
<P>we start by adding a line to the dialogs' <B><I>oninitdialog</I></B> method 
to set the value of icontimer to null, indicating that the timer has not yet 
been assigned. </P>
<P>the dialog window is initialized containing a default icon and 3 buttons: 
<B>start</B>, <B>stop</B> and <B>exit</B>, corresponding to the <A 
href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#onstart"><B><I>onstart</I></B></A>, 
<A 
href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#onstop"><I><B>onstop</B></I></A> 
and <A 
href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#onok"><B><I>onok</I></B></A> 
methods, respectively. the <B>start</B> button starts the timer, if one is not 
already running, the <B>stop</B> button stops the timer, and the <B>exit</B> 
button stops the running timer, if one is running, and exits the dialog. </P>
<P>when the <B>start</B> button has been activated, a timer is started with the 
default timer interval, 500 msec., causing a timer message to be generated every 
1/2 second. </P>
<P>the <A 
href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#ontimer"><B><I>ontimer</I></B></A> 
method is invoked to process each timer generated message. the <B>iconstate</B> 
modulus 4, a number between 0 and 3, inclusive, is used as an index in a select 
statement to choose the icon to display next. once the icon is selected 
(<B>iconid</B>), <B>iconstate</B> is incremented for the next timer message. 
finally, the <A 
href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#setcurrenticon"><B><I>setcurrenticon</I></B></A> 
method is passed the value in <B>iconid</B> to display the indicated icon in the 
dialog. </P>
<P>the timer will run until the <B>stop</B> button is pressed. after stopping 
the timer, the <B><I>onstop</I></B> method passes the default icon setting, 0, 
to the <B><I>setcurrenticon</I></B> method. </P>
<P>the <B>exit</B> button is used to stop the timer, if it is running, and exit 
the dialog when you are through with the application. </P>
<HR align=center width="50%">

<P><A name=methodsvars><B><U>methods and variables</U></B></A> 
<UL><B>
  <P>iconmover</B> is a sample application embodying a timer which signals the 
  program at regular intervals. when the program is signaled, the icon being 
  displayed is changed, leading to a crude animation of the icon. </P>
  <P>the program implements the methods discussed elsewhere in this tutorial: 
  <UL><B><A 
    href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#ontimer">
    <P>ontimer</A> <BR><A 
    href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#starttimer">starttimer</A> 
    <BR><A 
    href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#stoptimer">stoptimer</A> 
    <BR></P></B></UL>
  <P>as well as several new methods: 
  <UL><B><A 
    href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#onok">
    <P>onok</A> <BR><A 
    href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#onstart">onstart</A> 
    <BR><A 
    href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#onstop">onstop</A> 
    <BR><A 
    href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#setcurrenticon">setcurrenticon</A> 
    <BR></B></P></UL>
  <P>and two public variables: 
  <UL><B><A 
    href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#iconstate">
    <P>iconstate</A> <BR><A 
    href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#icontimer">icontimer</A> 
    <BR></B></P></UL></UL>
<HR align=center width="50%">

<P><A name=methods><B><U>methods</U></B></A> </P>
<P><B>uint <A name=starttimer>starttimer</A> (uint timerduration) </B>
<UL>
  <LI>associates a timer event with the current process and sets its duration, 
  in msec. <BR>
  <LI>returns the system timer assigned to process the event, or 0 if an error 
  occurs. </LI></UL>
<HR align=center width="50%">

<P align=center><B>bool <A name=stoptimer>stoptimer</A> (uint timerval) </B></P>
<UL>
  <LI>stops system timer event <B>timerval</B>. <BR>
  <LI>returns true if successful. </LI></UL>
<HR align=center width="50%">

<P align=center><B>void <A name=ontimer>ontimer</A> (uint timerval) </B></P>
<UL>
  <LI>processes the system timer <B>timerval</B>. <BR>
  <LI>does not stop the timer. <BR>
  <LI>displays the icon associated with the value in <B>iconstate</B> modulus 4 
  (a number between 0 and 3, inclusive). </LI></UL>
<HR align=center width="50%">

<P align=center><B>void <A name=onok>onok</A> ( )</B> </P>
<UL>
  <LI>processes the <B>exit</B> button <BR>
  <LI>stops the timer, if one is active, and exits the dialog </LI></UL>
<HR align=center width="50%">

<P align=center><B>void <A name=onstart>onstart</A> ( )</B> </P>
<UL>
  <LI>processes the <B>start</B> button <BR>
  <LI>starts a system timer event and stores the value of the timer in 
  <B>icontimer</B> </LI></UL>
<HR align=center width="50%">

<P align=center><B>void <A name=onstop>onstop</A> ( )</B> </P>
<UL>
  <LI>processes the <B>stop</B> button <BR>
  <LI>stops the system timer event <B>icontimer</B> </LI></UL>
<HR align=center width="50%">

<P align=center><B>void <A name=setcurrenticon>setcurrenticon</A> (int 
iconid)</B> </P>
<UL>
  <LI>sets the current icon being displayed. </LI></UL>
<HR align=center width="50%">

<P><A name=variables><B><U>variables</U></B></A> 
<UL>
  <TABLE border=1>
    <TBODY>
    <TR>
      <TD width=100><B><A name=iconstate>iconstate</A></B> </TD>
      <TD>contains the value of the next icon to display on the dialog 
        windows. </TD></TR>
    <TR>
      <TD width=100><B><A name=icontimer>icontimer</A></B> </TD>
      <TD>contains the value of the system assigned timer. invalid when null. 
      </TD></TR></TBODY></TABLE></UL>
<P><!-- remember to update this --></P>
<P>last updated: 30 july 1998 <!--comments--></P></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -