C++ WinForm Example

一个用C++.Net写的闹钟程序:

#pragma once

namespace cppWinForm {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Summary for Form1
    ///
    /// WARNING: If you change the name of this class, you will need to change the
    ///          ‘Resource File Name’ property for the managed resource compiler tool
    ///          associated with all .resx files this class depends on.  Otherwise,
    ///          the designers will not be able to interact properly with localized
    ///          resources associated with this form.
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Label^  label1;
    protected:
    private: System::Windows::Forms::Button^  button1;
    private: System::Windows::Forms::Timer^  timer1;
    private: System::Windows::Forms::TextBox^  textBox1;
    private: System::Windows::Forms::Button^  button2;
    private: System::Windows::Forms::Label^  label2;
    private: System::Windows::Forms::Label^  label3;
    private: System::Windows::Forms::Timer^  timer2;
    private: System::Windows::Forms::Button^  button3;
    private: System::ComponentModel::IContainer^  components;

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support – do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->components = (gcnew System::ComponentModel::Container());
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
            this->textBox1 = (gcnew System::Windows::Forms::TextBox());
            this->button2 = (gcnew System::Windows::Forms::Button());
            this->label2 = (gcnew System::Windows::Forms::Label());
            this->label3 = (gcnew System::Windows::Forms::Label());
            this->timer2 = (gcnew System::Windows::Forms::Timer(this->components));
            this->button3 = (gcnew System::Windows::Forms::Button());
            this->SuspendLayout();
            //
            // label1
            //
            this->label1->AutoSize = true;
            this->label1->Location = System::Drawing::Point(114, 66);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(0, 12);
            this->label1->TabIndex = 0;
            //
            // button1
            //
            this->button1->Location = System::Drawing::Point(49, 189);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(75, 23);
            this->button1->TabIndex = 1;
            this->button1->Text = L"Set Alarm";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
            //
            // timer1
            //
            this->timer1->Enabled = true;
            this->timer1->Interval = 1000;
            this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
            //
            // textBox1
            //
            this->textBox1->Location = System::Drawing::Point(95, 129);
            this->textBox1->Name = L"textBox1";
            this->textBox1->Size = System::Drawing::Size(87, 21);
            this->textBox1->TabIndex = 2;
            //
            // button2
            //
            this->button2->Location = System::Drawing::Point(153, 189);
            this->button2->Name = L"button2";
            this->button2->Size = System::Drawing::Size(75, 23);
            this->button2->TabIndex = 3;
            this->button2->Text = L"Cancel";
            this->button2->UseVisualStyleBackColor = true;
            this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
            //
            // label2
            //
            this->label2->AutoSize = true;
            this->label2->Location = System::Drawing::Point(93, 104);
            this->label2->Name = L"label2";
            this->label2->Size = System::Drawing::Size(89, 12);
            this->label2->TabIndex = 4;
            this->label2->Text = L"Alarm Time Is?";
            //
            // label3
            //
            this->label3->AutoSize = true;
            this->label3->Location = System::Drawing::Point(99, 39);
            this->label3->Name = L"label3";
            this->label3->Size = System::Drawing::Size(83, 12);
            this->label3->TabIndex = 5;
            this->label3->Text = L"Current Time:";
            //
            // timer2
            //
            this->timer2->Interval = 1000;
            this->timer2->Tick += gcnew System::EventHandler(this, &Form1::timer2_Tick);
            //
            // button3
            //
            this->button3->Location = System::Drawing::Point(210, 39);
            this->button3->Name = L"button3";
            this->button3->Size = System::Drawing::Size(18, 21);
            this->button3->TabIndex = 6;
            this->button3->Text = L"X";
            this->button3->UseVisualStyleBackColor = true;
            this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
            //
            // Form1
            //
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(292, 273);
            this->Controls->Add(this->button3);
            this->Controls->Add(this->label3);
            this->Controls->Add(this->label2);
            this->Controls->Add(this->button2);
            this->Controls->Add(this->textBox1);
            this->Controls->Add(this->button1);
            this->Controls->Add(this->label1);
            this->Name = L"Form1";
            this->Text = L"Alarm";
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion
   

    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

                 //设置闹钟时间的按钮

                 timer2->Enabled = true;
                 textBox1->Enabled = false;
             }
    private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
                 //令label1显示当前时间

                 DateTime dt = DateTime::Now;
                 int s = dt.Second; //显示秒
                 if (s<10)
                    label1->Text = dt.ToShortTimeString()+":0"+s.ToString();
                 else
                    label1->Text = dt.ToShortTimeString()+":"+s.ToString();
             }
    private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
                 //取消闹钟

                 textBox1->Enabled = true;
                 timer2->Enabled = false;
             }
    private: System::Void timer2_Tick(System::Object^  sender, System::EventArgs^  e) {
                 //当前时间=闹钟时间,弹出提醒

                 if (label1->Text==textBox1->Text)
                 {
                     System::Windows::Forms::MessageBox::Show("Time up!");
                     timer2->Enabled = false;
                     textBox1->Text = "";
                     textBox1->Enabled = true;
                 }
         }
private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
                //关闭界面

                Close();
         }
};
}

This entry was posted in Uncategorized. Bookmark the permalink.

1 Response to C++ WinForm Example

  1. 扬威·十年 says:

    太感谢了!!!! 谢谢谢谢谢谢谢 幸好看了你的示例 不然还在为WForm的语法格式烦心呢 第一次接触 呵呵呵 再次感谢

Leave a comment