现在一个小问题
我将画的线存起来,一次性画出来,想实现就是连续画线的功能,并且能够随时取到里面的点,然后进行点与点之间的捕捉
我现在程序的问题是在画布上,连线条都没显示出来,不知道什么原因
在winform里面的
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D ;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DrawWall
{
public partial class DrawWall : Form
{
bool isDrawLine = false;//标志是否进入画线状态
PointF first;
PointF start;
public DrawWall()
{
InitializeComponent();
}
private void InitCanvas(PictureBox pCanvas)
{
int canvasHeight = pCanvas.Height;
int canvasWidth = pCanvas.Width;
int space = 30;
Bitmap bitMap = new Bitmap(pCanvas.Width, pCanvas.Height);
Canvas.DrawCanvas(canvasWidth, canvasHeight, bitMap, space);
pCanvas.Image = bitMap;
}
private void DrawWall_Load(object sender, EventArgs e)
{
InitCanvas(pCanvas);
}
private void Draw_Click(object sender, EventArgs e)
{
if (isDrawLine)
{
isDrawLine = false;
}
else
{
isDrawLine = true;
}
}
private void pCanvas_MouseClick(object sender, MouseEventArgs e)
{
//按下鼠标时发生
if (e.Button == MouseButtons.Left)
{
if (Vector.ListPt.Count == 0 && isDrawLine)
{
first = e.Location;
start = e.Location;
Vector.SavePoint(start);
}
}
}
private void pCanvas_Paint(object sender, PaintEventArgs e)
{
}
private void pCanvas_MouseMove(object sender, MouseEventArgs e)
{
if (isDrawLine)
{
Vector.PointCurrent = e.Location;
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = pCanvas.CreateGraphics();
DrawTools.DrawAllLines(g);
}
}
}
自己定义的类
Canvas.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace DrawWall
{
public class Canvas
{
#region 创建画布
/// <summary>
/// 创建画布,画网格
/// </summary>
/// <param name="width">画布的宽度</param>
/// <param name="height">画布的高度</param>
/// <param name="bitMap">画布的位图</param>
/// <param name="space">网格线的间距</param>
public static void DrawXY(float width, float height, Bitmap bitMap, int space)
{
int penWidth = 1;
Pen penCenter = new Pen(Color.Red, penWidth);
Graphics g = Graphics.FromImage(bitMap);
Pen penXY = new Pen(Color.Black, penWidth);
float y = 0;
float x = 0;
for (int i = 0; i < height / space; i++)
{
PointF pt1 = new PointF(0, y + i * space);
PointF pt2 = new PointF(width, y + i * space);
float flag = Math.Abs(i * space) - (height / 2);
// if ((flag >= 0 && flag < space/2) || flag <= 0 && flag > -space/2)
if (Math.Abs(flag) < space / 2)
{
g.DrawLine(penCenter, pt1, pt2);
}
else
g.DrawLine(penXY, pt1, pt2);
}
for (int i = 0; i < width / space; i++)
{
PointF pt3 = new PointF(x + i * space, 0);
PointF pt4 = new PointF(x + i * space, height);
float flag = Math.Abs(i * space) - (width / 2);
//if ((flag >= 0 && flag < space/2) || flag <= 0 && flag > -space/2)
if (Math.Abs(flag) < space / 2)
{
g.DrawLine(penCenter, pt3, pt4);
}
else
{
g.DrawLine(penXY, pt3, pt4);
}
}
g.Dispose();
penCenter.Dispose();
penXY.Dispose();
}
public static void DrawCanvas(int canvasWidth, int canvasHeight, Bitmap bitMap, int space)
{
DrawXY(canvasWidth, canvasHeight, bitMap, space);
}
#endregion
}
}
DrawTools.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace DrawWall
{
class DrawTools
{
public static void DrawAllLines(Graphics g)
{
if (Vector.ListPt.Count != 0)
{
Pen pen = new Pen(Color.Blue, 1);
g.DrawLines(pen, Vector.ListPt.ToArray());
g.DrawLine(pen, Vector.GetLastPoint(), Vector.PointCurrent);
}
}
}
}
Vector.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace DrawWall
{
class Vector
{
private static PointF pointCurrent;
public static PointF PointCurrent
{
get { return pointCurrent; }
set { pointCurrent = value; }
}
private static List<PointF> listPt;
public static List<PointF> ListPt
{
get { return listPt; }
set { listPt = value; }
}
public static void SavePoint(PointF pf)
{
listPt.Add(pf);
}
public static PointF GetLastPoint()
{
if (listPt.Count == 0)
return pointCurrent;
return listPt[listPt.Count - 1];
}
}
}
求高手老师指点