C++ Tutorial: Monitoring System Time C++/CLI

   Hi. It is Bright777 and today we are going to do a program to monitor system time. In my program I will use SYSTEMTIME structure and Timer Control from visual C++. All work I have done in Microsoft Visual Studio 2012. Preparation    First of all, you need to create a new project of Windows Forms Application Type. After that you should place controls like in the image below or something else. For showing system time I use standard Label component. Also you need to add two buttons to your form and a Timer Control. Adding of Timer Control is very simple – just drag it onto your form and under form you will see it. Capture Settings So, here is the next step. You should add event on Button click for every button. For Start Timer Button
  1. private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
  2.                                  timer1->Enabled=true;
  3.                          }
For Disable Timer Button
  1. private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
  2.                           timer1->Enabled=false;
  3.                  }
   Then you set the events for buttons toy should change some settings in Timer Control. In properties set Enables property to False, Interval property set to 1000 (this is your interval in milliseconds). And that’s all in property tab, so go to the event tab and double click near Tick event. It will create function, which will be run at every tick of your timer. Place next code in this function:
  1. private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
  2.  
  3.     SYSTEMTIME systime;
  4.     GetSystemTime(&systime);
  5.     label1->Text=(systime.wHour).ToString();
  6.     label2->Text=(systime.wMinute).ToString();
  7.     label3->Text=(systime.wSecond).ToString();
  8. }
   Here you can see SYSTEMTIME structure, which has different values, such as time in hours, minutes, seconds and date in moths, days, weeks and other things. We will use only time. Also don't forget to include "winbase.h" to your project. Output Capture Thanks, for reading. Any questions are welcome. See you later.

Add new comment