C# Timer 를 이용해서 간단한 시계를 만들어 보았다.
아래의 코드를 Clock.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(310, 50); // Set the form size
label1 = new Label();
label1.Location = new Point(1, 1);
label1.Text = DateTime.Now.ToLongTimeString();
label1.AutoSize = true;
label1.Font = new Font("Arial", 35, 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)
{
label1.Text = DateTime.Now.ToLongTimeString();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
}
저장한 코드를 다음과 같이 컴파일 한다. 여기서는 Visual studio 를 사용하지 않고 코맨드 창에서 Compile 하는 방법을 소개 한다.
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe -target:winexe Clock.cs
컴파일한 후 실행 결과는 아래의 화면과 같다.

'C# 언어' 카테고리의 다른 글
| C# 에서 List 추가 및 정렬하기 (0) | 2021.01.14 |
|---|---|
| C# 으로 시계 만들기 (2) (0) | 2021.01.11 |
| C# 윈도우즈 폼을 사용하여 Hello world 출력하기 (1) | 2021.01.01 |
| 커맨드 라인에서 C# 컴파일하기 (0) | 2021.01.01 |
| C#을 사용하여 계산기 만들기 (0) | 2020.11.19 |
댓글