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 { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// 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 { /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }