?? mainform.cs
字號:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
namespace HandDrawer
{
public partial class MainForm : Form
{
/// <summary>
/// 存放所有的線條。
/// </summary>
/// <remarks>
/// 圖片是由很多條線構成的。每條線又是由鼠標經過的很多點構成的。
/// 因此一個List<Point>表示一條線。List<List<Point>>就表示所有的線條。
/// </remarks>
private List<List<Point>> m_lines;
/// <summary>
/// 存放新的一條線。
/// </summary>
private List<Point> m_newLine;
public MainForm()
{
InitializeComponent();
m_lines = new List<List<Point>>();
}
private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
// 鼠標按下的時候新建一個線條。
m_newLine = new List<Point>();
}
private void MainForm_MouseMove(object sender, MouseEventArgs e)
{
// 將經過的點添加到當前線條列表,并連接。
m_addPoint(e.X, e.Y);
}
private void MainForm_MouseUp(object sender, MouseEventArgs e)
{
// 將經過的點添加到當前線條列表,并連接。
m_addPoint(e.X, e.Y);
// 將線條添加到所有線條列表。
m_lines.Add(m_newLine);
}
private void MainForm_Paint(object sender, PaintEventArgs e)
{
// 重繪所有線條。
m_drawAllLines(e.Graphics);
}
private void m_mnuClear_Click(object sender, EventArgs e)
{
// 首先清除所有線條中的點。
foreach(List<Point> line in m_lines)
line.Clear();
// 清除所有線條。
m_lines.Clear();
// 重繪整個窗體。
this.Invalidate();
}
private void m_mnuSave_Click(object sender, EventArgs e)
{
// 顯示對話框,獲取用戶要保存的文件名。
if(DialogResult.OK != m_saveFileDialog.ShowDialog())
return;
// 所需的資源
Bitmap bmp = null; // 待保存的圖片
Graphics g = null; // 從圖片獲得的繪圖表面
Brush b = null; // 用于繪制背景色的畫刷
try
{
// 創建或獲取所需的資源。
bmp = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
g = Graphics.FromImage(bmp);
b = new SolidBrush(Color.White);
// 首先把背景填充為白色。
g.FillRectangle(b, this.ClientRectangle);
// 繪制所有的線條。
m_drawAllLines(g);
// 保存位圖。
bmp.Save(m_saveFileDialog.FileName, ImageFormat.Bmp);
}
catch(Exception ex)
{
MessageBox.Show(
String.Format("Cannot save the picture, detailes: {0}", ex.Message),
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Hand,
MessageBoxDefaultButton.Button1);
}
finally
{
// 清理已經獲取的資源。
if(bmp != null)
bmp.Dispose();
if(g != null)
g.Dispose();
if(b != null)
b.Dispose();
}
}
/// <summary>
/// 向當前線條中添加一個點,并將其與線條中的最后一個點連接起來。
/// </summary>
/// <param name="x">新添加的點的橫坐標。</param>
/// <param name="y">新添加的點的縱坐標。</param>
private void m_addPoint(int x, int y)
{
// 將經過的點添加到線條。
m_newLine.Add(new Point(x, y));
// 繪制線段,連接當前線條的最后一點和新經過的這一點。
int points = m_newLine.Count;
if(points > 1)
{
Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.Black);
// m_newLine[points - 2]是原線條最后一點
// m_newLine[points - 1]是新添加的點
g.DrawLine(
p,
m_newLine[points - 2].X, m_newLine[points - 2].Y,
m_newLine[points - 1].X, m_newLine[points - 1].Y);
g.Dispose();
p.Dispose();
}
}
/// <summary>
/// 用來繪制所有的線條。用于 1.窗體重繪 2.保存時繪制到位圖。
/// </summary>
/// <param name="g">用于繪圖的Graphics。</param>
private void m_drawAllLines(Graphics g)
{
Pen p = new Pen(Color.Black);
int totalLines = m_lines.Count;
for(int i = 0; i < totalLines; i++)
{
g.DrawLines(p, m_lines[i].ToArray());
}
p.Dispose();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -