Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9301

cscの作法 その35

$
0
0

概要

cscの作法、調べてみた。
練習問題やってみた。

練習問題

アナログ時計。

写真

image

サンプルコード

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing.Drawing2D;


class form1: Form {
    form1() {
        Text = "clock";
        Opacity = 0.7;
        ClientSize = new Size(150, 150);
        Timer timer = new Timer();
        timer.Interval = 1000; 
        timer.Tick += new EventHandler(timerTick);
        timer.Start();
    }
    protected override void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        g.TranslateTransform(ClientSize.Width / 2, ClientSize.Height / 2, MatrixOrder.Append);
        Pen BPen = new Pen(Color.Blue, 5);
        Pen GPen = new Pen(Color.Green, 5);
        Pen RPen = new Pen(Color.Red, 3);
        Point center = new Point(0, 0);
        DateTime time = DateTime.Now;
        double secAng = 2.0 * Math.PI * time.Second / 60.0;
        double minAng = 2.0 * Math.PI * (time.Minute + time.Second / 60.0) / 60.0;
        double hourAng = 2.0 * Math.PI * (time.Hour + time.Minute / 60.0) / 12.0;
        int r = Math.Min(ClientSize.Width, ClientSize.Height) / 2;
        int secHandLength = (int) (0.9 * r);
        int minHandLength = (int) (0.9 * r);
        int hourHandLength = (int) (0.7 * r);
        Point secHand = new Point((int) (secHandLength * Math.Sin(secAng)), (int) (-secHandLength * Math.Cos(secAng)));
        Point minHand = new Point((int) (minHandLength * Math.Sin(minAng)), (int) (-minHandLength * Math.Cos(minAng)));
        Point hourHand = new Point((int) (hourHandLength * Math.Sin(hourAng)), (int) (-hourHandLength * Math.Cos(hourAng)));
        g.DrawLine(RPen, center, secHand);
        g.DrawLine(GPen, center, minHand);
        g.DrawLine(BPen, center, hourHand);
    }
    void timerTick(object sender, EventArgs e) {
        this.Invalidate();
    }
    [STAThread]
    public static void Main() {
        Application.Run(new form1());
    }
}




以上。


Viewing all articles
Browse latest Browse all 9301

Trending Articles