C# WPF로 아래 그림과 같은 아날로그 시계를 만들어본다.
( 이 프로그램은 http://sarosh.wordpress.com/2012/12/24/analog-clock-in-wpf-c/ 의 코딩을 약간 변형한 것입니다. 코딩에 대한 설명은 이 링크의 원본 글을 참조하세요)
<Window x:Class="saroshClock.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="600" Width="800">
<StackPanel Orientation="Horizontal" Background="Yellow" >
<Canvas Name="InkCanvas" Margin="0,0,0,0"></Canvas>
</StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 saroshClock
{
public partial class MainWindow : Window
{
DateTime dateTime;
DispatcherTimer dispatchTimer;
float clockHeight = 500;
float clockRadius = 300;
float centerX = 400;
float centerY = 300;
float secThinkness = 1;
float minThinkness = 5;
float hourThinkness = 10;
public MainWindow()
{
InitializeComponent();
dispatchTimer = new DispatcherTimer();
dispatchTimer.Interval = new TimeSpan(0, 0, 1);
dispatchTimer.Tick += dispatchTimer_Tick;
dispatchTimer.Start();
}
private void DrawClock()
{
float hourLength = clockHeight / 3 / 1.65F;
float minLength = clockHeight / 2.8F;
float secLength = clockHeight / 3 / 1.15F;
InkCanvas.Children.Clear();
dateTime = DateTime.Now;
int minute = dateTime.Minute;
int sec = dateTime.Second;
float hour = dateTime.Hour % 12 + (float)dateTime.Minute / 60;
double hourRadian = hour * 360 / 12 * Math.PI / 180;
double minRadian = minute * 360 / 60 * Math.PI / 180;
double secRadian = sec * 360 / 60 * Math.PI / 180;
//Hour
float hourEndPointX = hourLength * (float)System.Math.Sin(hourRadian);
float hourEndPointY = hourLength * (float)System.Math.Cos(hourRadian);
DrawLine(centerX, centerY, centerX + hourEndPointX, centerY - hourEndPointY, Colors.Black, hourThinkness);
//minute
float minEndPointX = minLength * (float)Math.Sin(minRadian);
float minEndPointY = minLength * (float)Math.Cos(minRadian);
DrawLine(centerX, centerY, centerX + minEndPointX, centerY - minEndPointY, Colors.Blue, minThinkness);
//Second
float secEndPointX = secLength * (float)System.Math.Sin(secRadian);
float secEndPointY = secLength * (float)System.Math.Cos(secRadian);
DrawLine(centerX, centerY, centerX + secEndPointX, centerY - secEndPointY, Colors.Green, secThinkness);
// 바깥 눈금
float innerX, innerY, outerX, outerY;
for (int i = 0; i < 60; i++)
{
outerX = centerX + (float)(clockRadius / 1.50F * Math.Sin(i * 6 * Math.PI / 180));
outerY = centerY - (float)(clockRadius / 1.50F * Math.Cos(i * 6 * Math.PI / 180));
if (i % 5 == 0) // 굵은 눈금
{
innerX = centerX + (float)(clockRadius / 1.65F * Math.Sin(i * 6 * Math.PI / 180));
innerY = centerY - (float)(clockRadius / 1.65F * Math.Cos(i * 6 * Math.PI / 180));
}
else
{
innerX = centerX + (float)(clockRadius / 1.55F * Math.Sin(i * 6 * Math.PI / 180));
innerY = centerY - (float)(clockRadius / 1.55F * Math.Cos(i * 6 * Math.PI / 180));
}
DrawLine(outerX, outerY, innerX, innerY, Colors.Black, hourThinkness);
}
// center
Ellipse Center = new Ellipse();
Center.Margin = new Thickness(centerX - 10, centerY -10, 0, 0);
Center.Width = 20;
Center.Height = 20;
Center.Stroke = Brushes.Brown;
Center.StrokeThickness = 2;
Center.Fill = Brushes.Chocolate;
InkCanvas.Children.Add(Center);
}
private void DrawLine(double x1, double y1, double x2, double y2, Color color, float thinkness)
{
Line line = new Line();
line.X1 = x1; line.Y1 = y1; line.X2 = x2; line.Y2 = y2;
line.Stroke = new SolidColorBrush(color);
line.StrokeThickness = thinkness;
InkCanvas.Children.Add(line);
}
void dispatchTimer_Tick(object sender, object e)
{
DrawClock();
}
}
}
BeeEye 드무
'C# WPF' 카테고리의 다른 글
Thread.Sleep() 문제 (0) | 2013.11.14 |
---|---|
C# 숫자퍼즐(Jeu De Tacquin) in WPF (0) | 2013.10.19 |
Analog Clock in C# WPF(아날로그 시계) - 2 (0) | 2013.10.18 |
Analog Clock in C# WPF using Rotation(아날로그 시계) (0) | 2013.10.18 |
C# WPF 계산기 (0) | 2013.10.02 |