编写一个简易画图程序,可以使用.NET C#中的Windows Forms技术来实现。代码示例如下所示:
using System;
using System.Drawing;
using System.Windows.Forms;
public class SimpleDrawingProgram : Form
{
private Point currentPoint;
private Pen pen;
public SimpleDrawingProgram()
{
// 设置窗口标题
Text = "简易画图程序";
// 创建画笔并初始化其属性
pen = new Pen(Color.Black, 3);
pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
// 设置窗口大小及背景颜色
Size = new Size(600, 400);
BackColor = Color.White;
// 注册鼠标事件处理方法
MouseDown += SimpleDrawingProgram_MouseDown;
MouseMove += SimpleDrawingProgram_MouseMove;
MouseUp += SimpleDrawingProgram_MouseUp;
}
private void SimpleDrawingProgram_MouseDown(object sender, MouseEventArgs e)
{
currentPoint = e.Location;
}
private void SimpleDrawingProgram_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
using (Graphics graphics = CreateGraphics())
{
graphics.DrawLine(pen, currentPoint, e.Location);
}
currentPoint = e.Location;
}
}
private void SimpleDrawingProgram_MouseUp(object sender, MouseEventArgs e)
{
currentPoint = Point.Empty;
}
static void Main()
{
Application.Run(new SimpleDrawingProgram());
}
}
下面展示一个简单画图程序的基本功能,包含 选择颜色,铅笔,直线,矩形,椭圆,文字,橡皮等功能
鼠标触摸事件:
private void fMain_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//如果选择文字输入,则打开strInput窗体
if (tool == drawTools.String)
{
fTxtCon inputBox = new fTxtCon();
inputBox.StartPosition = FormStartPosition.CenterParent;
if (inputBox.ShowDialog() == DialogResult.OK)
{
Graphics g = this.CreateGraphics();
Font theFont = this.Font;
g.DrawString(inputBox.ztxt.Text, theFont, new SolidBrush(foreColor), e.X, e.Y);
ig.DrawString(inputBox.ztxt.Text, theFont, new SolidBrush(foreColor), e.X, e.Y);
}
}
//如果开始绘制,则开始记录鼠标位置
else if ((isDrawing = !isDrawing) == true)
{
startPoint = new Point(e.X, e.Y);
oldPoint = new Point(e.X, e.Y);
}
}
}
private void fMain_MouseMove(object sender, MouseEventArgs e)
{
Graphics g;
g = this.CreateGraphics();
if (isDrawing)
{
switch (tool)
{
case drawTools.None:
break;
case drawTools.Pen:
//从上一个点到当前点绘制线段
g.DrawLine(new Pen(foreColor, 1), oldPoint, new Point(e.X, e.Y));
ig.DrawLine(new Pen(foreColor, 1), oldPoint, new Point(e.X, e.Y));
oldPoint.X = e.X;
oldPoint.Y = e.Y;
break;
case drawTools.Line:
//首先恢复此次操作之前的图像,然后再添加Line
this.fMain_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
g.DrawLine(new Pen(foreColor, 1), startPoint, new Point(e.X, e.Y));
break;
case drawTools.Ellipse:
//首先恢复此次操作之前的图像,然后再添加Ellipse
this.fMain_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
g.DrawEllipse(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
break;
case drawTools.Rectangle:
//首先恢复此次操作之前的图像,然后再添加Rectangle
this.fMain_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
g.DrawRectangle(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
break;
case drawTools.String:
break;
case drawTools.Rubber:
//用背景色绘制宽线段
g.DrawLine(new Pen(backColor, 20), oldPoint, new Point(e.X, e.Y));
ig.DrawLine(new Pen(backColor, 20), oldPoint, new Point(e.X, e.Y));
oldPoint.X = e.X;
oldPoint.Y = e.Y;
break;
}
}
}
private void fMain_MouseUp(object sender, MouseEventArgs e)
{
isDrawing = false;
switch (tool)
{
case drawTools.Line:
ig.DrawLine(new Pen(foreColor, 1), startPoint, new Point(e.X, e.Y));
break;
case drawTools.Ellipse:
ig.DrawEllipse(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
break;
case drawTools.Rectangle:
ig.DrawRectangle(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
break;
}
}
private void fMain_Paint(object sender, PaintEventArgs e)
{
//将Image中保存的图像,绘制出来
Graphics g = this.CreateGraphics();
if (img != null)
{
g.Clear(Color.White);
g.DrawImage(img, this.ClientRectangle);
}
}