110,545
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace HandDrawing
{
public partial class HandDrawing : Form
{
public HandDrawing()
{
InitializeComponent();
m_lines = new List<List<Point>>();
}
private List<List<Point>> m_lines;
private List<Point> m_newLine;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
m_newLine = new List<Point>();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
m_addPoint(e.X, e.Y);
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
m_addPoint(e.X, e.Y);
m_lines.Add(m_newLine);
}
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);
g.DrawLine(p, m_newLine[points - 2].X, m_newLine[points - 2].Y, m_newLine[points - 1].X, m_newLine[points - 1].Y);
p.Dispose();
g.Dispose();
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
DrawAllLines(e.Graphics);
}
private void 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();
g.Dispose();
}
private void miClear_Click(object sender, EventArgs e)
{
//foreach (List<Point> l in m_lines)
// l.Clear();
m_lines.Clear();
this.Invalidate();
}
private void miSave_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() != DialogResult.OK)
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);
DrawAllLines(g);
bmp.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Cannot save picture,error message:{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();
}
}
private void miExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
我这里有个在窗体上可以写字的例子,不能保存
namespace WriteWords
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//MyPoint1, MyPoint2表示鼠标按下和弹起时鼠标的坐标位置
public Point MyPoint1, MyPoint2;
public int MyFlag = 0;
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
//当鼠标弹起时,设置MyFlag = 0,表示不能画线
this.MyFlag = 0;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
this.Text = "X=" + e.X.ToString() + ",Y=" + e.Y.ToString();
Graphics g = this.CreateGraphics();
Pen MyPen = new Pen(Color.Black);
//MyFlag=0表示鼠标弹起,不能进行画线
//当鼠标按下时,设置MyFlag=1表示可以画线
if (this.MyFlag == 0)
return;
//鼠标移动,每次变换时,MyPoint2都记录下鼠标的位置,以便进行鼠标移动画线
this.MyPoint2.X = e.X;
this.MyPoint2.Y = e.Y;
g.DrawLine(MyPen, MyPoint1.X, MyPoint1.Y, MyPoint2.X, MyPoint2.Y);
//当画完一条线后(很短的,可以当做一个小点看待),将MyPoint1的坐标重置为此时鼠标的位置
MyPoint1.X = e.X;
MyPoint1.Y = e.Y;
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
//鼠标第一次按下时,设置鼠标坐标为第一个点的坐标
this.MyFlag = 1;
this.MyPoint1.X = e.X;
this.MyPoint1.Y = e.Y;
}
}
}