using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Drawing; using System.Drawing.Drawing2D; using System.IO; using System.Drawing.Printing; namespace firstCSharp { class FirstForm : Form { CustomDialog dialog = new CustomDialog(); // ToolStripContainer lets me move the toolstrip around. private ToolStripContainer tsContainer_ = new ToolStripContainer(); private MenuStrip menu_ = new MenuStrip(); private Point dragReference_ = new Point(0, 0); private Point dragDelta_ = new Point(0, 0); List moveables_ = new List(); Piece thePiece_ = new Piece(new Bitmap( firstCSharp.Properties.Resources.blackKnight), new PointF(1,7)); Piece anotherPiece_ = new Piece(new Bitmap( firstCSharp.Properties.Resources.blackKnight), new PointF(1,0)); public FirstForm() { BackColor = Color.Tomato; Text = "Hello world!"; Height = 600; const double GOLDEN_RATIO = 1.618; Width = (int)(Height * GOLDEN_RATIO); // MouseClick += FirstForm_MouseClick; MouseDown += FirstForm_MouseDown1; MouseUp += FirstForm_MouseUp; MouseMove += FirstForm_MouseMove; Paint += FirstForm_Paint; // Create an instance of the Timer class const int REFRESHES_PER_SEC = 10; const int MILLISECONDS_PER_SECOND = 1000; Timer pieceTimer = new Timer(); pieceTimer.Tick += PieceTimer_Tick ; pieceTimer.Interval = MILLISECONDS_PER_SECOND / REFRESHES_PER_SEC; pieceTimer.Start(); moveables_.Add(thePiece_); moveables_.Add(anotherPiece_); const int N_COLUMNS = 8; for(int i=0;i dataPointList = getPlotData(dialog.FileName); // Create an array that has the points from the list PointF[] pointArray = new PointF[dataPointList.Count]; int nextIndex = 0; foreach (PointF point in dataPointList) { pointArray[nextIndex++] = point; } } } private List getPlotData(String filename) { List listOfPoints = new List(); StreamReader reader = new StreamReader(filename); char[] delimiters = new char[] { ' ', '\t', ';', ',' }; string nextLine; do { // Read an entire line nextLine = reader.ReadLine(); // Ignore blank li nes if (!String.IsNullOrEmpty(nextLine) && !String.IsNullOrWhiteSpace(nextLine)) { // Process lines with data // Create an array with one item for each field // in the line. string[] field = nextLine.Split(delimiters, StringSplitOptions.RemoveEmptyEntries); const int VALUES_PER_LINE = 2; if (field.Length == VALUES_PER_LINE) { // Use the Convert class to convert from // string to float float x = Convert.ToSingle(field[0]); float y = Convert.ToSingle(field[1]); listOfPoints.Add(new PointF(x, y)); } } } while (!reader.EndOfStream); // Close the stream reader.Close(); // Return the list of points return listOfPoints; } private void resizeHandler(object sender, EventArgs e) { // When the windows resized, we want to generate a paint event // In Windows, we do this with the Invalidate function Invalidate(); } private void setWorldCoordinates(Graphics graphics) { const int N_ROWS = 8; const int N_COLUMNS = 8; // ClientRectangle can provide the size of the client area int width = ClientRectangle.Width; int height = ClientRectangle.Height - menu_.Height; graphics.TranslateTransform(0, menu_.Height); // Find the smaller of the two dimensions int squareSide = Math.Min(width, height); int totalMargin = width - squareSide; int leftMargin = totalMargin / 2; int topMargin = (height - squareSide) / 2; graphics.TranslateTransform(leftMargin, topMargin); // Specify the number of pixels/world coordinate graphics.ScaleTransform(squareSide / N_COLUMNS, squareSide / N_ROWS); // At this point we are using the scaled wcs // graphics.TranslateTransform(3, 1); } private void FirstForm_Paint(object sender, PaintEventArgs e) { e.Graphics.TranslateTransform(dragDelta_.X, dragDelta_.Y); // Draw a chessboard in the window. Pen myPen = Pens.Red; // Create a font const int FONT_SIZE = 12; Font newTimesRoman = new Font("New Times Roman", FONT_SIZE, FontStyle.Bold | FontStyle.Italic | FontStyle.Underline | FontStyle.Strikeout); StringFormat format = new StringFormat(); format.LineAlignment = StringAlignment.Center; format.FormatFlags = StringFormatFlags.DirectionRightToLeft; // Pens of width zero are always 1 pixel in width // Pen myPlotPen = new Pen(Brushes.Red, 0); //myPlotPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; // Get the instance of class graphics that is associated // with the client area of this window. Graphics graphics = e.Graphics; setWorldCoordinates(e.Graphics); /* // Draw a square showing where the board will be graphics.DrawRectangle(Pens.Green, new Rectangle(new Point(0, 0), new Size(squareSide, squareSide))); graphics.FillRectangle(Brushes.Green, new Rectangle(new Point(0, 0), new Size(squareSide, squareSide))); */ drawBoard(graphics); // graphics.ResetTransform(); GraphicsState savedState = graphics.Save(); graphics.ResetTransform(); e.Graphics.TranslateTransform(dragDelta_.X, dragDelta_.Y); e.Graphics.DrawString( "Ask Ian when the chess club meets.", newTimesRoman, Brushes.Aqua, new PointF(ClientRectangle.Width / 2.0f, ClientRectangle.Height / 2.0f), format); graphics.Restore(savedState); } private void drawBoard(Graphics graphics) { const int N_ROWS = 8; const int N_COLUMNS = 8; Brush lightSquareBrush = Brushes.Bisque; Brush darkSquareBrush = Brushes.DarkGreen; Brush nextSquare = lightSquareBrush; // int smallSquare = squareSide / N_ROWS; for (int row = 0; row < N_ROWS; ++row) { for (int column = 0; column < N_COLUMNS; ++column) { //graphics.FillRectangle(nextSquare, // new Rectangle(column * smallSquare, row * smallSquare, // smallSquare, smallSquare)); graphics.FillRectangle(nextSquare, new Rectangle(column, row, 1, 1)); nextSquare = (nextSquare == lightSquareBrush) ? darkSquareBrush : lightSquareBrush; } nextSquare = (nextSquare == lightSquareBrush) ? darkSquareBrush : lightSquareBrush; } // Place a piece on the board //Bitmap bitMap = new Bitmap("blackKnight.bmp"); //Bitmap bitMap = new // Bitmap(firstCSharp.Properties.Resources.blackKnight); //Rectangle knightSquare = new Rectangle(6, 0, 1, 1); //bitMap.MakeTransparent(Color.LightGray); //graphics.DrawImage(bitMap, knightSquare); foreach (IMoveableObject x in moveables_) { x.draw(graphics); } } // Messages are handled by a function called // WndProc private const int WM_LBUTTONDOWN = 0x201; private const int MK_LBUTTON = 0x0001; private const int MK_RBUTTON = 0x0002; private const int MK_SHIFT = 0x0004; /* protected override void WndProc(ref Message m) { Console.WriteLine("Message Id is : {0}", m.Msg); Console.WriteLine("Message to string is: {0}", m.ToString()); switch(m.Msg) { case WM_LBUTTONDOWN: int iwparam = (int) m.WParam; if ((iwparam & MK_LBUTTON) != 0) Console.WriteLine("************LButton is up!"); if ((iwparam & MK_RBUTTON) != 0) Console.WriteLine("************RButton is up!"); if ((iwparam & MK_SHIFT) != 0) Console.WriteLine("************Shift is down!"); int iLParam = (int) m.LParam; int x = iLParam & 0x0000ffff; int y = (iLParam >> 16) & 0x0000ffff; Console.WriteLine("xy coord is ({0}, {1})", x, y); break; } //if (m.Msg != WM_LBUTTONDOWN) { base.WndProc(ref m); } } */ private void FirstForm_MouseMove(object sender, MouseEventArgs e) { // Text = String.Format("({0}, {1})", e.X, e.Y); // Compute the distance from where you started draging. if (e.Button == MouseButtons.Left) { dragDelta_.X = e.X - dragReference_.X; dragDelta_.Y = e.Y - dragReference_.Y; } // Get an instance of class Graphics Graphics graphics = CreateGraphics(); setWorldCoordinates(graphics); // Create an array with points that you want to translate // from device coordinates to world coordinates. PointF[] mousePosition = new PointF[1]; mousePosition[0] = new PointF(e.X, e.Y); graphics.TransformPoints(System.Drawing.Drawing2D.CoordinateSpace.World, System.Drawing.Drawing2D.CoordinateSpace.Device, mousePosition); //Text = mousePosition[0].ToString(); Point truncatedPoint = new Point(); truncatedPoint.X = (int)mousePosition[0].X; truncatedPoint.Y = (int)mousePosition[0].Y; // Text = truncatedPoint.ToString(); Invalidate(); } private void FirstForm_MouseClick(object sender, MouseEventArgs e) { // Create a graphics Graphics graphics = CreateGraphics(); setWorldCoordinates(graphics); // Convert from device to world coordinates PointF[] pointToTransform = new PointF[1]; pointToTransform[0] = new PointF(e.X, e.Y); graphics.TransformPoints( System.Drawing.Drawing2D.CoordinateSpace.World, System.Drawing.Drawing2D.CoordinateSpace.Device, pointToTransform); Point truncPoint = new Point( (int) pointToTransform[0].X, (int) pointToTransform[0].Y); foreach(IMoveableObject x in moveables_) { x.moveTo(truncPoint); } Text = truncPoint.ToString(); // See if the left button was clicked //if (e.Button == MouseButtons.Left) //{ // // The want to change the color of the background // // Let them pick a color. // ColorDialog colorDialog = new ColorDialog(); // DialogResult result = colorDialog.ShowDialog(); // if (result != DialogResult.Cancel) // { // BackColor = colorDialog.Color; // } //} //else //{ // OpenFileDialog fileDialog = new OpenFileDialog(); // DialogResult result = fileDialog.ShowDialog(); // if (result != DialogResult.Cancel) // { // MessageBox.Show(fileDialog.FileName); // } //} /* if (BackColor == Color.Tomato) { BackColor = Color.Chartreuse; } else { BackColor = Color.Tomato; } */ } private void InitializeComponent() { this.SuspendLayout(); // // FirstForm // this.ClientSize = new System.Drawing.Size(284, 262); this.Name = "FirstForm"; this.ResumeLayout(false); } } public interface IMoveableObject { void move(); void draw(Graphics graphic); void moveTo(PointF x); } public class Piece : IMoveableObject { public Piece(Bitmap image, PointF currentLocation) { image_ = image; currentLocation_ = currentLocation; } public void moveTo(PointF destination) { destination_ = new PointF(destination.X, destination.Y); float xDistance = destination.X - currentLocation_.X; float yDistance = destination.Y - currentLocation_.Y; const float REFRESHES_PER_SECOND = 10; const float SECONDS_TO_DESTINATION = 1; xDelta_ = xDistance / (REFRESHES_PER_SECOND * SECONDS_TO_DESTINATION); yDelta_ = yDistance / (REFRESHES_PER_SECOND * SECONDS_TO_DESTINATION); ticksToDestination_ = (int) REFRESHES_PER_SECOND * (int) SECONDS_TO_DESTINATION; } public void move() { if (ticksToDestination_ > 0) { currentLocation_.X += xDelta_; currentLocation_.Y += yDelta_; --ticksToDestination_; } } public void draw(Graphics graphics) { RectangleF rect = new RectangleF(currentLocation_.X, currentLocation_.Y, 1, 1); image_.MakeTransparent(Color.LightGray); graphics.DrawImage(image_, rect); //currentLocation_.X += .1f; //currentLocation_.Y -= .1f; } private Bitmap image_; PointF currentLocation_; PointF destination_; int ticksToDestination_ = 0; float xDelta_ = 0; float yDelta_ = 0; } }