概要
cscの作法、調べてみた。
練習問題やってみた。
練習問題
アナログな時計を作れ。
参考にしたページ
写真
サンプルコード
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.Timers;
namespace Analog_Clock
{
public partial class Form1: Form {
private PictureBox pictureBox1;
private PictureBox pictureBox2;
private PictureBox pictureBox3;
private PictureBox pictureBox4;
private NotifyIcon notifyIcon1;
private ContextMenuStrip contextMenuStrip1;
private ToolStripMenuItem exitToolStripMenuItem;
private ToolStripMenuItem clockFaceToolStripMenuItem;
private ToolStripMenuItem locationSizeToolStripMenuItem;
private ToolStripMenuItem alarmEtcToolStripMenuItem;
private ToolStripMenuItem alarmOFFToolStripMenuItem;
private System.ComponentModel.IContainer components = null;
Bitmap Clock_Face;
Bitmap Clock_Second;
Bitmap Clock_Minute;
Bitmap Clock_Hour;
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
System.Drawing.Graphics g;
Clock_Face = new Bitmap(@"Clock_Face-006.png");
Clock_Second = new Bitmap(@"Clock-Hand-001s.png");
Clock_Minute = new Bitmap(@"Clock-Hand-001m.png");
Clock_Hour = new Bitmap(@"Clock-Hand-001h.png");
pictureBox1.Dock = DockStyle.Fill;
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox2.Dock = DockStyle.Fill;
pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox3.Dock = DockStyle.Fill;
pictureBox3.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox4.Dock = DockStyle.Fill;
pictureBox4.SizeMode = PictureBoxSizeMode.Zoom;
Clock_Face.MakeTransparent();
Clock_Hour.MakeTransparent();
Clock_Minute.MakeTransparent();
Clock_Second.MakeTransparent();
g = pictureBox2.CreateGraphics();
g.DrawImage(Clock_Hour, new System.Drawing.Point(0, 0));
g.Dispose();
g = pictureBox3.CreateGraphics();
g.DrawImage(Clock_Minute, new System.Drawing.Point(0, 0));
g.Dispose();
g = pictureBox4.CreateGraphics();
g.DrawImage(Clock_Second, new System.Drawing.Point(0, 0));
g.Dispose();
pictureBox1.Image = Clock_Face;
pictureBox2.Parent = pictureBox1;
pictureBox3.Parent = pictureBox2;
pictureBox4.Parent = pictureBox3;
pictureBox2.BackColor = Color.Transparent;
pictureBox3.BackColor = Color.Transparent;
pictureBox4.BackColor = Color.Transparent;
System.Timers.Timer timer = new System.Timers.Timer(1000);
timer.Elapsed += (sender_Temp, e_Temp) => {
Draw_Clock();
};
timer.Start();
}
private void Draw_Clock() {
DateTime time = DateTime.Now;
float SecondAng = (float) (time.Second * 6.0);
float MinuteAng = (float) ((time.Minute + time.Second / 60.0) * 6.0);
float HourAng = (float) ((time.Hour + time.Minute / 60.0) * 30.0);
pictureBox4.Image = RotateBitmap(Clock_Second, SecondAng, Clock_Second.Width / 2, Clock_Second.Height / 2);
pictureBox3.Image = RotateBitmap(Clock_Minute, MinuteAng, Clock_Minute.Width / 2, Clock_Minute.Height / 2);
pictureBox2.Image = RotateBitmap(Clock_Hour, HourAng, Clock_Hour.Width / 2, Clock_Hour.Height / 2);
}
public Bitmap RotateBitmap(Bitmap org_bmp, float angle, int x, int y) {
Bitmap result_bmp = new Bitmap((int) org_bmp.Width, (int) org_bmp.Height);
Graphics g = Graphics.FromImage(result_bmp);
g.TranslateTransform(-x, -y);
g.RotateTransform(angle, System.Drawing.Drawing2D.MatrixOrder.Append);
g.TranslateTransform(x, y, System.Drawing.Drawing2D.MatrixOrder.Append);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
g.DrawImageUnscaled(org_bmp, 0, 0);
g.Dispose();
return result_bmp;
}
protected override void Dispose(bool disposing) {
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent() {
this.components = new System.ComponentModel.Container();
//System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.pictureBox1 = new PictureBox();
this.pictureBox2 = new PictureBox();
this.pictureBox3 = new PictureBox();
this.pictureBox4 = new PictureBox();
this.notifyIcon1 = new NotifyIcon(this.components);
this.contextMenuStrip1 = new ContextMenuStrip(this.components);
this.exitToolStripMenuItem = new ToolStripMenuItem();
this.clockFaceToolStripMenuItem = new ToolStripMenuItem();
this.locationSizeToolStripMenuItem = new ToolStripMenuItem();
this.alarmEtcToolStripMenuItem = new ToolStripMenuItem();
this.alarmOFFToolStripMenuItem = new ToolStripMenuItem();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox4)).BeginInit();
this.contextMenuStrip1.SuspendLayout();
this.SuspendLayout();
this.pictureBox1.Location = new System.Drawing.Point(374, 22);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(100, 50);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox2.Location = new System.Drawing.Point(374, 110);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(100, 50);
this.pictureBox2.TabIndex = 0;
this.pictureBox2.TabStop = false;
this.pictureBox3.Location = new System.Drawing.Point(510, 110);
this.pictureBox3.Name = "pictureBox3";
this.pictureBox3.Size = new System.Drawing.Size(100, 50);
this.pictureBox3.TabIndex = 0;
this.pictureBox3.TabStop = false;
this.pictureBox4.Location = new System.Drawing.Point(641, 110);
this.pictureBox4.Name = "pictureBox4";
this.pictureBox4.Size = new System.Drawing.Size(100, 50);
this.pictureBox4.TabIndex = 0;
this.pictureBox4.TabStop = false;
this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
//this.notifyIcon1.Icon = ((System.Drawing.Icon) (resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "Clock_Setting";
this.notifyIcon1.Visible = true;
this.contextMenuStrip1.Items.AddRange(new ToolStripItem[] {
this.exitToolStripMenuItem,
this.clockFaceToolStripMenuItem,
this.locationSizeToolStripMenuItem,
this.alarmEtcToolStripMenuItem,
this.alarmOFFToolStripMenuItem
});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(190, 154);
this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
this.exitToolStripMenuItem.Size = new System.Drawing.Size(189, 30);
this.exitToolStripMenuItem.Text = "Exit";
//this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
this.clockFaceToolStripMenuItem.Name = "clockFaceToolStripMenuItem";
this.clockFaceToolStripMenuItem.Size = new System.Drawing.Size(189, 30);
this.clockFaceToolStripMenuItem.Text = "Clock_Face";
//this.clockFaceToolStripMenuItem.Click += new System.EventHandler(this.clockFaceToolStripMenuItem_Click);
this.locationSizeToolStripMenuItem.Name = "locationSizeToolStripMenuItem";
this.locationSizeToolStripMenuItem.Size = new System.Drawing.Size(189, 30);
this.locationSizeToolStripMenuItem.Text = "Location_Size";
//this.locationSizeToolStripMenuItem.Click += new System.EventHandler(this.locationSizeToolStripMenuItem_Click);
this.alarmEtcToolStripMenuItem.Name = "alarmEtcToolStripMenuItem";
this.alarmEtcToolStripMenuItem.Size = new System.Drawing.Size(189, 30);
this.alarmEtcToolStripMenuItem.Text = "Alarm_Set";
//this.alarmEtcToolStripMenuItem.Click += new System.EventHandler(this.alarmSetToolStripMenuItem_Click);
this.alarmOFFToolStripMenuItem.Name = "alarmOFFToolStripMenuItem";
this.alarmOFFToolStripMenuItem.Size = new System.Drawing.Size(189, 30);
this.alarmOFFToolStripMenuItem.Text = "Alarm_OFF";
//this.alarmOFFToolStripMenuItem.Click += new System.EventHandler(this.alarmOFFToolStripMenuItem_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.pictureBox4);
this.Controls.Add(this.pictureBox3);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Form1";
this.TransparencyKey = System.Drawing.SystemColors.Control;
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox4)).EndInit();
this.contextMenuStrip1.ResumeLayout(false);
this.ResumeLayout(false);
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
}
}
以上。