본문 바로가기
C# 언어

C# 으로 시계 만들기 (2)

by treeCoder 2021. 1. 11.

C# 으로 시계 만들기 - 2 를 소개한다.

다음 프로그램을 Clock2.cs 에 저장한다.

 

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace FormWithButton
{
       public class Form1 : Form
      {
            public Label label1;

            public Form1()
            {
                  ClientSize = new Size(200, 90); // Set the form size
                  this.ShowInTaskbar = false;
                  
                  label1 = new Label();
                  label1.Location = new Point(1, 1);
                  label1.Text = "";
                  label1.AutoSize = true;
                  label1.Font = new Font("Arial", 30, FontStyle.Regular);
                  this.Controls.Add(label1);
                                      
                  Timer timer = new System.Windows.Forms.Timer();
                  timer.Interval = 1000; // 1초
                  timer.Tick += new EventHandler(timer_Tick);
                  timer.Start();
            }

            void timer_Tick(object sender, EventArgs e)
            {     
                  // string ss = DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.DayOfWeek + "\n";
                  // ss = ss + "             " + DateTime.Now.Hour + ":" + DateTime.Now.Minute ;

                  // http://taeyo.net/columns/View.aspx?SEQ=78&PSEQ=8&IDX=1

                  // int hour = DateTime.Now.Hour;
                  // int min = DateTime.Now.Minute;
                  // int sec = DateTime.Now.Second;

                  // string ss = string.Format("{0}시 {1}분 {2}초"
                  //                  , hour, min, sec);
                  // label1.Text = ss;
                              
                  label1.Text = DateTime.Now.ToString("MM-dd-ddd\nHH:mm");
            }


            [STAThread]

            static void Main()
            {
                Application.EnableVisualStyles();
                Application.Run(new Form1());
            }
       }
}

// 컴파일 하는 방법은 아래와 같다.
// C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe -target:winexe Clock2.cs

 

저장한 코드를 다음과 같이 컴파일 한다. 여기서는 Visual studio 를 사용하지 않고 코맨드 창에서 Compile 하도록 하겠다.

 

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe -target:winexe Clock2.cs

 

컴파일한 후 실행 결과는 아래의 화면과 같다.

 

C# 시계실행 이미지

 

댓글