Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Function Plotter - Form1.cs

Form1.cs

Caricato da: Crybot
Scarica il programma completo

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Windows.Forms;
  6.  
  7. namespace DrawGraph
  8. {
  9.     public sealed partial class Form1 : Form
  10.     {
  11.         private new const char Enter = (char) 13;
  12.         private readonly Bitmap _buffer;
  13.         private readonly Drawer _drawer;
  14.         private readonly Graphics _formGraphics;
  15.         private Graphics _graphics;
  16.  
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.             CenterToScreen();
  21.             SetStyle(
  22.                 ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
  23.  
  24.             //creates a Graph object of quadratic size
  25.             var graph = new Graph(new Size(ClientSize.Width, ClientSize.Width), 20);
  26.             var blackPen = new Pen(Brushes.Black, 2);
  27.             var bluePen = new Pen(Brushes.DodgerBlue, 2);
  28.             //creates a Drawer object that will draw the graph and the functions
  29.             _drawer = new Drawer(graph, blackPen, blackPen, bluePen, Color.White);
  30.             //creates a BitMap used as a buffer
  31.             _buffer = new Bitmap((int) graph.Width, (int) graph.Heigth);
  32.             _formGraphics = CreateGraphics(); // creates the graphics object of the form
  33.             _formGraphics.CompositingQuality = CompositingQuality.HighSpeed;
  34.  
  35.             LoadComboBox(comboBox1, GetColors());
  36.             comboBox1.SelectedItem = comboBox1.Items[comboBox1.Items.IndexOf(Color.DodgerBlue.Name)];
  37.         }
  38.  
  39.         private List<string> GetColors()
  40.         {
  41.             var colors = new List<string>();
  42.             string[] colorNames = Enum.GetNames(typeof (KnownColor));
  43.  
  44.             foreach (string colorName in colorNames)
  45.             {
  46.                 var knownColor = (KnownColor) Enum.Parse(typeof (KnownColor), colorName);
  47.  
  48.                 if (knownColor > KnownColor.Transparent)
  49.                 {
  50.                     colors.Add(colorName);
  51.                 }
  52.             }
  53.             return colors;
  54.         }
  55.  
  56.         protected override void OnPaint(PaintEventArgs e)
  57.         {
  58.             UpdateScreen();
  59.             base.OnPaint(e);
  60.         }
  61.  
  62.         private void EnterPress(object sender, KeyPressEventArgs e)
  63.         {
  64.             if (e.KeyChar != Enter) return;
  65.  
  66.             var f = new Function();
  67.             try
  68.             {
  69.                 f.Expression = functionBox.Text;
  70.             }
  71.             catch (Exception ex)
  72.             {
  73.                 ErrorLbl.Text = ex.Message;
  74.                 return;
  75.             }
  76.             _drawer.AddFunction(f);
  77.             ErrorLbl.Text = string.Empty;
  78.             UpdateScreen();
  79.         }
  80.  
  81.         private void DrawClick(object sender, EventArgs e)
  82.         {
  83.             _drawer.ClearFunctionList();
  84.             var f = new Function();
  85.             try
  86.             {
  87.                 f.Expression = functionBox.Text;
  88.             }
  89.             catch (Exception ex)
  90.             {
  91.                 ErrorLbl.Text = ex.Message;
  92.                 return;
  93.             }
  94.             _drawer.AddFunction(f);
  95.             ErrorLbl.Text = string.Empty;
  96.             UpdateScreen();
  97.         }
  98.  
  99.         private void AddClick(object sender, EventArgs e)
  100.         {
  101.             var f = new Function();
  102.             try
  103.             {
  104.                 f.Expression = functionBox.Text;
  105.             }
  106.             catch (Exception ex)
  107.             {
  108.                 ErrorLbl.Text = ex.Message;
  109.                 return;
  110.             }
  111.             _drawer.AddFunction(f);
  112.             ErrorLbl.Text = string.Empty;
  113.             UpdateScreen();
  114.         }
  115.  
  116.         private void UpdateScreen()
  117.         {
  118.             DrawGraph();
  119.             DrawFunctions();
  120.  
  121.             _formGraphics.DrawImageUnscaledAndClipped(_buffer, ClientRectangle);
  122.         }
  123.  
  124.         private void DrawFunctions()
  125.         {
  126.             _graphics = Graphics.FromImage(_buffer);
  127.             _graphics.SmoothingMode = SmoothingMode.AntiAlias;
  128.  
  129.             _drawer.DrawFunctions(_graphics);
  130.         }
  131.  
  132.         private void DrawGraph()
  133.         {
  134.             _graphics = Graphics.FromImage(_buffer);
  135.             _graphics.SmoothingMode = SmoothingMode.HighSpeed;
  136.  
  137.             _drawer.DrawGraph(_graphics);
  138.         }
  139.  
  140.         private void Zoom(object sender, MouseEventArgs e)
  141.         {
  142.             _drawer.Zoom(e.Delta);
  143.             UpdateScreen();
  144.         }
  145.  
  146.         private void gridBox_CheckedChanged(object sender, EventArgs e)
  147.         {
  148.             _drawer.DrawGrid = gridBox.Checked;
  149.             UpdateScreen();
  150.         }
  151.  
  152.         private void ticksBox_CheckedChanged(object sender, EventArgs e)
  153.         {
  154.             _drawer.DrawTicks = ticksBox.Checked;
  155.             UpdateScreen();
  156.         }
  157.  
  158.         private void axesBox_CheckedChanged(object sender, EventArgs e)
  159.         {
  160.             _drawer.DrawAxes = axesBox.Checked;
  161.             UpdateScreen();
  162.         }
  163.  
  164.         private void controlCheck_CheckedChanged(object sender, EventArgs e)
  165.         {
  166.             foreach (Control control in Controls)
  167.             {
  168.                 if (control.Name != controlCheck.Name)
  169.                     control.Visible = controlCheck.Checked;
  170.             }
  171.             Update(); // clean controls
  172.             UpdateScreen();
  173.         }
  174.  
  175.         private void colorBtn_Click(object sender, EventArgs e)
  176.         {
  177.             using (var colorDlg = new ColorDialog())
  178.             {
  179.                 colorDlg.AllowFullOpen = true;
  180.                 colorDlg.AnyColor = true;
  181.                 colorDlg.SolidColorOnly = false;
  182.  
  183.                 if (colorDlg.ShowDialog() == DialogResult.OK)
  184.                 {
  185.                     _drawer.FunctionPen = new Pen(colorDlg.Color, 2);
  186.                 }
  187.                 UpdateScreen();
  188.             }
  189.         }
  190.  
  191.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  192.         {
  193.             _drawer.FunctionPen = new Pen(Color.FromName(comboBox1.SelectedItem.ToString()), 2);
  194.             UpdateScreen();
  195.             functionBox.Focus(); // set the focus to the textBox to prevent scrolling
  196.         }
  197.  
  198.         private void LoadComboBox<T>(ComboBox comboBox, IEnumerable<T> collection)
  199.         {
  200.             foreach (T item in collection)
  201.             {
  202.                 comboBox1.Items.Add(item);
  203.             }
  204.         }
  205.     }
  206. }