<Window x:Class="timer_analog.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:timer_analog"
        mc:Ignorable="d"
        Title="WPF Analog Clock" Height="350" Width="350">
    <Grid Margin="10">
        <Canvas x:Name="canvas1" HorizontalAlignment="Center" Height="250" VerticalAlignment="Center" Width="250">
            <Ellipse x:Name="aClock" Fill="#FFF4F4F5" Height="250" Stroke="Black" Width="250" StrokeThickness="30"/>
        </Canvas>

    </Grid>
</Window>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace timer_analog
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        private Point center;
        private double radius;
        private int hourHand;
        private int minHand;
        private int secHand;
        public MainWindow()
        {
            InitializeComponent();

            aClock_Setting();
            TimerSetting();
        }

        private void aClock_Setting()
        {
            center = new Point(canvas1.Width / 2, canvas1.Height / 2);
            radius = canvas1.Width / 2;
            hourHand = (int)(radius * 0.45);
            minHand = (int)(radius * 0.55);
            secHand = (int)(radius * 0.65);
        }

        private void TimerSetting()
        {
            DispatcherTimer timer = new DispatcherTimer(); //https://www.crocus.co.kr/1406 디스패쳐
            timer.Interval = new TimeSpan(0, 0, 0, 0, 10);
            timer.Tick += Timer_Tick;
            timer.Start();
        }

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

            canvas1.Children.Clear();
            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 + c.Millisecond / 1000.0) * 6 * Math.PI / 180;
            DrawHands(radHr, radMin, radSec); //바늘그리기.
        }

        //시계바늘 그리기/
        private void DrawClockFace()
        {
            aClock.Stroke = Brushes.LightSteelBlue;
            aClock.StrokeThickness = 30;
            canvas1.Children.Add(aClock);
        }

        //시계바늘 그리기.
        private void DrawHands(double radHr, double radMin, double radSec)
        {
            //시침.
            DrawLine(hourHand * Math.Sin(radHr), -hourHand * Math.Cos(radHr), 0, 0, Brushes.RoyalBlue, 8, new Thickness(center.X, center.Y, 0, 0));
            //분침
            DrawLine(minHand * Math.Sin(radMin), -minHand * Math.Cos(radMin), 0, 0, Brushes.SkyBlue, 6, new Thickness(center.X, center.Y, 0, 0));
            //초침
            DrawLine(secHand * Math.Sin(radSec), -secHand * Math.Cos(radSec), 0, 0, Brushes.OrangeRed, 3, new Thickness(center.X, center.Y, 0, 0));

            Ellipse core = new Ellipse();
            core.Margin = new Thickness(canvas1.Width / 2 - 10, canvas1.Height / 2 - 10, 0, 0);
            core.Stroke = Brushes.SteelBlue;
            core.Fill = Brushes.LightSteelBlue;
            core.Width = 20;
            core.Height = 20;
            canvas1.Children.Add(core);
        }

        private void DrawLine(double x1, double y1, int x2, int y2, SolidColorBrush color, int thick, Thickness margin)
        {
            Line line = new Line();
            line.X1 = x1;
            line.Y1 = y1;
            line.X2 = x2;
            line.Y2 = y2;

            line.Stroke = color;
            line.StrokeThickness = thick;
            line.Margin = margin;
            line.StrokeStartLineCap = PenLineCap.Round;
            canvas1.Children.Add(line);
        }
    }
}

 

'C#(.Net)' 카테고리의 다른 글

wpf View, Control 분리하여 Main xaml에 두 컨트롤 xaml 로드  (0) 2023.06.04
wpf 참조  (0) 2021.08.01
C# 기본 get, set (c++ getter, setter)  (0) 2021.07.13
wpf 바인딩 연습1  (0) 2021.07.12
데이터 바인딩  (0) 2021.07.11

+ Recent posts