본문 바로가기

C#

6주차-아날로그 시계 만들기(1)

모든 코드 ↓

더보기
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _022_dClock
{
    public partial class Form1 : Form
    {
        //필드(멤버변수)
        //private 생략되어있음
        Graphics g;//다른함수에도 사용하게
        bool aClockFlag = true;//아날로그 시계
        Point center;
        int radius;
        int hourHand; //시침의 길이
        int minHand; //분침길이
        int secHand; //시침
        //const는 상수라 바꿀수 없는값 바꾸려하면 오류
        const int clientsSize = 300; //client의 크기
        const int clockSize = 200; //시계의 크기

        Font fDate; //날짜 폰트
        Font fTime; //시간 폰트
        Brush bDate;
        Brush bTime;

        Point panelCenter;
        public Form1()
        {
            InitializeComponent();
            this.ClientSize = new Size(clientsSize, clockSize + menuStrip1.Height);
            this.Text = "My Form Clock";

            //pancel 지정
            panel1.BackColor = Color.WhiteSmoke;
            panel1.Width = clockSize + 1;
            panel1.Height = clockSize + 1;
            panel1.Location = new Point(clientsSize / 2-clockSize / 2, clientsSize / 2 - clockSize / 2 + menuStrip1.Height);

            g = panel1.CreateGraphics(); //pancel에서 그려주는 그래픽 객체
            aClockSetting();
            dClockSetting();
            timerSetting();
        }
        private void aClockSetting()
        {//아날로그 시계 세팅
            center = new Point(clientsSize / 2, clockSize / 2);
            radius = clockSize / 2;
            hourHand = (int)(radius * 0.45);
            minHand = (int)(radius * 0.55);
            secHand = (int)(radius * 0.65);
        }
        private void dClockSetting()
        {//디지털 시계 세팅
            fDate = new Font("맑은 고딕",12,FontStyle.Bold);
            fTime = new Font("맑은 고딕", 32, FontStyle.Bold|FontStyle.Italic);
            bDate = Brushes.SkyBlue;
            bTime = Brushes.Blue;
        }
        private void timerSetting()
        {//시계설정
            Timer t = new Timer();

            t.Enabled = true;
            t.Interval = 1000;
            t.Tick += T_tick;
        }
        private void T_tick(object sender, EventArgs e)
        {
            DateTime c = DateTime.Now;
            panel1.Refresh();
            if(aClockFlag == true){//아날로그라면?
                DrawClockFace();

                double radHr = (c.Hour % 12 + c.Minute / 60.0) * 30 * Math.PI / 180;
                double radMin = (c.Minute + c.Second / 60.0) * 60.0 * 6 * Math.PI / 180;
                double radSec = c.Second * 6 * Math.PI / 180;

                DrawHands(radHr, radMin, radSec);
            }
            else {//디지털이라면?
                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, fDate, bDate, new Point(10, 70));
                g.DrawString(time, fDate, bDate, new Point(10, 100));
            }
        }
        private void DrawClockFace()
        {
            const int penWidth = 30;

            Pen p = new Pen(Brushes.LightSteelBlue,penWidth);
            g.DrawEllipse(p, penWidth / 2, penWidth / 2, clockSize - penWidth, clockSize - penWidth);
        }
        private void DrawHands(double radHr, double radMin, double radSec)
        {
            
        }
    }
}

1. 디자인 설정

도구상자에서 MenuStrip 추가

도구상자에서 Panel 추가

public partial class Form1 : Form
{
    //필드(멤버변수)
    //private 생략되어있음

 

필드에 먼저 여러 함수와 객체 지정

 Graphics g;

아날로그 시계를 그릴 Grphics 객체

 bool aClockFlag = false;//아날로그 시계

aClockFlag bool함수가 fales면 디지털 시계 true면 아날로그 시계를 그려줄 bool 함수

Point center;
int radius;
int hourHand; //시침의 길이
int minHand; //분침길이
int secHand; //시침

Point center는 중심점

radius는 반지름

등등  함수지정

const int clientsSize = 300; //client의 크기
const int clockSize = 200; //시계의 크기

const 상수를 지정해주면 그 값은 바뀔수 없다

 Font fDate; //날짜 폰트
 Font fTime; //시간 폰트
 Brush bDate;
 Brush bTime;

Font 클래스는 폰트설정

Brush 클래스는 글자의 색깔 설정

 Point panelCenter;

panel의 중심 Point

public Form1()
{
    InitializeComponent();
    this.ClientSize = new Size(clientsSize, clockSize + menuStrip1.Height);
    this.Text = "My Form Clock";

클라이언트 속성 지정

프로그램 상단 문구 설정

//panel 지정
panel1.BackColor = Color.WhiteSmoke;
panel1.Width = clockSize + 1;
panel1.Height = clockSize + 1;
panel1.Location = new Point(clientsSize / 2-clockSize / 2, clientsSize / 2 - clockSize / 2 + menuStrip1.Height);

panel1 설정

panel1의 배경색은 Whitesmoke 

너비와 높이는 clockSize에 + 1

위치지정

g = panel1.CreateGraphics(); //pancel에서 그려주는 그래픽 객체
aClockSetting();
dClockSetting();
timerSetting();

panel1에서 그려질 그래픽 객체

aClockSetting 메소드

private void aClockSetting()
{//아날로그 시계 세팅
    center = new Point(clientsSize / 2, clockSize / 2);
    radius = clockSize / 2;
    hourHand = (int)(radius * 0.45);
    minHand = (int)(radius * 0.55);
    secHand = (int)(radius * 0.65);
}

아날로그 시계에서의 시침 분침 초침 길이 설정

dClockSetting 메소드

private void dClockSetting()
{//디지털 시계 세팅
    fDate = new Font("맑은 고딕",12,FontStyle.Bold);
    fTime = new Font("맑은 고딕", 32, FontStyle.Bold|FontStyle.Italic);
    bDate = Brushes.SkyBlue;
    bTime = Brushes.Blue;
}

디지털 시계의 폰트,글자색상 등등 설정

timerSetting 메소드

 private void timerSetting()
 {//시계설정
     Timer t = new Timer();

     t.Enabled = true;
     t.Interval = 1000;
     t.Tick += T_tick;
 }

시계 설정

저번주차에 해서 생략

Timer1_Tick 메소드

private void T_tick(object sender, EventArgs e)
{
    DateTime c = DateTime.Now;
    panel1.Refresh();

현재시각 c 에 불러오고

panel1 새로고침

if(aClockFlag == true){//아날로그라면?
    DrawClockFace();

    double radHr = (c.Hour % 12 + c.Minute / 60.0) * 30 * Math.PI / 180;
    double radMin = (c.Minute + c.Second / 60.0) * 60.0 * 6 * Math.PI / 180;
    double radSec = c.Second * 6 * Math.PI / 180;

    DrawHands(radHr, radMin, radSec);
}

시계판을 그린후

시침 분침 초침을 초를 기준으로 만든후

바늘을 그려준다

    else {//디지털이라면?
        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, fDate, bDate, new Point(10, 70));
        g.DrawString(time, fDate, bTime, new Point(10, 100));
    }
}

오늘의 날짜를 글자로 "D"(자세한 날짜)로 도출하고

지금 시간 도출후

위치를 설정

DrawClockFace 메소드

 private void DrawClockFace()
 {
     const int penWidth = 30;

     Pen p = new Pen(Brushes.LightSteelBlue,penWidth);
     g.DrawEllipse(p, penWidth / 2, penWidth / 2, clockSize - penWidth, clockSize - penWidth);
 }

펜의 굵기는 30

Pen p는 밝은 파란색이며

원을 그려준다

DrawHands 메소드

private void DrawHands(double radHr, double radMin, double radSec)
{
    
}

'C#' 카테고리의 다른 글

9주차-FireBase 완성  (0) 2024.05.05
8주차-아날로그 시계(2) / FireBase(1)  (0) 2024.04.23
5주차-범위,차트,디지털 시계  (1) 2024.04.07
4주차-Loop문 외...  (0) 2024.03.31
4주차  (1) 2024.03.27