PDA

View Full Version : حرفه ای: ترسیم خط موازی



saeidmscs
دوشنبه 06 خرداد 1398, 15:04 عصر
سلام دوستان
نوشتن ترسیم خط معمولی، وقتی موس روی فرم حرکت میکنه که خیلی ساده اس.
مساله من اونجایی که وقتی حرکت موس، یه خط دوتایی (موازی با فاصله خیلی نزدیک بهم) رسم بشه.

چنین کدی رو تو MSDN پیدا کردم اما جواب نمیده:

public partial class Form1 : Form
{
private GraphicsPath _gPath = null;
private List<Point> _pts = null;
private bool _tracking = false;
private Graphics _fgx = null;

public Form1()
{
InitializeComponent();
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (_gPath != null)
{
_gPath.Dispose();
_gPath = null;
}

if (_pts != null)
_pts.Clear();
else
_pts = new List<Point>();

_gPath = new GraphicsPath();
_pts.Add(new Point(e.X, e.Y));
_fgx = this.CreateGraphics();
_tracking = true;
}
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (_tracking)
{
_fgx.DrawLine(Pens.Blue, _pts[_pts.Count - 1], new Point(e.X, e.Y));
_pts.Add(new Point(e.X, e.Y));

}
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (_tracking)
{
_fgx.DrawLine(Pens.Blue, _pts[_pts.Count - 1], new Point(e.X, e.Y));
_fgx.Dispose();
_fgx = null;
_pts.Add(new Point(e.X, e.Y));

_gPath.AddLines(_pts.ToArray());
this.Invalidate();
}
_tracking = false;
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
if (_gPath != null)
{
e.Graphics.DrawPath(Pens.Blue, _gPath);
GraphicsPath fPath = (GraphicsPath)_gPath.Clone();
fPath.Transform(new Matrix(1, 0, 0, 1, 100, 0));
e.Graphics.DrawPath(Pens.Red, fPath);
fPath.Dispose();
}
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (_gPath != null)
_gPath.Dispose();
}

private void Form1_Load(object sender, EventArgs e)
{

}
}


ممنون میشم کمک کنید