你是要简单的绘图程序?呵呵我这这几天正好做这个 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace Drawing { public partial class Form1 : Form { string BoolStyle; Line lineObj; Circle circleObj; Rectangle rectangleObj; RectCircle rectcircleOjt; Color[] DrawColor; Color currentcolor; bool c; List<Line> listline = new List<Line>(); List<Circle> listcircle = new List<Circle>(); List<Rectangle> listrectangle = new List<Rectangle>(); List<RectCircle> listrectcircle = new List<RectCircle>(); public Form1() { currentcolor = Color.Black; DrawColor = new Color[8]; DrawColor[0] = Color.Brown; DrawColor[1] = Color.Red; DrawColor[2] = Color.Blue; DrawColor[3] = Color.Orange; DrawColor[4] = Color.Green; DrawColor[5] = Color.Yellow; DrawColor[6] = Color.White; DrawColor[7] = Color.Black; InitializeComponent(); } private void Btn1_Click(object sender, EventArgs e) { BoolStyle = "line"; } private void Btn2_Click(object sender, EventArgs e) { BoolStyle = "circle"; } private void Btn3_Click(object sender, EventArgs e) { BoolStyle = "rectangle"; } private void Btn4_Click(object sender, EventArgs e) { BoolStyle = "rectcircle"; } private void panel1_MouseDown(object sender, MouseEventArgs e) { c = true; switch (BoolStyle) { case "line": lineObj = new Line(); lineObj.SetPoint1(e.Location); listline.Add(lineObj); lineObj.StrokeColor = currentcolor; break; case "circle": circleObj = new Circle(); circleObj.SetCenterPoint(e.Location); listcircle.Add(circleObj); circleObj.StrokeColor = currentcolor; break; case "rectangle": rectangleObj = new Rectangle(); rectangleObj.SetLeftTop(e.Location); listrectangle.Add(rectangleObj); rectangleObj.StrokeColor = currentcolor; break; case "rectcircle": rectcircleOjt = new RectCircle(); listrectcircle.Add(rectcircleOjt); rectcircleOjt.SetLeftTop(e.Location); rectcircleOjt.StrokeColor = currentcolor; break; default: break; } } private void panel1_MouseMove(object sender, MouseEventArgs e) { if (c == true) { switch (BoolStyle) { case "line": lineObj.SetPoint2(e.Location); break; case "circle": circleObj.SetRadius(e.Location); break; case "rectangle": rectangleObj.SetRightBottom(e.Location); break; case "rectcircle": rectcircleOjt.SetRightBottom(e.Location); break; default: break; } } panel1.Refresh(); } private void panel1_MouseUp(object sender, MouseEventArgs e) { c = false; switch (BoolStyle) { case "line": lineObj = null; break; case "circle": circleObj = null; break; case "rectangle": rectangleObj = null; break; case"rectcircle": rectcircleOjt = null; break; default: break; } } private void panel1_Paint(object sender, PaintEventArgs e) { /* if (x) { switch (BoolStyle) { case "line": lineObj.Draw(e.Graphics); break; case "circle": circleObj.Draw(e.Graphics); break; case "rectangle": rectangleObj.Draw(e.Graphics); break; default : break; } }*/ Graphics g = e.Graphics; for (int i=0;i<listline.Count;i++) listline[i].Draw(g);
foreach(Circle circle in listcircle) { circle.Draw(e.Graphics); } foreach(Rectangle rectangle in listrectangle) { rectangle.Draw(e.Graphics); } foreach (RectCircle rectcircle in listrectcircle) { rectcircle.Draw(e.Graphics); } } private void panel2_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(new SolidBrush(currentcolor), 20, 0, 30, 20); g.DrawRectangle(new Pen(Color.Black), 20, 0, 30, 20); int x=80; for (int i=0;i<DrawColor.Length;i++) { g.FillRectangle(new SolidBrush(DrawColor[i]), x, 0, 30, 20); g.DrawRectangle(new Pen(Color.Black), 20, 0, 30, 20); x += 30; }
} /* private void panel2_Click(object sender, EventArgs e) { int x = e.X; int y = e.Y; if (x>80&&y<30) { int colorindex = (x - 80) / 30; if (colorindex < DrawColor.Length) { currentcolor = DrawColor[colorindex]; } } panel2.Refresh(); }*/ private void panel2_MouseClick(object sender, MouseEventArgs e) { int x = e.X; int y = e.Y; if (x > 80 && y < 30) { int colorindex = (x - 80) / 30; if (colorindex < DrawColor.Length) { currentcolor = DrawColor[colorindex]; } } panel2.Refresh(); } }}
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing;namespace Drawing { /// <summary> /// 直线对象 /// </summary> class Line { private int x1; private int y1; private int x2; private int y2; // 线条宽度 private int strokeWidth; public int StrokeWidth { get { return strokeWidth; } set { strokeWidth = value; } } // 线条颜色 private Color strokeColor; public Color StrokeColor { get { return strokeColor; } set { strokeColor = value; } } public Line() { StrokeWidth = 1; StrokeColor = Color.Black; } public void Draw(Graphics graphics) { graphics.DrawLine(new Pen(StrokeColor, StrokeWidth), x1, y1, x2, y2); } public void SetPoint1(Point point) { x1 = point.X; y1 = point.Y; } public void SetPoint2(Point point) { x2 = point.X; y2 = point.Y; } } } 这是line.cs,上面那个是form.cs
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms;namespace Drawing { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace Drawing
{
public partial class Form1 : Form
{
string BoolStyle;
Line lineObj;
Circle circleObj;
Rectangle rectangleObj;
RectCircle rectcircleOjt;
Color[] DrawColor;
Color currentcolor;
bool c;
List<Line> listline = new List<Line>();
List<Circle> listcircle = new List<Circle>();
List<Rectangle> listrectangle = new List<Rectangle>();
List<RectCircle> listrectcircle = new List<RectCircle>();
public Form1()
{
currentcolor = Color.Black;
DrawColor = new Color[8];
DrawColor[0] = Color.Brown;
DrawColor[1] = Color.Red;
DrawColor[2] = Color.Blue;
DrawColor[3] = Color.Orange;
DrawColor[4] = Color.Green;
DrawColor[5] = Color.Yellow;
DrawColor[6] = Color.White;
DrawColor[7] = Color.Black;
InitializeComponent();
} private void Btn1_Click(object sender, EventArgs e)
{
BoolStyle = "line";
} private void Btn2_Click(object sender, EventArgs e)
{
BoolStyle = "circle";
} private void Btn3_Click(object sender, EventArgs e)
{
BoolStyle = "rectangle";
}
private void Btn4_Click(object sender, EventArgs e)
{
BoolStyle = "rectcircle";
} private void panel1_MouseDown(object sender, MouseEventArgs e)
{
c = true;
switch (BoolStyle)
{
case "line":
lineObj = new Line();
lineObj.SetPoint1(e.Location);
listline.Add(lineObj);
lineObj.StrokeColor = currentcolor;
break;
case "circle":
circleObj = new Circle();
circleObj.SetCenterPoint(e.Location);
listcircle.Add(circleObj);
circleObj.StrokeColor = currentcolor;
break;
case "rectangle":
rectangleObj = new Rectangle();
rectangleObj.SetLeftTop(e.Location);
listrectangle.Add(rectangleObj);
rectangleObj.StrokeColor = currentcolor;
break;
case "rectcircle":
rectcircleOjt = new RectCircle();
listrectcircle.Add(rectcircleOjt);
rectcircleOjt.SetLeftTop(e.Location);
rectcircleOjt.StrokeColor = currentcolor;
break;
default:
break; }
} private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (c == true)
{
switch (BoolStyle)
{
case "line":
lineObj.SetPoint2(e.Location);
break;
case "circle":
circleObj.SetRadius(e.Location);
break;
case "rectangle":
rectangleObj.SetRightBottom(e.Location);
break;
case "rectcircle":
rectcircleOjt.SetRightBottom(e.Location);
break;
default:
break; }
}
panel1.Refresh();
} private void panel1_MouseUp(object sender, MouseEventArgs e)
{
c = false; switch (BoolStyle)
{
case "line": lineObj = null;
break;
case "circle":
circleObj = null;
break;
case "rectangle":
rectangleObj = null;
break;
case"rectcircle":
rectcircleOjt = null;
break;
default:
break; } } private void panel1_Paint(object sender, PaintEventArgs e)
{
/* if (x)
{
switch (BoolStyle)
{
case "line":
lineObj.Draw(e.Graphics);
break;
case "circle":
circleObj.Draw(e.Graphics);
break;
case "rectangle":
rectangleObj.Draw(e.Graphics);
break;
default :
break; }
}*/
Graphics g = e.Graphics;
for (int i=0;i<listline.Count;i++)
listline[i].Draw(g);
foreach(Circle circle in listcircle)
{
circle.Draw(e.Graphics);
}
foreach(Rectangle rectangle in listrectangle)
{
rectangle.Draw(e.Graphics);
}
foreach (RectCircle rectcircle in listrectcircle)
{
rectcircle.Draw(e.Graphics);
}
} private void panel2_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(new SolidBrush(currentcolor), 20, 0, 30, 20);
g.DrawRectangle(new Pen(Color.Black), 20, 0, 30, 20);
int x=80;
for (int i=0;i<DrawColor.Length;i++)
{
g.FillRectangle(new SolidBrush(DrawColor[i]), x, 0, 30, 20);
g.DrawRectangle(new Pen(Color.Black), 20, 0, 30, 20);
x += 30;
}
} /* private void panel2_Click(object sender, EventArgs e)
{
int x = e.X;
int y = e.Y;
if (x>80&&y<30)
{
int colorindex = (x - 80) / 30;
if (colorindex < DrawColor.Length)
{
currentcolor = DrawColor[colorindex];
}
}
panel2.Refresh();
}*/ private void panel2_MouseClick(object sender, MouseEventArgs e)
{
int x = e.X;
int y = e.Y;
if (x > 80 && y < 30)
{
int colorindex = (x - 80) / 30;
if (colorindex < DrawColor.Length)
{
currentcolor = DrawColor[colorindex];
}
}
panel2.Refresh();
} }}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;namespace Drawing
{
/// <summary>
/// 直线对象
/// </summary>
class Line
{
private int x1;
private int y1;
private int x2;
private int y2; // 线条宽度
private int strokeWidth; public int StrokeWidth
{
get { return strokeWidth; }
set { strokeWidth = value; }
} // 线条颜色
private Color strokeColor; public Color StrokeColor
{
get { return strokeColor; }
set { strokeColor = value; }
} public Line()
{
StrokeWidth = 1;
StrokeColor = Color.Black;
} public void Draw(Graphics graphics)
{
graphics.DrawLine(new Pen(StrokeColor, StrokeWidth), x1, y1, x2, y2);
} public void SetPoint1(Point point)
{
x1 = point.X;
y1 = point.Y;
} public void SetPoint2(Point point)
{
x2 = point.X;
y2 = point.Y;
}
}
}
这是line.cs,上面那个是form.cs
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;namespace Drawing
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}