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

📄 mfctimer.shtml.htm

📁 Using Timers in MFC Applications在MFC应用程序中使用时间
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0056)http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm -->
<HTML><HEAD><TITLE>mfc programmer's sourcebook : miscellaneous topics</TITLE>
<META content="text/html; charset=iso-8859-1" http-equiv=content-type>
<META content="zafir anjum" name=author>
<META content="source codes for misc topics" name=description>
<META content="source code misc" name=keywords>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR></HEAD>
<BODY background=MFCTimer.shtml.files/di2001.jpg bgColor=#ffffff 
tppabs="http://www.codeguru.com/fancyhome/back.gif"><!-- article title -->
<H3 align=center><FONT color=#a0a099>using timers in mfc applications 
</FONT></H3>
<HR align=center>
<!-- author and contact details -->
<P>this article was contributed by <A 
href="mailto:jay@earthwalkdesigns.com">jaywheeler</A>. <!-- sample image and source code/demo project --></P>
<P><A href="http://go1.163.com/~81000/source/misc/mfctimer.zip" 
tppabs="http://www.codeguru.com/misc/mfctimer.zip">download source code and 
example</A> </P>
<P><BR><!-- the article... --></P>
<P><B><U>timer events in mfc applications</U></B> </P>
<P>event timers are always handy to have around and useful in nearly every 
project. when a timer is readily available, such as the timer control in visual 
basic, you find all kinds of reasons to use one in an application. the lack of a 
timer object in mfc is, in my opinion, a serious oversight. </P>
<P>luckily, microsoft windows nt/95 systems provide a number of timers for use 
by application programs. this tutorial provides information on how to install 
timers in your mfc application, and how to start, process and stop the timers. 
</P>
<P>in the final section of the tutorial, we develope a simple timer project 
which is capable of rough animation of an icon in the dialog window. this 
project is useful in any compute or i/o bound application, and illustrates the 
techniques involved with installing and using a system timer. </P>
<P>these steps were developed and tested on a windowsnt 4.0 workstation and 
windows95 using visualc++ 4.2 with microsoft foundation classes. </P>
<P><A name=install><B><U>installing a timer</U></B></A> 
<OL type=1>
  <LI>in the header file of the dialog using the timer, 
  <OL type=a>
    <LI>add a message number for each timer needed: 
    <UL>
      <TABLE border=0>
        <TBODY>
        <TR>
          <TD width=100>#define </TD>
          <TD width=150>idt_timer_0 </TD>
          <TD>wm_user + 200 </TD></TR>
        <TR>
          <TD width=100>#define </TD>
          <TD width=150>idt_timer_1 </TD>
          <TD>idt_timer_0 + 1 </TD></TR></TBODY></TABLE></UL>
    <LI>add the timer interrupt handler 
    <UL><B>
      <P>ontimer (uint timerval)</B> </P></UL>
    <P>to the <I>generated message map</I>: 
    <OL type=a>
      <LI>locate the section under <B>implementation</B> where the 
      <B>afx_msg</B> message map is declared (usually begins with the line 
      <I>oninitdialog()</I>); 
      <LI>add a line just after the last line in the message map (just before 
      the "//}}afx_msg"): 
      <UL>
        <TABLE border=0>
          <TBODY>
          <TR>
            <TD width=80><B>afx_msg</B> </TD>
            <TD width=80><B>void</B> </TD>
            <TD><B>ontimer (uint timerval);</B> </TD></TR></TBODY></TABLE></UL>
      <P>this should be the <B>last</B> entry in the message map. 
    </P></LI></OL></LI></OL>
  <LI>in the dialog implementation (.cpp) file, 
  <OL type=a>
    <LI>locate the dialog class start 
    <LI>find the line starting "begin_message_map" 
    <LI>after the last entry in the message map (before the line 
    "//}}afx_msg_map"), add 
    <UL><B>
      <P>on_wm_timer ( )</B> </P></UL></LI></OL></LI></OL>
<P><B><U>starting the timer</U></B> </P>
<P>to start the timer, you must issue a <B><I>settimer</I></B> command: 
<UL><I>
  <P>t-number</I><B> = settimer (</B><I>t-message</I><B>, 
  </B><I>t-interval</I><B>, null)</B> </P></UL>
<P><I><B>where</B></I>: 
<UL>
  <TABLE>
    <TBODY>
    <TR>
      <TD vAlign=top><I>t-message</I> </TD>
      <TD>the message number assigned for processing the timer request. 
        <BR>this is referred to as <B>idt_timer_0</B> in the <A 
        href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#install">installation</A> 
        instructions. </TD></TR>
    <TR>
      <TD vAlign=top><I>t-interval</I> </TD>
      <TD>the timer duration, in msec. </TD></TR>
    <TR>
      <TD vAlign=top><I>t-number</I> </TD>
      <TD>the system timer number assigned to this event, or 0 if no timers 
        available. </TD></TR></TBODY></TABLE></UL>
<P><B>for example:</B> 
<UL><FONT color=#990000><TT><PRE>
    uint  starttimer (uint timerduration)
    {
        uint    timerval;

        timerval = settimer (idt_timer_0, timerduration, null);
        if (timerval == 0)
        {
                messagebox ("unable to obtain timer",
		      "idt_timer_0",
		      mb_ok|mb_systemmodal);
        }

        return timerval;

    }// end starttimer

  </PRE></TT></FONT>
  <P>to start the timer, pass it the value of the timer duration, in 
  milliseconds (msec). if the returned value is non-zero, the timer has been 
  initialized. </P>
  <P><B>note:</B> you must save the assigned timer value in order to stop the 
  timer, and also to know which timer to process in a multi-timer application. 
  </P></UL>
<P><A name=stopping><B><U>stopping the timer</U></B></A> </P>
<P>to stop the timer, issue the <I><B>killtimer</B></I> command: 
<UL><I>
  <P>t-result</I><B> = killtimer (</B><I>t-number</I><B>);</B> </P></UL>
<P><B><I>where:</I></B> 
<UL>
  <TABLE>
    <TBODY>
    <TR>
      <TD><I>t-number</I> </TD>
      <TD>the system timer number from either the settimer or ontimer methods. 
      </TD></TR>
    <TR>
      <TD><I>t-result</I> </TD>
      <TD>the boolean result of the operation, true indicating success. 
    </TD></TR></TBODY></TABLE></UL>
<P><B>for example:</B> 
<UL><FONT color=#990000><TT><PRE>      bool stoptimer (uint timerval)
      {

          //
          //    stop the timer
          //
          if (!killtimer (timerval))
          {
              return false;
          }

          //
          //    place clean-up code following this point.
          //


          return true;

      } // end stoptimer

  </PRE></TT></FONT></UL>
<P><U><B>processing timer events - the <I>ontimer</I> method</B></U> 
<UL>
  <P>after the timer has been started, the <B>ontimer</B> event will be called 
  each time the timer counts down to zero (reaches <I>terminal count</I>) from 
  the requested value. this event is <I>asynchronous</I> to the timer - a 
  message is placed in the message queue of the calling process and the timer is 
  automatically restarted. </P>
  <P>the timer can be stopped (refer to <A 
  href="http://go1.163.com/~81000/source/misc/MFCTimer.shtml.htm#stopping">stopping 
  the timer</A>) upon entry to the <B>ontimer</B> routine, or left to run if the 
  event will <B>not</B> occur again before processing the <B>ontimer</B> routine 
  has completed. </P>
  <P>the <B>ontimer</B> method will be executed each time the timer reaches 
  <I>terminal count</I>. the code in this method should be short and concise; it 
  should be code which <B>must</B> be executed each time the timer reaches its 
  <I>terminal count</I>, such as changing the view of an animated drawing or 
  icon, or setting a flag to process latter, or starting a thread to handle the 
  more complex functions. </P>
  <P><B><U>example:</U></B> </P><FONT color=#990000><TT><PRE>
    void ontimer (uint timerval)
    {
        //////////////////////////////////////////////
        //
        //    stop the timer
        //
        //////////////////////////////////////////////

        if (!killtimer (timerval))
        {

        }

        //////////////////////////////////////////////
        //
        //    process the event
        //
        //////////////////////////////////////////////


        //////////////////////////////////////////////
        //
        //    restart the timer, if needed, before exiting
        //
        //////////////////////////////////////////////


    }
  </PRE></TT></FONT></UL>
<P><B><U>multiple timers</U></B> 
<UL>

⌨️ 快捷键说明

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