📄 ch04.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<SCRIPT LANGUAGE="JavaScript">
<!--
function popUp(pPage) {
var fullURL = document.location;
var textURL = fullURL.toString();
var URLlen = textURL.length;
var lenMinusPage = textURL.lastIndexOf("/");
lenMinusPage += 1;
var fullPath = textURL.substring(0,lenMinusPage);
popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
figDoc= popUpWin.document;
zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
zhtm += '</head>';
zhtm += '<BODY bgcolor="#FFFFFF">
<!-- Spidersoft WebZIP Ad Banner Insert -->
<TABLE width=100% border="0" cellpadding="0" cellspacing="0">
<TR>
<TD>
<ILAYER id=ad1 visibility=hidden height=60></ILAYER>
<NOLAYER>
<IFRAME SRC="http://www.spidersoft.com/ads/bwz468_60.htm" width="100%" height="60" marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no></IFRAME>
</NOLAYER>
</TD>
</TR>
</TABLE>
<!-- End of Spidersoft WebZIP Ad Banner Insert-->
';
zhtm += '<IMG SRC="' + fullPath + pPage + '">';
zhtm += '<P><B>' + pPage + '</B>';
zhtm += '
<layer src="http://www.spidersoft.com/ads/bwz468_60.htm" visibility=hidden id=a1 width=600 onload="moveToAbsolute(ad1.pageX,ad1.pageY); a1.clip.height=60;visibility='show';"></layer>
</BODY></HTML>';
window.popUpWin.document.write(zhtm);
window.popUpWin.document.close();
// Johnny Jackson 4/28/98
}
//-->
</SCRIPT>
<link rel="stylesheet" href="../../../../includes/stylesheets/ebooks.css">
<META NAME="GENERATOR" Content="Symantec Visual Page Mac 1.1.1">
<TITLE>Teach Yourself Visual C++ 6 in 21 Days -- Ch 4 -- Working with Timers</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<H1 ALIGN="CENTER"><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM"
BORDER="0"><BR>
Teach Yourself Visual C++ 6 in 21 Days</H1>
<CENTER>
<P><A HREF="../ch03/ch03.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch05/ch05.htm"><IMG
SRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A>
<HR>
</CENTER>
<H1 ALIGN="CENTER">- 4 -<BR>
Working with Timers</H1>
<H1></H1>
<UL>
<LI><A HREF="#Heading1">Understanding Windows Timers</A>
<LI><A HREF="#Heading2">Placing a Clock on Your Application</A>
<UL>
<LI><A HREF="#Heading3">Creating the Project and Application</A>
<LI><A HREF="#Heading4">Adding the Timer IDs</A>
<LI><A HREF="#Heading5">Starting the Clock Timer</A>
<LI><A HREF="#Heading6">Handling the Clock Timer Event</A>
</UL>
<LI><A HREF="#Heading7">Adding a Second Timer to Your Application</A>
<UL>
<LI><A HREF="#Heading8">Adding the Application Variables</A>
<LI><A HREF="#Heading9">Starting and Stopping the Counting Timer</A>
<LI><A HREF="#Heading10">Enabling the Stop Button</A>
</UL>
<LI><A HREF="#Heading11">Summary</A>
<LI><A HREF="#Heading12">Q&A</A>
<LI><A HREF="#Heading13">Workshop</A>
<UL>
<LI><A HREF="#Heading14">Quiz</A>
<LI><A HREF="#Heading15">Exercise</A>
</UL>
</UL>
<P>
<HR SIZE="4">
<BR>
You may often find yourself building an application that needs to perform a specific
action on a regular basis. The task can be something simple such as displaying the
current time in the status bar every second or writing a recovery file every five
minutes. Both of these actions are regularly performed by several applications that
you probably use on a daily basis. Other actions that you might need to perform include
checking specific resources on a regular basis, as a resource monitor or performance
monitor does. These examples are just a few of the situations where you want to take
advantage of the availability of timers in the Windows operating system.</P>
<P>Today you are going to learn</P>
<P>
<UL>
<LI>How to control and use timers in your Visual C++ applications.
<P>
<LI>How to set multiple timers, each with a different recurrence interval.
<P>
<LI>How to know which timer has triggered.
<P>
<LI>How you can incorporate this important resource into all your Visual C++ applications.
</UL>
<H2><A NAME="Heading1"></A>Understanding Windows Timers</H2>
<P>Windows timers are mechanisms that let you set one or more timers to be triggered
at a specific number of milliseconds. If you set a timer to be triggered at a 1,000
millisecond interval, it triggers every second. When a timer triggers, it sends a
WM_TIMER message to your application. You can use the Class Wizard to add a function
to your application to handle this timer message.</P>
<P>Timer events are placed only in the application event queue if that queue is empty
and the application is idle. Windows does not place timer event messages in the application
event queue if the application is already busy. If your application has been busy
and has missed several timer event messages, Windows places only a single timer message
in the event queue. Windows does not send your application all the timer event messages
that occurred while your application was busy. It doesn't matter how many timer messages
your application may have missed; Windows still places only a single timer message
in your queue.</P>
<P>When you start or stop a timer, you specify a timer ID, which can be any integer
value. Your application uses this timer ID to determine which timer event has triggered,
as well as to start and stop timers. You'll get a better idea of how this process
works as you build your application for today.</P>
<P>
<H2><A NAME="Heading2"></A>Placing a Clock on Your Application</H2>
<P>In the application that you will build today, you will use two timers. The first
timer maintains a clock on the window. This timer is always running while the application
is running. The second timer is configurable to trigger at whatever interval the
user specifies in the dialog. The user can start and stop this timer at will. Let's
get started.</P>
<P>
<H3><A NAME="Heading3"></A>Creating the Project and Application</H3>
<P>You will build today's sample application in three phases. In the first phase,
you will add all the controls necessary for the entire application. In the second
phase, you will add the first of the two timers. This first timer will control the
clock on the application dialog. In the third phase, you will add the second timer,
which the user can tune, start, and stop as desired.</P>
<P>To create today's application, follow these steps:</P>
<P>
<DL>
<DT></DT>
<DD><B>1. </B>Create a new project, named <B>Timers</B>, using the same AppWizard
settings that you've used for the past three days. Specify the application title
as <B>Timers</B>.
<P>
<DT></DT>
<DD><B>2. </B>Lay out the dialog window as shown in Figure 4.1, using the control
properties in Table 4.1. Remember that when you place a control on the window, you
can right-click the mouse to open the control's properties from the pop-up menu.
<P>
</DL>
<P><A HREF="javascript
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -