본문 바로가기

C# Form

[C# Form] 아날로그 디지털시계

아날로그와 디지털 시계를 Form으로 만들어 보겠습니다.

Form을 사용할 때는 GDI+를 사용합니다.

디자인은 위와 같습니다. 메뉴 하나와 타이머 컨트롤을 사용하겠습니다. 또한 그림을 그리기 위한 Panel을 사용했습니다..

소스코드는 다음과 같습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Drawing;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Graphics g;
        private bool aClock_Flag = false;
        private Point Center;
        private double radius;
        private int hourHand;
        private int minHand;
        private int secHand;
        public Form1()
        {
            InitializeComponent();
            this.Text = "2016 FormClock";
            this.DoubleBuffered = true;
            g = panel1.CreateGraphics();
 
            panel1.Width = 200;
            panel1.Height = 200;
 
            panel1.BackColor = Color.White;
            this.BackColor = Color.White;
            
            aClockSetting();
            TimerSetting();
        }
 
        private void TimerSetting()
        {
            timer1.Interval = 1000;     // 1초에 한번씩
            timer1.Tick += Timer1_Tick;
            timer1.Start();
        }
 
        private void Timer1_Tick(object sender, EventArgs e)
        {
            DateTime c = DateTime.Now;
            Font font = new Font("맑은고딕"12, FontStyle.Bold);
            Font font1 = new Font("맑은고딕"32, FontStyle.Bold | FontStyle.Italic);
            Brush brush = Brushes.SkyBlue;
            Brush brush1 = Brushes.SteelBlue;
 
            panel1.Refresh();
 
            if (aClock_Flag == false)
            {
                string date = DateTime.Today.ToString("D");
                string time = string.Format("{0:D2}:{1:D2}:{2:D2}", c.Hour, c.Minute, c.Second);
                g.DrawString(date, font, brush, new Point(250));
                g.DrawString(time, font1, brush1, new Point(080));
            }
            else
            {
                DrawClockFace(); //시계판 그리기
                double radHr = (c.Hour % 12 + c.Minute / 60.0* 30 * Math.PI / 180;
                double radMin = (c.Minute + c.Second / 60.0* 6 * Math.PI / 180;
                double radSec = (c.Second) * 6 * Math.PI / 180;
                DrawHands(radHr, radMin, radSec); // 바늘 그리기
            }
        }
 
        private void DrawHands(double radHr, double radMin, double radSec)
        {
            // 시침
            DrawLine((int)(hourHand * Math.Sin(radHr)), (int)(-hourHand * Math.Cos(radHr)),
                00, Brushes.RoyalBlue, 8, Center.X, Center.Y);
            // 분침
            DrawLine((int)(minHand * Math.Sin(radMin)), (int)(-minHand * Math.Cos(radMin)),
                00, Brushes.SkyBlue, 6, Center.X, Center.Y);
            // 초침
            DrawLine((int)(secHand * Math.Sin(radSec)), (int)(-secHand * Math.Cos(radSec)),
                00, Brushes.OrangeRed, 3, Center.X, Center.Y);
 
            // 배꼽
            int coreSize = 20;
            Rectangle r = new Rectangle(Center.X - coreSize / 2, Center.Y - coreSize / 2, coreSize, coreSize);
            g.FillEllipse(Brushes.LightSteelBlue, r);
            Pen p = new Pen(Brushes.DarkBlue, 1);
            g.DrawEllipse(p, r);
        }
        private void DrawLine(int x1, int y1, int x2, int y2, Brush color, int thick, int Cx, int Cy)
        {
            Pen pen = new Pen(color, thick);
            pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
            pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
            g.DrawLine(pen, x1+Cx, y1+Cy, x2+Cx, y2+Cy);
        }
        private void DrawClockFace()
        {
            Pen pen = new Pen(Brushes.LightSteelBlue, 30);
            g.DrawEllipse(pen, Center.X - 85, Center.Y-85170170);
        }
 
        // 아날로그 클럭을 그리기 위한 기본 수치 설정
        private void aClockSetting()
        {
            Center = new Point(panel1.Width / 2, panel1.Height / 2);
            radius = panel1.Height / 2;
 
            hourHand = (int)(radius * 0.45);
            minHand = (int)(radius * 0.55);
            secHand = (int)(radius * 0.65);
        }
        private void 기지털ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            aClock_Flag = false;
        }
 
        private void 아날로그ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            aClock_Flag = true;
        }
 
        private void 종료ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
cs

실행화면은 다음과 같습니다.


Beeeye Dmu