C# Windows Form アナログ時計 ソースコード ― 2022年08月09日 18:50
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 anarogtokei
{
public partial class Form1 : Form
{
Bitmap bmpCampus;
Graphics graCampus;
Pen[] penDraw = new Pen[3];
Image img = Image.FromFile(@"img\banmen.png");
Font fnt;
const int BYO = 0;
const int FUN = 1;
const int JIK = 2;
const int BYO_NAGA2 = 20;
const int BYO_NAGA = 90;
const int FUN_NAGA = 80;
const int JIK_NAGA = 60;
const int BYO_360 = 6; //秒 6 は 360 / 6 = 60秒
const int FUN_360 = 6; //分 6 は 360 / 6 = 60分
const int JIK_360 = 30; //時 30 は 360 / 30 = 12時間
const int BYO_FUTO = 1;
const int FUN_FUTO = 2;
const int JIK_FUTO = 4;
const int MOJ_X = 70;
const int MOJ_Y = 120;
public Form1()
{
InitializeComponent();
pbCampus.Width = 200;
pbCampus.Height = 200;
fnt = new Font("MS UI Gothic", 12);
bmpCampus = new Bitmap(pbCampus.Width, pbCampus.Height);
pbCampus.Image = bmpCampus;
graCampus = Graphics.FromImage(pbCampus.Image);
penDraw[BYO] = new Pen(Color.Red, BYO_FUTO);
penDraw[FUN] = new Pen(Color.Blue, FUN_FUTO);
penDraw[JIK] = new Pen(Color.Black, JIK_FUTO);
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
double fun,byo;
DateTime d = DateTime.Now;
//label1.Text = d.ToLongTimeString();
//graCampus.Clear(Color.Black);
graCampus.DrawImage(img, 0, 0, bmpCampus.Width, bmpCampus.Height);
//針を描く
byo = d.Second / 60.0; //C#の実数割り算はどちらかに.0をつけること!
fun = d.Minute / 60.0;
//label1.Text = fun.ToString();
graCampus.DrawString(d.ToLongTimeString(), fnt, Brushes.Green, MOJ_X, MOJ_Y);
draw(BYO_360, BYO, d.Second , BYO_NAGA);
draw(BYO_360, BYO, d.Second + 30 , BYO_NAGA2); //秒針の中心より反対側を描く
draw(FUN_360, FUN, d.Minute + byo, FUN_NAGA);
draw(JIK_360, JIK, d.Hour + fun, JIK_NAGA);
pbCampus.Refresh();
}
private void draw(int bairitu, int ban, double time, int harinaga)
{
//中心点
int cx = pbCampus.Width / 2;
int cy = pbCampus.Height / 2;
//原点
int x1 = cx;
int y1 = cy;
//針
double kakudotani = Math.PI / 180;
double kakudo = kakudotani * bairitu * time - Math.PI / 2;
int x2 = cx + (int)(Math.Cos(kakudo) * harinaga);
int y2 = cy + (int)(Math.Sin(kakudo) * harinaga);
graCampus.DrawLine(penDraw[ban], x1, y1, x2, y2);
}
}
}
コメント
トラックバック
このエントリのトラックバックURL: http://tukasa.asablo.jp/blog/2022/08/09/9516427/tb