VS C++ MethodInvoker 倒数计时器
来源:互联网 发布:淘宝女士连衣裙 编辑:程序博客网 时间:2024/06/06 06:33
以下是一个使用了MethodInvoker的VS C++ 窗口程序,主要是为了展示MethodInvoker的简单应用。
1. 在VS中创建一个窗口应用程序,通过添加控件,形成类似下图所示的界面。控件的名称随个人喜好而定,只要保证程序能够正常编译即可。
2. 在表单程序的托管类中定义如下成员变量:
//========================================================================/* ******************************** ** *Member variables used* ** *********************************/private: Thread^ thrd;private: ThreadStart^ thrd_start;private: int t_hour,t_min,t_sec;private: bool isStop;private: bool isPause;private: System::Windows::Forms::Button^ pause;private: System::Windows::Forms::Button^ stop;//========================================================================
3. 在Form的构造函数中初始化这些成员。
//TODO: 在此处添加构造函数代码 // t_hour = 0; t_min = 0; t_sec = 0; isStop = false; isPause = false;4. 在Form类中定义如下成员函数:
//=====================================================================================///* ************************************************ ** *Function to convert user input string* *into integers.* ** *************************************************/private: void SetTime() { t_hour = Convert::ToInt32(m_hour -> Text); t_min = Convert::ToInt32(m_min -> Text); t_sec = Convert::ToInt32(m_sec -> Text); };//=====================================================================================///* ************************************************ ** *Function to decrement the total time in* *seconds by 1 in every second.* ** *************************************************/private: void CountDown() { int tol = TotalSec(); tol--; t_hour = tol/3600; t_min = (tol - t_hour*3600)/60; t_sec = tol-t_hour*3600-t_min*60; };//=====================================================================================///* ************************************************ ** *Function to compute the total time in* *seconds.* ** *************************************************/private: int TotalSec(){return (t_hour*3600+t_min*60+t_sec);};5. 定义计时线程函数:
/* ************************************************ ** *Thread function called when the timing* *thread starts.* ** *************************************************/private: void TimingThrd() { MethodInvoker^ mi = gcnew MethodInvoker(this,&Form1::InvokeFunc); int t = TotalSec(); while(t>0&&!isStop) { if(!isPause) { Sleep(1000); CountDown(); this -> BeginInvoke(mi); t--; } else { Sleep(10); } } };6. 程序另外一个重要部分就是Invoke调用的函数:
/* ************************************************ ** *Invoke function to change fields in the* *user interface.* ** *************************************************/private: void InvokeFunc() { m_hour -> Text = Convert::ToString(t_hour); m_min -> Text = Convert::ToString(t_min); m_sec -> Text = Convert::ToString(t_sec); };
7. 按钮控件的单击事件的实现如下:
//=====================================================================================///* ************************************************ ** *Start counting when "Start" button is* *clicked.* ** *************************************************/private: System::Void start_Click(System::Object^ sender, System::EventArgs^ e) { m_hour -> Text = s_hour -> Text; m_min -> Text = s_min -> Text; m_sec -> Text = s_sec -> Text; SetTime(); thrd_start = gcnew ThreadStart(this,&Form1::TimingThrd); thrd = gcnew Thread(thrd_start); thrd -> Start(); }//=====================================================================================///* **************** ** * Stop timer* ** *****************/private: System::Void stop_Click(System::Object^ sender, System::EventArgs^ e) {isStop = true; //thrd -> Abort(); };//=====================================================================================///* **************** ** * Pause timer* ** *****************/private: System::Void pause_Click(System::Object^ sender, System::EventArgs^ e) { if(!isPause) {isPause = true; } else { isPause = false; } };//=====================================================================================//
8. 编译运行。以倒数10秒为例,下面的组图为运行的结果。
0 0
- VS C++ MethodInvoker 倒数计时器
- CountDownTimer计时器倒数计时器
- MethodInvoker
- android CountDownTimer 倒数计时器
- MFC制作倒数计时器
- js 倒数计时器
- JS倒数计时器
- Android倒数计时器CountDownTimer
- JS倒数计时器
- js促销倒数计时器
- 11..6创建倒数计时器
- js实现倒数计时器功能
- 一个简单的倒数计时器
- C ++ 计时器
- jQuery css3圆形倒数秒计时器代码
- AsyncTask实时跟进进度(倒数计时器)
- MethodInvoker 委托
- c、c#---计时器
- Cf436B
- cocos2d-x真正的随机数
- 【CSDN开源夏令营】 基于Web的SSH客户端,实现跨平台的云服务管理(一)前言
- uva 1587 - Box
- python 十进制与二进制以及位运算
- VS C++ MethodInvoker 倒数计时器
- 2013 长沙邀请赛 ADEGH 题解
- 如何让平滑细分只平滑一个物体的一部分,而其他部分不受影响
- java 线程 原子类相关操作示例 thinking in java4 目录21.3.4
- Cf386B
- LeetCode -- sameTree
- C/C++内存分配
- 黑马程序员_反射
- Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils