using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { int x = 0; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //ダブルバッファリング this.SetStyle(ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); //四角形描画 e.Graphics.FillRectangle(Brushes.Red, x, 50, 100, 100); } private void Form1_Shown(object sender, EventArgs e) { //ゲームループ System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); double nextframe = (double)sw.ElapsedMilliseconds; float wait = 1000f / 60f; while (this.Created) { if ((double)sw.ElapsedMilliseconds >= nextframe) { //計算処理 x++; if ((double)sw.ElapsedMilliseconds < nextframe + wait) { //描画処理 this.Invalidate(); } nextframe += wait; } Application.DoEvents(); } } } }