📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ch11_5
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private Boolean fMouseDown;
private ArrayList fPoints;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
fPoints = new ArrayList();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(824, 621);
this.Name = "Form1";
this.Text = "Form1";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnMouseDown);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.OnMouseUp);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnMouseMove);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
fMouseDown = true;
// Clear the array of points
fPoints.Clear();
// Add a new point
Point p = new Point();
p.X = e.X;
p.Y = e.Y;
fPoints.Add( p );
}
private void OnMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if ( fMouseDown )
{
// Add a new point to the array
Point p = new Point();
p.X = e.X;
p.Y = e.Y;
fPoints.Add( p );
// And redraw
this.Refresh();
}
}
private void OnMouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Add this point
Point p = new Point();
p.X = e.X;
p.Y = e.Y;
fPoints.Add( p );
// Redraw
this.Refresh();
// And stop collecting points
fMouseDown = false;
}
private void OnPaint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// Move to the first point
System.Collections.IEnumerator myEnumerator = fPoints.GetEnumerator();
Pen pen1 = new Pen(Color.FromArgb(150, Color.Purple));
if ( myEnumerator.MoveNext() )
{
Point p1 = (Point)myEnumerator.Current;
while ( myEnumerator.MoveNext() )
{
Point p2 = (Point)myEnumerator.Current;
e.Graphics.DrawLine(pen1, p1, p2);
p1 = p2;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -