Win10系列:VC++ 定时器

计时器机制俗称"心跳",表示以特定的频率持续触发特定事件和执行特定程序的机制。在开发Windows应用商店应用的过程中,可以使用定义在Windows::UI::Xaml命名空间中的DispatcherTimer类来创建计时器。DispatcherTimer类包含了如下的成员:

  • Tick事件,周期性触发的事件。
  • Start函数,用于启动计时器。
  • Stop函数,用于停止计时器。
  • Interval属性,设置触发Tick事件的时间周期,此属性值的类型为TimeSpan。

简单介绍了DispatcherTimer类之后,接下来模拟实现一个简易的计时器。在Visual Staudio 2012中新建一个Visual C++的Windows应用商店的空白应用程序项目,并命名为DispatcherTimerDemo,接着在MainPage.xaml文件的Grid元素中添加如下的代码,用于布局前台界面。

<StackPanel HorizontalAlignment="Center" Margin="50,300,0,0">

<TextBlock x:Name="ClockText" FontSize="24"></TextBlock>

<Grid>

<Grid.ColumnDefinitions>

<ColumnDefinition Width="Auto"></ColumnDefinition>

<ColumnDefinition Width="*"></ColumnDefinition>

</Grid.ColumnDefinitions>

<Button x:Name="Start" Click="StartClick" Content="开始" Grid.Column="0"></Button>

<Button x:Name="Stop" Click="StopClick" Content="停止" Grid.Column="1"></Button>

</Grid>

</StackPanel>

在上面的代码中,添加了一个TextBlock控件和两个按钮。将这个TextBlock控件命名为ClockText,用来显示计时器的计时。两个按钮分别为"开始"按钮和"停止"按钮,其中"开始"按钮用来启动计时器,"停止"按钮用来停止计时器。

布局了前台界面以后,接下来添加计时器的后台实现代码。打开MainPage.xaml.h头文件,添加如下的代码:

private:

    //声明DispatcherTimer类型变量timer

    Windows::UI::Xaml::DispatcherTimer^ timer;

    //声明TimeSpan类型变量timeSpan

    Windows::Foundation::TimeSpan timeSpan;

    //声明int32类型变量

    int32 highNum;

    //声明int32类型变量

    int32 lowNum;

在上面的代码中,使用private关键字声明了四个私有的成员变量,分别为timer、timeSpan、highNum和lowNum,其中timer是一个DispatcherTimer类型的变量,用来表示计时器,timeSpan为TimeSpan类型的变量,用来表示时间。highNum和lowNum都为int32类型的变量,分别代表计时器的十位数和个位数。

声明了上述的变量之后,接下来打开MainPage.xaml.cpp源文件,并在构造函数中添加如下的代码:

MainPage::MainPage()

{

    InitializeComponent();

    //创建DispatcherTimer类的对象

    timer=ref new DispatcherTimer();

    //Tick事件添加事件函数

    timer->Tick +=ref new EventHandler<Object^>(this,&DispatcherTimerDemo::MainPage::DispatcherTimerTick);

    // Duration属性记录的时间为1s

    timeSpan.Duration=10000000;

    //设置时间间隔

    timer->Interval=timeSpan;

    //highNum变量赋值0

    highNum=0;

    //lowNum变量赋值0

    lowNum=0;

}

在上面的代码中,初始化一个DispatcherTimer类的对象timer,并为timer对象的Tick事件添加事件处理函数DispatcherTimerTick,后面将介绍DispatcherTimerTick函数的具体实现代码。然后把timeSpan变量的Duration属性赋值为10000000,并将timeSpan变量赋值给timer对象的Interval属性,使timer对象的Tick事件每1秒触发一次。最后将highNum变量和lowNum变量分别赋值为0,用于表示计时器的起始时间。

在实现DispatcherTimerTick函数之前,首先需要在MainPage.xaml.h头文件中进行声明,代码如下所示:

public:

    //更新计时器计时

    void DispatcherTimerTick(Object^ sender, Object^ e);

在上述代码中,使用public关键字声明一个公有的DispatcherTimerTick函数,此函数用来更新计时器的计时,并将更新后的计时显示到前台界面中。

声明了DispatcherTimerTick函数以后,接下来在MainPage.xaml.cpp源文件中添加DispatcherTimerTick函数的实现代码,具体代码如下所示:

//更新计时器计时

void DispatcherTimerDemo::MainPage::DispatcherTimerTick(Object^ sender, Object^ e)

{

    //lowNum小于9时,lowNum1

    if(lowNum<9)

    {

        lowNum++;

    }

    else

    {

        //lowNum大于9时,将lowNum设为0

        lowNum=0;

        //highNum小于9时,highNum1

        if(highNum<9)

        {

            highNum++;

        }

        else

        {

            //highNum大于9时,将highNum设为0

            highNum=0;

        }

    }

    //将计时显示到TextBlock控件中

    ClockText->Text="开始计时:"+highNum+lowNum;

}

在上面的代码中,当lowNum变量的值小于9时,lowNum变量自增1。而当lowNum变量的值大于9时,将lowNum变量赋值为0,并设置highNum变量的值。同样,当highNum变量的值小于9时,highNum变量自增1。而当highNum变量大于9时,将highNum变量赋值为0。最后将highNum变量和lowNum变量赋值给名为"ClockText"的TextBlock控件的Text属性,用于将计时显示到前台界面中。

添加了DispatcherTimerTick函数的实现代码后,接下来为"开始"按钮添加单击事件处理函数StartClick。在MainPage.xaml.h头文件中添加如下的代码,用来声明StartClick函数。

public:

    //启动计时器

    void StartClick(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);

声明了StartClick函数之后,接下来在MainPage.xaml.cpp源文件中添加StartClick函数的实现代码,在此函数中调用timer对象的Start函数来启动计时器。具体代码如下所示:

//启动计时器

void DispatcherTimerDemo::MainPage::StartClick(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)

{

    timer->Start();

}

接着给"停止"按钮添加单击事件处理函数StopClick,在MainPage.xaml.h头文件中添加如下的代码,用来声明StopClick函数。

public:

    //停止计时器

    void StopClick(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);

声明了StopClick函数之后,接下来在MainPage.xaml.cpp源文件中添加StopClick函数的实现代码,在此函数中调用timer对象的Stop函数来停止计时器。具体代码如下所示:

//停止计时器

void DispatcherTimerDemo::MainPage::StopClick(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)

{

    timer->Stop();

}

运行DispatcherTimerDemo项目后,单击"开始"按钮启动计时器,显示如图20-1所示的计时器界面。

图20-1 计时器

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541676.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

dbms系统 rdbms_DBMS与传统文件系统之间的区别

dbms系统 rdbmsIntroduction 介绍 DBMS and Traditional file system have some advantages, disadvantages, applications, functions, features, components and uses. So, in this article, we will discuss these differences, advantages, disadvantages and many other …

android 百度地图api密钥,Android百度地图开发获取秘钥之SHA1

最近在做一个关于百度地图的开发。不过在正式开发之前还必须要在百度地图API官网里先申请秘钥&#xff0c;而在申请秘钥的过程中&#xff0c;就需要获取一个所谓的SHA1值。如上所示&#xff0c;但是由于不是正式开发&#xff0c;所以以上的发布版和开发版的SHA1可以先填写相同。…

单位矩阵的逆| 使用Python的线性代数

Prerequisites: 先决条件&#xff1a; Defining a Matrix 定义矩阵 Identity Matrix 身份矩阵 There are matrices whose inverse is the same as the matrices and one of those matrices is the identity matrix. 有些矩阵的逆与矩阵相同&#xff0c;并且这些矩阵之一是单位…

华为荣耀七能升级鸿蒙系统吗,华为鸿蒙系统来了,你知道哪些华为手机荣耀手机可以升级吗?...

从鸿蒙系统第一次开始登场&#xff0c;到现在慢慢有许多鸿蒙系统设备出现&#xff0c;手机市场的格局似乎又要升级变化了。科技树儿了解到&#xff0c;在某数码博主经过和相关人员的沟通核实之后&#xff0c;目前暂定的是搭载华为麒麟710芯片以上的机型&#xff0c;无论华为或荣…

day5-shutil模块

一、简述 我们在日常处理文件时&#xff0c;经常用到os模块&#xff0c;但是有的时候你会发现&#xff0c;像拷贝、删除、打包、压缩等文件操作&#xff0c;在os模块中没有对应的函数去操作&#xff0c;下面我们就来讲讲高级的 文件、文件夹、压缩包 处理模块&#xff1a;shuti…

matlab中now函数_now()方法以及JavaScript中的示例

matlab中now函数JavaScript now()方法 (JavaScript now() method) now() method is a Date class method, it is used to current time in milliseconds, it returns the total number of milliseconds since 01st January 1970, 00:00:00 UTC. now()方法是Date类的一种方法&am…

android 集成x5内核时 本地没有,腾讯浏览服务-接入文档

三、SDK集成步骤1. 第一步下载 SDK jar 包放到工程的libs目录下&#xff0c;将源码和XML里的系统包和类替换为SDK里的包和类&#xff0c;具体对应如下&#xff1a;系统内核SDK内核android.webkit.ConsoleMessagecom.tencent.smtt.export.external.interfaces.ConsoleMessageand…

java vector_Java Vector sureCapacity()方法与示例

java vector向量类别sureCapacity()方法 (Vector Class ensureCapacity() method) ensureCapacity() method is available in java.util package. sureCapacity()方法在java.util包中可用。 ensureCapacity() method is used to ensure the capacity of this Vector when requi…

Tcl与Design Compiler (十二)——综合后处理

本文如果有错&#xff0c;欢迎留言更正&#xff1b;此外&#xff0c;转载请标明出处 http://www.cnblogs.com/IClearner/ &#xff0c;作者&#xff1a;IC_learner 概述 前面也讲了一些综合后的需要进行的一些工作&#xff0c;这里就集中讲一下DC完成综合了&#xff0c;产生了…

Java短类的compareTo()方法和示例

简短的类compareTo()方法 (Short class compareTo() method) compareTo() method is available in java.lang package. compareTo()方法在java.lang包中可用。 compareTo() method is used to check equality or inequality for this Short object against the given Short obj…

四则运算网页版

一.设计思想&#xff1a; 1&#xff09;写出一个菜单界面&#xff0c;有两个选项一个是分数&#xff0c;一个是整数。 2&#xff09;而这两个标签后面则是转向其更详细的菜单&#xff0c;题目数量&#xff0c;有无括号&#xff0c;运算的项数等等详细功能&#xff0c;再点击这两…

Java RandomAccessFile seek()方法与示例

RandomAccessFile类seek()方法 (RandomAccessFile Class seek() method) seek() method is available in java.io package. seek()方法在java.io包中可用。 seek() method is used to sets the file pointer position calculated from the starting of this file at which the …

Javascript开发技巧(JS中的变量、运算符、分支结构、循环结构)

一、Js简介和入门 继续跟进JS开发的相关教程。 <!-- [使用JS的三种方式] 1、HTML标签中内嵌JS&#xff08;不提倡使用&#xff09;&#xff1a; 示例&#xff1a;<button οnclick"javascript:alert(你真点啊&#xff01;)">有本事点我呀&#xff01;&#…

android 颜色范围,Android系统颜色的适用范围

###All Clickable Views:ripple effect (Lollipop only) — “colorControlHighlight”###Status Bar:background (Lollipop only) – “colorPrimaryDark”###Navigation Bar:background (Lollipop only) – “android:navigationBarColor”###EditText:underline (unfocused)…

bytevalue_Java Short类byteValue()方法及示例

bytevalue短类byteValue()方法 (Short class byteValue() method) byteValue() method is available in java.lang package. byteValue()方法在java.lang包中可用。 byteValue() method is used to return the value denoted by this Short object converted to type byte (by …

分布式交换机配置备份和还原

1.备份和还原vSphere Distributed Switch配置 1.1导出 vSphere Distributed Switch 配置 可以将 vSphere Distributed Switch 和分布式端口组配置导出到某一文件。该文件保留有效的网络配置&#xff0c;使这些配置能够传输至其他环境。 步骤&#xff1a; 1) 在 vSphere Web Cli…

html自动执行函数,JS 自执行函数原理及用法

js自执行函数&#xff0c;听到这个名字&#xff0c;首先会联想到函数。接下来&#xff0c;我来定义一个函数&#xff1a;function aaa(a,b){return sum a b}定义了一个名为aaa的函数&#xff0c;在里面可以计算两个数的和。如果想执行它&#xff0c;就必须得调用它&#xff0…

java reverse_Java Integer类reverse()方法与示例

java reverse整数类reverse()方法 (Integer class reverse() method) reverse() method is available in java.lang package. reverse()方法在java.lang包中可用。 reverse() method is used to returns the value generated by reversing the order of bits in binary 2s comp…

华为鸿蒙系统好在哪,华为鸿蒙2.0可以替代安卓吗,华为鸿蒙2.0优势在哪

在华为开发者大会上&#xff0c;华为消费业务CEO 余承东&#xff0c;正式发布鸿蒙OS2.0&#xff0c;并宣布华为鸿蒙OS将全面启用全场景生态&#xff0c;并将于2020年12月发布手机版。余承东还表示&#xff0c;明年&#xff0c;华为的智能手机将全面升级&#xff0c;以支持鸿蒙操…

Java GregorianCalendar add()方法与示例

GregorianCalendar类的add()方法 (GregorianCalendar Class add() method) add() method is available in java.util package. add()方法在java.util包中可用。 add() method is used to add the given quantity to the specified GregorianCalendar field (fi). add()方法用于…