?? drawlinesform.cs
字號:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace DrawLines
{
/// <summary>
/// 一家日企的面試題目之三
/// 完成下面的程序,功能要求為:
/// (1)可以用鼠標(biāo)自由的在屏幕上面畫出一段折線(要求用橡皮筋“技術(shù)),如
/// 折線相交,彈出提示框,要求重新取點(diǎn)(該點(diǎn)不作為折線上的點(diǎn))
/// (2)在上面折線的兩側(cè),以一定的距離畫出與之平行的折線
/// </summary>
public class DrawLinesForm : System.Windows.Forms.Form
{
/// <summary>
/// 必需的設(shè)計(jì)器變量。
/// </summary>
private System.ComponentModel.Container components = null;
private bool isMouseDown = false; //記錄鼠標(biāo)是否已按下。
private bool ptEndIsNull = true; //記錄ptEnd是否無效。當(dāng)鼠標(biāo)鍵剛按下而鼠標(biāo)沒有移動時應(yīng)是無效的。
private Point ptBegin; //記錄新畫線的起點(diǎn)。
private Point ptEnd; //記錄新畫線的終點(diǎn)。
private ArrayList listLines; //記錄所有已畫的線。
public DrawLinesForm()
{
//
// Windows 窗體設(shè)計(jì)器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
listLines = new ArrayList();
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
//
// DrawLinesForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(656, 381);
this.Name = "DrawLinesForm";
this.Text = "畫線程序";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.DrawLinesForm_MouseDown);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.DrawLinesForm_MouseUp);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.DrawLinesForm_Paint);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.DrawLinesForm_MouseMove);
}
#endregion
/// <summary>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </summary>
static void Main()
{
Application.Run(new DrawLinesForm());
}
private void DrawLinesForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics dc = this.CreateGraphics();
Pen pen = new Pen(this.ForeColor, 1);
IEnumerator pos = listLines.GetEnumerator();
while(pos.MoveNext())
{
Line line = (Line)pos.Current;
line.Draw(dc, pen);
}
}
private void DrawLinesForm_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
//鼠標(biāo)已按下,記錄線段起點(diǎn)。
isMouseDown = true;
ptBegin = new Point(e.X, e.Y);
}
private void DrawLinesForm_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (isMouseDown)
{
//鼠標(biāo)已按下,若ptEnd有效則擦去舊線重繪新線,以實(shí)現(xiàn)橡皮筋技術(shù)。
Graphics dc = this.CreateGraphics();
Pen pen = new Pen(this.ForeColor, 1);
if (!ptEndIsNull)
{
pen.Color = this.BackColor;
dc.DrawLine(pen, ptBegin, ptEnd);
pen.Color = this.ForeColor;
}
ptEnd = new Point(e.X, e.Y);
ptEndIsNull = false;
dc.DrawLine(pen, ptBegin, ptEnd);
}
}
private void DrawLinesForm_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
//鼠標(biāo)鍵彈起,擦去橡皮筋所畫的線。判斷是否有線相交,若無則
//用Line類繪制新線,并將新線記錄。
isMouseDown = false;
ptEndIsNull = true;
ptEnd = new Point(e.X, e.Y);
Pen pen = new Pen(this.BackColor, 1);
Graphics dc = this.CreateGraphics();
dc.DrawLine(pen, ptBegin, ptEnd);
Line line = new Line(ptBegin, ptEnd);
if (IsCutWithOldLines(line))
{
MessageBox.Show("折線不能相交!", "提示");
DrawLinesForm_Paint(null, null);
}
else
{
pen.Color = this.ForeColor;
line.Draw(dc, pen);
listLines.Add(line);
}
}
/// <summary>
/// 判斷新線是否與已記錄的舊線相交。
/// </summary>
/// <param name="newLine">新繪制的線。</param>
/// <returns>若相交,返回真。否則返回假。</returns>
private bool IsCutWithOldLines(Line newLine)
{
IEnumerator pos = listLines.GetEnumerator();
while(pos.MoveNext())
{
Line line = (Line)pos.Current;
if (newLine.IsCutWithLine(line))
return true;
}
return false;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -