using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Drawing; namespace firstCSharp { class FirstForm : Form { 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; } 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> 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; } */ } } }