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.IO; namespace firstCSharp { class FirstForm : Form { // ToolStripContainer lets me move the toolstrip around. private ToolStripContainer tsContainer_ = new ToolStripContainer(); private MenuStrip menu_ = new MenuStrip(); public FirstForm() { BackColor = Color.Tomato; Text = "Hello world!"; Height = 600; const double GOLDEN_RATIO = 1.618; Width = (int)(Height * GOLDEN_RATIO); MouseClick += FirstForm_MouseClick; MouseMove += FirstForm_MouseMove; Paint += FirstForm_Paint; Resize += resizeHandler; DoubleBuffered = true; // Opacity = .5; //tsContainer_.Dock = DockStyle.Fill; //tsContainer_.TopToolStripPanel.Controls.Add(menu_); this.Controls.Add(menu_); // Add the tsContainer to the Form's set of controls //this.Controls.Add(tsContainer_); // Create a menu for the menu strip (File) ToolStripMenuItem fileMenu = new ToolStripMenuItem(); fileMenu.Text = "&File"; menu_.Items.Add(fileMenu); // Create a menu for the menu strip (View) ToolStripMenuItem viewMenu = new ToolStripMenuItem(); viewMenu.Text = "Vi&e&w"; menu_.Items.Add(viewMenu); ToolStripMenuItem selectColor = new ToolStripMenuItem(); selectColor.Text = "&Color"; selectColor.Click += SelectColor_Click; viewMenu.DropDownItems.Add(selectColor); ToolStripMenuItem fileOpen = new ToolStripMenuItem(); fileOpen.Text = "&Open"; fileOpen.Click += FileOpen_Click; fileMenu.DropDownItems.Add(fileOpen); } private void SelectColor_Click(object sender, EventArgs e) { ColorDialog colorDialog = new ColorDialog(); DialogResult result = colorDialog.ShowDialog(); if (result != DialogResult.Cancel) { BackColor = colorDialog.Color; } } private void FileOpen_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); DialogResult result = dialog.ShowDialog(); if (result != DialogResult.Cancel) { MessageBox.Show(String.Format("The file is: {0}", dialog.FileName)); // Read the file List 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 lines 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; // 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) { // Draw a chessboard in the window. // 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))); */ 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; } } // 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); // 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(); } private void FirstForm_MouseClick(object sender, MouseEventArgs e) { // 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; } */ } } }