プロが教える店舗&オフィスのセキュリティ対策術

//Visual C#2019でアナログ時計の製作練習をしている初心者です
//下記のプログラムで分針、秒針は正常に動作するのですが短針(時間針)のみが正常に動かず、1時、
//3時等の「正時」のみ動きます
//どうしても原因が分からず、詳しい方の力をお借りしたいと思います。
//また、改善点が有りましたら具体的に教えてください。(timer1_Tick_1の中にあるInvalidate();
//の挿入位置が正しいか、これを使うことが妥当か?など)

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;
using System.Drawing.Drawing2D;

namespace clock_vc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

this.DoubleBuffered = true;//秒針が動く時のチラツキを止める
}

//各種処理を関数側に渡す
private void Form1_Paint(object sender, PaintEventArgs e)
{
//円盤上に1~12の目盛り表示する
memori_hyoji(e);

//針を動かす
hari_move(e);
}

//年月日、時刻の表示処理(始まり)
private void timer1_Tick_1(object sender, EventArgs e)
{
Invalidate();//これがないと針が動かないのでここに入れてみたが挿入位置が正しいかどうか分からない)
}


//円盤上に1~12の目盛り表示する(始まり)
void memori_hyoji(PaintEventArgs e)
{
//円盤上に1~12の目盛り表示する(始まり)
Font myfont = new Font("MS P ゴシック", 12); // 文字盤1~12の数字の大きさを12として初期設定
SolidBrush mybrush1 = new SolidBrush(Color.Red);

double x3,y3;//文字表示位置の座標
int n;
const double pii = 3.1415926535;

for (n=1; n <= 12; n++)
{
x3 = 138 + 110 * Math.Sin((pii / 180) * n * 30); //文字盤1~12の数字のx座標、30°ピッチ
y3 = 165 - 110 * Math.Cos((pii / 180) * n * 30); //文字盤1~12の数字のy座標、30°ピッチ

e.Graphics.DrawString(n.ToString(), myfont, mybrush1, (int)x3, (int)y3); //文字盤1~12の数字の描画
}
}


//針を動かす(始まり)
void hari_move(PaintEventArgs e)
{
DateTime dt = DateTime.Now;

//時分秒の取り出しと針を動かす
double x4, y4;//秒 // 各座標を表す変数
double x5, y5;//分
double x6, y6;//時
int ss, mm, hh;//秒、分、時
const double pi = 3.1415926535;


//秒針(始まり) 円盤の中心を(145,175)とする
ss = dt.Second; // 秒を取り出す
x4 = 145 + 115 * Math.Sin((pi / 180) * (ss * 6)); //秒針先端のx座標・・・1secで6°進む
y4 = 175 - 115 * Math.Cos((pi / 180) * (ss * 6));///'秒針先端のy座標//160-115*

Pen mypen2 = new Pen(Color.Gray, 2);
e.Graphics.DrawLine(mypen2, 145, 175, (int)x4, (int)y4); // 秒針を表示し、動かす145,165
//秒針(終わり)


//長針(始まり)
mm = dt.Minute; //分を取り出す

x5 = 145 + 100 * Math.Sin((pi / 180) * ((mm * 6) + (ss * 6 / 60))); //長針先端のx座標・・・1minで6°進む
y5 = 175 - 100 * Math.Cos((pi / 180) * ((mm * 6) + (ss * 6 / 60)));//'長針先端のy座標

Pen mypen3 = new Pen(Color.Black, 8);
mypen3.EndCap = LineCap.Triangle; // 針先を三角形にする
e.Graphics.DrawLine(mypen3, 145, 175, (int)x5, (int)y5);//長針を動かす
//長針(終わり)


//短針(始まり)
hh=dt.Hour; //時を取り出す

x6 = 145 + 75 * Math.Sin((pi / 180) * ((hh * 30) + (mm * (30 / 60)) + ss * (30 / 3600)));// '短針先端のx座標・・・1hrで30°進む
y6 = 175 - 75 * Math.Cos((pi / 180) * ((hh * 30) + (mm * (30 / 60)) + ss * (30 / 3600)));// '短針先端のy座標

Pen mypen4 = new Pen(Color.Black, 12);
mypen4.EndCap = LineCap.Triangle; //針先を三角形にする
e.Graphics.DrawLine(mypen4, 145, 175, (int)x6, (int)y6);
//短針(終わり)
}
//針を動かす(終わり)
}
}

A 回答 (1件)

30 / 60 = 0 だからですかね。


30.0 / 60 = 0.5 と型変換させてやると意図した動作になると思います。
(他にも同様の修正が必要な箇所がいくつかありますね。)
    • good
    • 0
この回答へのお礼

「おみくろん」さん
早速、適格な回答を頂き有難うございました。
正常に動作することを確認しました。
これで3日間の苦労が一気に解決しました。
今後とも宜しくお願い致します。

お礼日時:2021/03/10 17:30

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!