こんな感じの時計です。
画像なので静止画ですが、実際は時計として動作します。
サンプルコード
namespace test
{
public partial class Form1 : Form
{
const int Base60 = 6; // 60進数
const int Base24 = 30; // 24進数
const int HandsSecond = 100; // 秒針の長さ
const int HandsMinute = 90; // 長針の長さ
const int HandsHour = 70; // 短針の長さ
const double AngleUnit = Math.PI / 180; // 角度当たりの単位
const int PenWidth1 = 1; // 線の太さ
const int PenWidth2 = 2; // 線の太さ
const int PenWidth3 = 3; // 線の太さ
const int PenWidth6 = 6; // 線の太さ
public Form1()
{
InitializeComponent();
}
// ************************************************
// タイマーイベント
// ************************************************
private void timer1_Tick(object sender, EventArgs e)
{
DateTime Dtime = DateTime.Now; // 現在の日時取得
// フォームセンター座標取得
int CenterX = Width / 2;
int CenterY = Height / 2;
using (Graphics Graphics = CreateGraphics())
{
// 針の色と太さ
Pen PenSecond = new Pen(Color.Black,PenWidth1);
Pen PenMinute = new Pen(Color.Black,PenWidth2);
Pen PenHour = new Pen(Color.Black,PenWidth3);
// 時と分を求める
double Minute = Dtime.Minute + (double)Dtime.Second / 60;
double Hour = Dtime.Hour + (double)Dtime.Minute / 60;
// 文字盤を描画
Graphics.FillEllipse(Brushes.White, CenterX - HandsSecond, CenterY - HandsSecond, HandsSecond + HandsSecond, HandsSecond + HandsSecond);
Pen PenPoint = new Pen(Color.DarkGray,PenWidth6);
Graphics.DrawLine(PenPoint,CenterX,CenterY - HandsSecond,CenterX,CenterY - HandsMinute);
Graphics.DrawLine(PenPoint,CenterX + HandsMinute,CenterY,CenterX + HandsSecond,CenterY);
Graphics.DrawLine(PenPoint,CenterX,CenterY + HandsMinute,CenterX,CenterY + HandsSecond);
Graphics.DrawLine(PenPoint,CenterX - HandsSecond,CenterY,CenterX - HandsMinute,CenterY);
// 針を描画
DrawHands(Graphics,PenSecond,HandsSecond,Base60,Dtime.Second);
DrawHands(Graphics,PenMinute,HandsMinute,Base60,Minute);
DrawHands(Graphics,PenHour,HandsHour,Base24,Hour);
Graphics.Dispose();
}
}
// ************************************************
// 時計を表示
// ************************************************
void DrawHands(Graphics Graphics,Pen PenWidth,int HandsLength,int BaseTime,double Time)
{
// フォームセンター座標取得
int CenterX = Width / 2;
int CenterY = Height / 2;
// 罫線描画起点
int x1 = CenterX;
int y1 = CenterY;
// 針描画
double Angle = AngleUnit * BaseTime * Time - Math.PI / 2;
int x2 = CenterX + (int)(Math.Cos(Angle) * HandsLength);
int y2 = CenterY + (int)(Math.Sin(Angle) * HandsLength);
Graphics.DrawLine(PenWidth,x1,y1,x2,y2);
}
// ************************************************
// フォームロードイベント
// ************************************************
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
}
}
フォームロードイベントでタイマーを開始します。
タイマーイベントで文字盤と時計の針を描画します。
コメント