benshepherd icon

User Drawline C# Full

benshepherd | PRO | 12/11/12 08:22:48 PM UTC | 0 ⭐ | 318 👁️ | Never ⏰ | []
C# |

4.98 KB

|

None

|

0 👍

/

0 👎

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace Drawline
{
    public partial class Form1 : Form
    {
        private int step;
        private Point P1;
        private Point P2;
 
        public Form1()
        {
            InitializeComponent();
            this.MouseDown +=new MouseEventHandler(Form1_MouseDown);
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            Reset();
        }
 
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            step++;
 
            switch (step)
            {
                case 1:
                    P1 = e.Location;
                    ToolStatus("Click to select point 2");
                    break;
                case 2:
                    P2 = e.Location;
                    break;
            }
 
            if (step == 2)
            {
                StartDrawing();
                Reset();
            }            
        }
 
        private void StartDrawing()
        {
            using (Graphics g = this.CreateGraphics())
            {
                g.DrawLine(new Pen(Color.Red), P1, P2);
            }
        }
 
        private void Reset()
        {
            ToolStatus("Click to select point 1");
            step = 0;
            P1 = P2 = new Point(0, 0);
        }
 
        private void ToolStatus(string value)
        {
            Invoke((Action)(() =>
            {
                txtStatus.Text = value;
            }));
        }
    }
 
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.txtStatus = new System.Windows.Forms.ToolStripLabel();
            this.toolStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // toolStrip1
            // 
            this.toolStrip1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.txtStatus});
            this.toolStrip1.Location = new System.Drawing.Point(0, 337);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Size = new System.Drawing.Size(434, 25);
            this.toolStrip1.TabIndex = 0;
            this.toolStrip1.Text = "toolStrip1";
            // 
            // txtStatus
            // 
            this.txtStatus.Name = "txtStatus";
            this.txtStatus.Size = new System.Drawing.Size(86, 22);
            this.txtStatus.Text = "toolStripLabel1";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(434, 362);
            this.Controls.Add(this.toolStrip1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "Form1";
            this.ShowIcon = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Drawing Line";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.toolStrip1.ResumeLayout(false);
            this.toolStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();
 
        }
 
        #endregion
 
        private System.Windows.Forms.ToolStrip toolStrip1;
        private System.Windows.Forms.ToolStripLabel txtStatus;
    }
 
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Comments