using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace test1Program { public partial class Form1 : Form { const float calHeightInPixels = 600.0f; const float calWidthInPixels = 348.0f; const float worldWidth = 100; const float worldHeight = 172; const float topLeftPlusX = 6; const float topLeftPlusY = 64; const float ButtonWidth = 18; const float ButtonHeight = 12; const float ButtonWidthPlusGap = ButtonWidth + 3.5f; const float ButtonHeightPlusGap = ButtonHeight + 5f; Point anchorPoint_ = new Point(0, 0); const int nColumns = 3; float buttonWidthPercent = ButtonWidth / ButtonWidthPlusGap; float buttonHeightPercent = ButtonHeight / ButtonHeightPlusGap; Timer sevenTimer_ = new Timer(); bool sevenIsMoving_ = false; PointF topLeftOfSeven_ = new PointF(6,64+2*ButtonHeightPlusGap); PointF velocityOfSeven_ = new PointF(0, 0); public Form1() { InitializeComponent(); Width = (int) calWidthInPixels; Height = (int) calHeightInPixels; Paint += Form1_Paint; MouseClick += Form1_MouseClick; MouseMove += Form1_MouseMove; KeyDown += Form1_KeyDown; sevenTimer_.Tick += SevenTimer__Tick; } private void SevenTimer__Tick(object sender, EventArgs e) { topLeftOfSeven_.X += velocityOfSeven_.X; topLeftOfSeven_.Y += velocityOfSeven_.Y; // Check for hitting a wall. if (topLeftOfSeven_.X < 0 || topLeftOfSeven_.X + ButtonWidth > worldWidth) velocityOfSeven_.X *= -1; if (topLeftOfSeven_.Y < 0 || topLeftOfSeven_.Y + ButtonHeight > worldHeight) velocityOfSeven_.Y *= -1; Invalidate(true); } private void Form1_KeyDown(object sender, KeyEventArgs e) { sevenTimer_.Interval = 1000 / 30; sevenTimer_.Start(); velocityOfSeven_.X = 1; velocityOfSeven_.Y = 1; sevenIsMoving_ = true; } private void Form1_MouseMove(object sender, MouseEventArgs e) { Graphics graphics = CreateGraphics(); graphics.ScaleTransform( ClientRectangle.Width / worldWidth, ClientRectangle.Height / worldHeight); PointF[] transformPts = new PointF[2]; transformPts[0] = new PointF(anchorPoint_.X, anchorPoint_.Y); transformPts[1] = new PointF(e.X, e.Y); graphics.TransformPoints(CoordinateSpace.World, CoordinateSpace.Device, transformPts); Text = String.Format("({0}, {1}) : ({2}, {3})", e.X - anchorPoint_.X, e.Y - anchorPoint_.Y, transformPts[1].X - transformPts[0].X, transformPts[1].Y - transformPts[0].Y); } private void Form1_MouseClick(object sender, MouseEventArgs e) { anchorPoint_.X = e.X; anchorPoint_.Y = e.Y; } private void Form1_Paint(object sender, PaintEventArgs e) { // Compute pixels/worldUnit e.Graphics.ScaleTransform( ClientRectangle.Width / worldWidth, ClientRectangle.Height / worldHeight); e.Graphics.TranslateTransform(topLeftPlusX, topLeftPlusY); e.Graphics.ScaleTransform(ButtonWidthPlusGap, ButtonHeightPlusGap); for(int row=0;row<6;++row) { for(int column=0;column