Advertisement
csaki

Egérkezelés (Mouse handling)

Oct 22nd, 2013
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace mousehandling
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         Graphics g;
  15.         List<PointF> points;
  16.  
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.             g = CreateGraphics();
  21.             points = new List<PointF>();
  22.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  23.         }
  24.  
  25.         private void Form1_MouseClick(object sender, MouseEventArgs e)
  26.         {
  27.             points.Add(new PointF(e.X, e.Y));
  28.             DrawPoints();
  29.         }
  30.  
  31.         private void DrawPoints()
  32.         {
  33.             Invalidate();
  34.             Update();
  35.             foreach (var point in points)
  36.             {
  37.                 g.DrawEllipse(Pens.Black, point.X, point.Y, 10, 10);
  38.                 g.FillEllipse(Brushes.Red, point.X, point.Y, 10, 10);
  39.             }
  40.         }
  41.  
  42.         private void button1_Click(object sender, EventArgs e)
  43.         {
  44.             try
  45.             {
  46.                 Kirajzolas();
  47.             }
  48.             catch
  49.             {
  50.                 MessageBox.Show("Legalább két pontot tegyél!");
  51.             }
  52.  
  53.         }
  54.  
  55.         private void Kirajzolas()
  56.         {
  57.             Invalidate();
  58.             Update();
  59.             if (points.Count < 2)
  60.             {
  61.                 throw new Exception("Kevés pont!");
  62.             }
  63.             if (checkBox1.Checked) DrawPoints();
  64.             g.DrawCurve(new Pen(Color.Blue, 3), points.ToArray());
  65.         }
  66.  
  67.         private void button2_Click(object sender, EventArgs e)
  68.         {
  69.             Invalidate();
  70.             Update();
  71.             points.Clear();
  72.         }
  73.  
  74.  
  75.     //Kiegészítés trackbarokkal
  76.  
  77.         private TrackBar trackBar1;
  78.         private TrackBar trackBar2;
  79.  
  80.         private void trackBar1_Scroll(object sender, EventArgs e)
  81.         {
  82.             novekmeny = trackBar1.Value - elozoX;
  83.             elozoX = trackBar1.Value;
  84.             eltolX(novekmeny);
  85.             DrawPoints();
  86.             DrawCurve();
  87.         }
  88.  
  89.         private void eltolX(int novekmeny)
  90.         {
  91.             for (int i = 0; i < points.Count; i++)
  92.             {
  93.                 points[i] = new PointF(points[i].X + novekmeny, points[i].Y);
  94.             }
  95.         }
  96.  
  97.         private void trackBar2_Scroll(object sender, EventArgs e)
  98.         {
  99.             novekmeny = -trackBar2.Value - elozoY;
  100.             elozoY = -trackBar2.Value;
  101.             eltolY(novekmeny);
  102.             DrawPoints();
  103.             DrawCurve();
  104.         }
  105.  
  106.         private void eltolY(int novekmeny)
  107.         {
  108.             for (int i = 0; i < points.Count; i++)
  109.             {
  110.                 points[i] = new PointF(points[i].X, points[i].Y + novekmeny);
  111.             }
  112.         }
  113.  
  114.     // Forgatás (NE MÁSOLD, ÁTNEVEZTEM FÜGGVÉNYEKET ÉS VÁLTOZÓKAT, magyarról angolra)
  115.  
  116.     private TrackBar trackBar3;
  117.         float prevAlpha;
  118.  
  119.         private void trackBar3_Scroll(object sender, EventArgs e)
  120.         {
  121.             float alpha = (float)((trackBar3.Value - prevAlpha) * Math.PI / 180.0); // és így lett radián!
  122.             prevAlpha = trackBar3.Value;
  123.             Rotate(alpha);
  124.         }
  125.  
  126.         private void Rotate(float alpha)
  127.         {
  128.             float newX, newY;
  129.             for (int i = 0; i < points.Count; i++)
  130.             {
  131.                 int centerX = (int)points[points.Count / 2].X;
  132.                 int centerY = (int)points[points.Count / 2].Y;
  133.                 moveX(-centerX);
  134.                 moveY(-centerY);
  135.                 newX = (float)(points[i].X * Math.Cos(alpha) - points[i].Y * Math.Sin(alpha));
  136.                 newY = (float)(points[i].X * Math.Sin(alpha) + points[i].Y * Math.Cos(alpha));
  137.                 points[i] = new PointF(newX, newY);
  138.                 moveX(centerX);
  139.                 moveY(centerY);
  140.                 DrawPoints();
  141.                 DrawCurve();
  142.             }
  143.         }
  144.  
  145.  
  146.     // Designer kód
  147.         /// <summary>
  148.         /// Required designer variable.
  149.         /// </summary>
  150.         private System.ComponentModel.IContainer components = null;
  151.  
  152.         /// <summary>
  153.         /// Clean up any resources being used.
  154.         /// </summary>
  155.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  156.         protected override void Dispose(bool disposing)
  157.         {
  158.             if (disposing && (components != null))
  159.             {
  160.                 components.Dispose();
  161.             }
  162.             base.Dispose(disposing);
  163.         }
  164.  
  165.         #region Windows Form Designer generated code
  166.  
  167.         /// <summary>
  168.         /// Required method for Designer support - do not modify
  169.         /// the contents of this method with the code editor.
  170.         /// </summary>
  171.         private void InitializeComponent()
  172.         {
  173.             this.button1 = new System.Windows.Forms.Button();
  174.             this.button2 = new System.Windows.Forms.Button();
  175.             this.checkBox1 = new System.Windows.Forms.CheckBox();
  176.             this.SuspendLayout();
  177.             //
  178.             // button1
  179.             //
  180.             this.button1.Location = new System.Drawing.Point(12, 12);
  181.             this.button1.Name = "button1";
  182.             this.button1.Size = new System.Drawing.Size(75, 23);
  183.             this.button1.TabIndex = 0;
  184.             this.button1.Text = "Draw";
  185.             this.button1.UseVisualStyleBackColor = true;
  186.             this.button1.Click += new System.EventHandler(this.button1_Click);
  187.             //
  188.             // button2
  189.             //
  190.             this.button2.Location = new System.Drawing.Point(12, 41);
  191.             this.button2.Name = "button2";
  192.             this.button2.Size = new System.Drawing.Size(75, 23);
  193.             this.button2.TabIndex = 1;
  194.             this.button2.Text = "Reset";
  195.             this.button2.UseVisualStyleBackColor = true;
  196.             this.button2.Click += new System.EventHandler(this.button2_Click);
  197.             //
  198.             // checkBox1
  199.             //
  200.             this.checkBox1.AutoSize = true;
  201.             this.checkBox1.Checked = true;
  202.             this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked;
  203.             this.checkBox1.Location = new System.Drawing.Point(12, 70);
  204.             this.checkBox1.Name = "checkBox1";
  205.             this.checkBox1.Size = new System.Drawing.Size(87, 17);
  206.             this.checkBox1.TabIndex = 2;
  207.             this.checkBox1.Text = "Visible points";
  208.             this.checkBox1.UseVisualStyleBackColor = true;
  209.             //
  210.             // Form1
  211.             //
  212.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  213.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  214.             this.ClientSize = new System.Drawing.Size(691, 366);
  215.             this.Controls.Add(this.checkBox1);
  216.             this.Controls.Add(this.button2);
  217.             this.Controls.Add(this.button1);
  218.             this.Name = "Form1";
  219.             this.Text = "Form1";
  220.             this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);
  221.             this.ResumeLayout(false);
  222.             this.PerformLayout();
  223.  
  224.         }
  225.  
  226.         #endregion
  227.  
  228.         private System.Windows.Forms.Button button1;
  229.         private System.Windows.Forms.Button button2;
  230.         private System.Windows.Forms.CheckBox checkBox1;
  231.     }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement