Advertisement
benshepherd

User Drawline C# Full

Dec 11th, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.98 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 Drawline
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         private int step;
  15.         private Point P1;
  16.         private Point P2;
  17.  
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.             this.MouseDown +=new MouseEventHandler(Form1_MouseDown);
  22.         }
  23.  
  24.         private void Form1_Load(object sender, EventArgs e)
  25.         {
  26.             Reset();
  27.         }
  28.  
  29.         private void Form1_MouseDown(object sender, MouseEventArgs e)
  30.         {
  31.             step++;
  32.  
  33.             switch (step)
  34.             {
  35.                 case 1:
  36.                     P1 = e.Location;
  37.                     ToolStatus("Click to select point 2");
  38.                     break;
  39.                 case 2:
  40.                     P2 = e.Location;
  41.                     break;
  42.             }
  43.  
  44.             if (step == 2)
  45.             {
  46.                 StartDrawing();
  47.                 Reset();
  48.             }            
  49.         }
  50.  
  51.         private void StartDrawing()
  52.         {
  53.             using (Graphics g = this.CreateGraphics())
  54.             {
  55.                 g.DrawLine(new Pen(Color.Red), P1, P2);
  56.             }
  57.         }
  58.  
  59.         private void Reset()
  60.         {
  61.             ToolStatus("Click to select point 1");
  62.             step = 0;
  63.             P1 = P2 = new Point(0, 0);
  64.         }
  65.  
  66.         private void ToolStatus(string value)
  67.         {
  68.             Invoke((Action)(() =>
  69.             {
  70.                 txtStatus.Text = value;
  71.             }));
  72.         }
  73.     }
  74.  
  75.     partial class Form1
  76.     {
  77.         /// <summary>
  78.         /// Required designer variable.
  79.         /// </summary>
  80.         private System.ComponentModel.IContainer components = null;
  81.  
  82.         /// <summary>
  83.         /// Clean up any resources being used.
  84.         /// </summary>
  85.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  86.         protected override void Dispose(bool disposing)
  87.         {
  88.             if (disposing && (components != null))
  89.             {
  90.                 components.Dispose();
  91.             }
  92.             base.Dispose(disposing);
  93.         }
  94.  
  95.         #region Windows Form Designer generated code
  96.  
  97.         /// <summary>
  98.         /// Required method for Designer support - do not modify
  99.         /// the contents of this method with the code editor.
  100.         /// </summary>
  101.         private void InitializeComponent()
  102.         {
  103.             this.toolStrip1 = new System.Windows.Forms.ToolStrip();
  104.             this.txtStatus = new System.Windows.Forms.ToolStripLabel();
  105.             this.toolStrip1.SuspendLayout();
  106.             this.SuspendLayout();
  107.             //
  108.             // toolStrip1
  109.             //
  110.             this.toolStrip1.Dock = System.Windows.Forms.DockStyle.Bottom;
  111.             this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
  112.             this.txtStatus});
  113.             this.toolStrip1.Location = new System.Drawing.Point(0, 337);
  114.             this.toolStrip1.Name = "toolStrip1";
  115.             this.toolStrip1.Size = new System.Drawing.Size(434, 25);
  116.             this.toolStrip1.TabIndex = 0;
  117.             this.toolStrip1.Text = "toolStrip1";
  118.             //
  119.             // txtStatus
  120.             //
  121.             this.txtStatus.Name = "txtStatus";
  122.             this.txtStatus.Size = new System.Drawing.Size(86, 22);
  123.             this.txtStatus.Text = "toolStripLabel1";
  124.             //
  125.             // Form1
  126.             //
  127.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  128.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  129.             this.ClientSize = new System.Drawing.Size(434, 362);
  130.             this.Controls.Add(this.toolStrip1);
  131.             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  132.             this.MaximizeBox = false;
  133.             this.MinimizeBox = false;
  134.             this.Name = "Form1";
  135.             this.ShowIcon = false;
  136.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  137.             this.Text = "Drawing Line";
  138.             this.Load += new System.EventHandler(this.Form1_Load);
  139.             this.toolStrip1.ResumeLayout(false);
  140.             this.toolStrip1.PerformLayout();
  141.             this.ResumeLayout(false);
  142.             this.PerformLayout();
  143.  
  144.         }
  145.  
  146.         #endregion
  147.  
  148.         private System.Windows.Forms.ToolStrip toolStrip1;
  149.         private System.Windows.Forms.ToolStripLabel txtStatus;
  150.     }
  151.  
  152.     static class Program
  153.     {
  154.         /// <summary>
  155.         /// The main entry point for the application.
  156.         /// </summary>
  157.         [STAThread]
  158.         static void Main()
  159.         {
  160.             Application.EnableVisualStyles();
  161.             Application.SetCompatibleTextRenderingDefault(false);
  162.             Application.Run(new Form1());
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement