using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.Timers;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
const double MaxPixelPerSec = 500;
float x = 0;
Stopwatch Stopwatch = new Stopwatch();
System.Timers.Timer Timer2 = new System.Timers.Timer();
public Form1()
{
InitializeComponent();
UpdatePixelPerSec();
Timer2.SynchronizingObject = this;
UpdateTimerInterval();
Timer2.Elapsed += Timer2_Elapsed;
Timer2.Start();
}
public double PixelPerSec
{
get
{
return Math.Round(MaxPixelPerSec * tbSpeedInPercentage.Value / 100, 2);
}
}
private void Timer2_Elapsed(object sender, ElapsedEventArgs e)
{
var bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
var r = pictureBox1.Height / 2;
x += (float)(PixelPerSec * Stopwatch.Elapsed.TotalSeconds);
if (x > pictureBox1.Width) x -= pictureBox1.Width + 2 * r;
Stopwatch.Restart();
using (var g = Graphics.FromImage(bmp))
{
g.Clear(Color.White);
using (var br = new SolidBrush(Color.Red))
{
g.FillEllipse(br, x, 0, r * 2, r * 2);
}
}
pictureBox1.Image = bmp;
}
private void UpdateTimerInterval()
{
Timer2.Interval = 1000 / tbFrameRate.Value;
lblFps.Text = tbFrameRate.Value.ToString();
}
private void UpdatePixelPerSec()
{
lblPixelPerSec.Text = PixelPerSec.ToString("F2");
}
private void tbFrameRate_ValueChanged(object sender, EventArgs e)
{
UpdateTimerInterval();
}
private void tbSpeedInPercentage_ValueChanged(object sender, EventArgs e)
{
UpdatePixelPerSec();
}
}
partial class Form1
{
/// <summary>
/// 必要なデザイナー変数です。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 使用中のリソースをすべてクリーンアップします。
/// </summary>
/// <param name="disposing">マネージ リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows フォーム デザイナーで生成されたコード
/// <summary>
/// デザイナー サポートに必要なメソッドです。このメソッドの内容を
/// コード エディターで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.tbFrameRate = new System.Windows.Forms.TrackBar();
this.tbSpeedInPercentage = new System.Windows.Forms.TrackBar();
this.label1 = new System.Windows.Forms.Label();
this.lblFps = new System.Windows.Forms.Label();
this.lblPixelPerSec = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.tbFrameRate)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.tbSpeedInPercentage)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.pictureBox1.Location = new System.Drawing.Point(12, 12);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(685, 208);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// tbFrameRate
//
this.tbFrameRate.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tbFrameRate.Location = new System.Drawing.Point(150, 226);
this.tbFrameRate.Maximum = 60;
this.tbFrameRate.Minimum = 1;
this.tbFrameRate.Name = "tbFrameRate";
this.tbFrameRate.Size = new System.Drawing.Size(547, 45);
this.tbFrameRate.TabIndex = 1;
this.tbFrameRate.TickFrequency = 10;
this.tbFrameRate.Value = 30;
this.tbFrameRate.ValueChanged += new System.EventHandler(this.tbFrameRate_ValueChanged);
//
// tbSpeedInPercentage
//
this.tbSpeedInPercentage.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tbSpeedInPercentage.Location = new System.Drawing.Point(150, 277);
this.tbSpeedInPercentage.Maximum = 100;
this.tbSpeedInPercentage.Name = "tbSpeedInPercentage";
this.tbSpeedInPercentage.Size = new System.Drawing.Size(547, 45);
this.tbSpeedInPercentage.TabIndex = 2;
this.tbSpeedInPercentage.TickFrequency = 10;
this.tbSpeedInPercentage.Value = 20;
this.tbSpeedInPercentage.ValueChanged += new System.EventHandler(this.tbSpeedInPercentage_ValueChanged);
//
// label1
//
this.label1.Location = new System.Drawing.Point(10, 226);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 20);
this.label1.TabIndex = 3;
this.label1.Text = "Frame/sec";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// lblFps
//
this.lblFps.BackColor = System.Drawing.Color.White;
this.lblFps.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblFps.Location = new System.Drawing.Point(80, 226);
this.lblFps.Name = "lblFps";
this.lblFps.Size = new System.Drawing.Size(64, 20);
this.lblFps.TabIndex = 4;
this.lblFps.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// lblPixelPerSec
//
this.lblPixelPerSec.BackColor = System.Drawing.Color.White;
this.lblPixelPerSec.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblPixelPerSec.Location = new System.Drawing.Point(80, 277);
this.lblPixelPerSec.Name = "lblPixelPerSec";
this.lblPixelPerSec.Size = new System.Drawing.Size(64, 20);
this.lblPixelPerSec.TabIndex = 6;
this.lblPixelPerSec.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label4
//
this.label4.Location = new System.Drawing.Point(10, 277);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(64, 20);
this.label4.TabIndex = 5;
this.label4.Text = "Pixel/sec";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(709, 334);
this.Controls.Add(this.lblPixelPerSec);
this.Controls.Add(this.label4);
this.Controls.Add(this.lblFps);
this.Controls.Add(this.label1);
this.Controls.Add(this.tbSpeedInPercentage);
this.Controls.Add(this.tbFrameRate);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.tbFrameRate)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.tbSpeedInPercentage)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.TrackBar tbFrameRate;
private System.Windows.Forms.TrackBar tbSpeedInPercentage;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label lblFps;
private System.Windows.Forms.Label lblPixelPerSec;
private System.Windows.Forms.Label label4;
}
}