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