System.Environment.TickCount64を使い描画のタイミングをとっています。
プロジェクトの作成
PowerShellで実行。要dotnet.exe
mkdir プロジェクト名
cd プロジェクト名
dotnet new winforms
code .
ソースコード
ファイル名:Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
namespace TickCountSample
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var frm = new Form1();
var picbox1 = new PictureBox { Dock = DockStyle.Fill };
frm.Controls.Add(picbox1);
var bgc = BufferedGraphicsManager.Current;
var bg = bgc.Allocate(
picbox1.CreateGraphics(), picbox1.DisplayRectangle);
picbox1.Paint += (s, e) => bg.Render();
frm.Show();
var prev_tc = (double)Environment.TickCount64;
var wait = 1000d / 60d;
var next_tc = prev_tc + wait;
int x = 50;
int y = 50;
int r = 16;
int dx = 4;
int dy = 4;
int w = 800;
int h = 450;
while(frm.Created)
{
if ((double)Environment.TickCount64 > next_tc)
{
x = x + dx;
y = y + dy;
if ((x + r) > w || x < 0)
dx = dx * -1;
if ((y + r) > h || y < 0)
dy = dy * -1;
if ((double)Environment.TickCount64 < (next_tc + wait))
{
bg.Graphics.Clear(Color.Navy);
bg.Graphics.FillEllipse(Brushes.AliceBlue, x, y, r, r);
bg.Render();
}
next_tc += wait;
}
Application.DoEvents();
}
}
}
}
ビルド
dotnet build
実行
dotnet run
感想
今一
コメント