Drawing Rectangles with drag mouse c# - c#

I want to draw a rectangle, triangle, circle and a pie form graphic using draw and drag with the mouse (not filled).
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;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.IO;
namespace mainpaint
{
public partial class Form1 : Form
{
Color paintcolor;
bool choose = false;
bool draw = false;
int x, y, lx, ly = 0;
Item currItem;
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
this.Cursor = System.Windows.Forms.Cursors.Cross;
}
public enum Item
{
Rectangle, Ellipse, Line,Pencil,eraser, Triangle
}
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
choose = true;
}
private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
{
choose = false;
}
private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
if (choose)
{
Bitmap bmp = (Bitmap)pictureBox2.Image.Clone();
paintcolor = bmp.GetPixel(e.X, e.Y);
red.Value = paintcolor.R;
green.Value = paintcolor.G;
blue.Value = paintcolor.B;
alpha.Value = paintcolor.A;
redval.Text = paintcolor.R.ToString();
greenval.Text = paintcolor.G.ToString();
blueval.Text = paintcolor.B.ToString();
alphaval.Text = paintcolor.A.ToString();
pictureBox3.BackColor = paintcolor;
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
draw = true;
x = e.X;
y = e.Y;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
draw = false;
lx = e.X;
ly = e.Y;
if (currItem == Item.Line)
{
Graphics g = pictureBox1.CreateGraphics();
g.DrawLine(new Pen(new SolidBrush(paintcolor),3), new Point(x, y), new Point(lx, ly));
g.Dispose();
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (draw)
{
Graphics g = pictureBox1.CreateGraphics();
switch (currItem)
{
case Item.Rectangle:
g.DrawRectangle(new Pen(new SolidBrush(paintcolor),3), x, y, e.X - x, e.Y - y);
// g.FillRectangle(new SolidBrush(paintcolor), x, y, e.X - x, e.Y - y); these is to draw a filled rectangle not a problem
break;
case Item.Ellipse:
g.DrawEllipse(new Pen(new SolidBrush(paintcolor), 3), x, y, e.X - x, e.Y - y);
//g.FillEllipse(new SolidBrush(paintcolor), x, y, e.X - x, e.Y - y);
break;
case Item.Pencil:
g.FillEllipse(new SolidBrush(paintcolor), e.X - x + x, e.Y - y + y, Convert.ToInt32(toolStripTextBox1.Text), Convert.ToInt32(toolStripTextBox1.Text));
break;
case Item.eraser:
g.FillEllipse(new SolidBrush(pictureBox1.BackColor), e.X - x + x, e.Y - y + y, Convert.ToInt32(toolStripTextBox1.Text), Convert.ToInt32(toolStripTextBox1.Text));
break;
case Item.Triangle:
g.FillPolygon(new SolidBrush(paintcolor), new Point[3] { new Point(x-e.X,y+e.Y), new Point(x,y), new
Point(x+e.X,y+e.Y) });
break;
}
g.Dispose();
}
}
private void toolStripButton9_Click(object sender, EventArgs e)
{
currItem = Item.Rectangle;
}
private void toolStripButton10_Click(object sender, EventArgs e)
{
currItem = Item.Ellipse;
}
private void toolStripButton13_Click(object sender, EventArgs e)
{
currItem = Item.Pencil;
}
private void toolStripButton15_Click(object sender, EventArgs e)
{
currItem = Item.eraser;
}
private void toolStripButton11_Click(object sender, EventArgs e)
{
currItem = Item.Line;
}
private void toolStripButton6_Click(object sender, EventArgs e)
{
currItem = Item.Triangle;
}
private void Form1_Load(object sender, EventArgs e)
{
FontFamily[] family = FontFamily.Families;
}
private void red_Scroll(object sender, EventArgs e)
{
paintcolor = Color.FromArgb(alpha.Value, red.Value, green.Value, blue.Value);
pictureBox3.BackColor = paintcolor;
redval.Text = "R: " + paintcolor.R.ToString();
}
private void green_Scroll(object sender, EventArgs e)
{
paintcolor = Color.FromArgb(alpha.Value, red.Value, green.Value, blue.Value);
pictureBox3.BackColor = paintcolor;
greenval.Text = "G: " + paintcolor.G.ToString();
}
private void blue_Scroll(object sender, EventArgs e)
{
paintcolor = Color.FromArgb(alpha.Value, red.Value, green.Value, blue.Value);
pictureBox3.BackColor = paintcolor;
blueval.Text = "B: " + paintcolor.B.ToString();
}
private void alpha_Scroll(object sender, EventArgs e)
{
paintcolor = Color.FromArgb(alpha.Value, red.Value, green.Value, blue.Value);
pictureBox3.BackColor = paintcolor;
alphaval.Text = "A: " + paintcolor.A.ToString();
}
}
}
Im having these results:
Rectangles and Circles:
And my triangle doesnt even have a form (im using the filling method here whith polygon,the draw method should be the same?)
And i want something like this:
And finally, how can I draw a pie form figure?
Thank you!

Related

C# Graphics class want to make signature panel : input from drawing tablet

i am trying to make signature panel in c# windowsform application where input is from drawing tablet
my code as below this code working for line drawing not dot created.
So please suggest how dot and line both are create.
{
Graphics graphics;
Boolean cusorMoving = false;
Pen cursorPen;
int cursorX = -1;
int cursorY = -1;
public SignPad()
{
InitializeComponent();
graphics = panel2.CreateGraphics();
cursorPen = new Pen(Color.Black, 2);
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
cursorPen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
cursorPen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
}
Mouse Down event
private void panel2_MouseDown(object sender, MouseEventArgs e)
{
cusorMoving = true;
cursorX = e.X;
cursorY = e.Y;
}
private void panel2_MouseUp(object sender, MouseEventArgs e)
{
cusorMoving = false;
cursorX = -1;
cursorY = -1;
}
Mouse Move event
private void panel2_MouseMove(object sender, MouseEventArgs e)
{
if (cursorX != -1 && cursorY != -1 && cusorMoving == true)
{
graphics.DrawLine(cursorPen, new Point(cursorX, cursorY), e.Location);
cursorX = e.X;
cursorY = e.Y;
}
}
You need to store individual points in a collection and draw them separately in the Paint handler. Every time you add a point to the collection, you also need to tell the panel to draw the area where the new segment was added. Something like this:
using System.Collections.Generic;
using System.Drawing;
namespace Lines
{
public partial class SignPad : Form
{
Pen cursorPen = SystemPens.ControlText;
List<Point> points = new List<Point>();
bool cursorMoving = false;
public SignPad()
{
InitializeComponent();
cursorPen = new Pen(Color.Black, 2);
cursorPen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
cursorPen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
}
private void panel2_Paint(object? sender, PaintEventArgs e)
{
var g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
for (int i = 1; i < points.Count; ++i)
g.DrawLine(cursorPen, points[i - 1], points[i]);
}
private void panel2_MouseDown(object? sender, MouseEventArgs e)
{
if (!cursorMoving)
{
cursorMoving = true;
points.Clear();
points.Add(e.Location);
panel2.Invalidate();
}
}
private void panel2_MouseMove(object? sender, MouseEventArgs e)
{
if (cursorMoving && points.Count > 0)
{
var p = e.Location;
var q = points[points.Count - 1];
var r = Rectangle.FromLTRB(Math.Min(p.X, q.X), Math.Min(p.Y, q.Y), Math.Max(p.X, q.X), Math.Max(p.Y, q.Y));
r = Rectangle.Inflate(r, (int)cursorPen.Width, (int)cursorPen.Width);
points.Add(p);
panel2.Invalidate(r);
}
}
private void panel2_MouseUp(object? sender, MouseEventArgs e)
{
cursorMoving = false;
}
}
}
Don't forget to add the Paint handler the same way you added MouseMove, MouseDown and MouseUp handlers - in the Designer.

The image is supposed to be moved inside the PictureBox via MouseMove events

Before starting a project, I want to know beforehand whether the following would work.
The application creates a System.Drawing.Bitmap and paints on it.
The Bitmap is much larger than the PictureBox. The Height of the Bitmap and the PictureBox are the same. Now I would like to be able to move the Image from left to right within (along) the PictureBox by pressing the left mouse button and moving it.
See the drawing:
I just realized that I haven't answered yet, which I am trying to make up for.
As a solution, I use Jimi's solution. I've shortened the code here to meet my needs.
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;
namespace Zoom_an_image_from_the_mouse_location
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
string imagePath = "C:\\Users\\yourPath\\Pictures\\....jpeg";
drawingImage = (Bitmap)Image.FromStream(new MemoryStream(File.ReadAllBytes(imagePath)));
imageRect = new RectangleF(Point.Empty, drawingImage.Size);
canvas = new PictureBoxEx(new Size(525,700));
canvas.Location = new Point(10, 10);
canvas.MouseMove += this.canvas_MouseMove;
canvas.MouseDown += this.canvas_MouseDown;
canvas.MouseUp += this.canvas_MouseUp;
canvas.Paint += this.canvas_Paint;
this.Controls.Add(canvas);
}
private void FormMain_Load(object sender, EventArgs e)
{
this.BackColor = Color.FromArgb(174, 184, 177);
}
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
}
private float rotationAngle = 0.0f;
private float zoomFactor = 1.0f;
private RectangleF imageRect = RectangleF.Empty;
private PointF imageLocation = PointF.Empty;
private PointF mouseLocation = PointF.Empty;
private Bitmap drawingImage = null;
private PictureBoxEx canvas = null;
private void canvas_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
mouseLocation = e.Location;
imageLocation = imageRect.Location;
canvas.Cursor = Cursors.NoMove2D;
}
private void canvas_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
imageRect.Location =
new PointF(imageLocation.X + (e.Location.X - mouseLocation.X),
imageLocation.Y); //+ (e.Location.Y - mouseLocation.Y));
canvas.Invalidate();
}
private void canvas_MouseUp(object sender, MouseEventArgs e) =>
canvas.Cursor = Cursors.Default;
private void canvas_Paint(object sender, PaintEventArgs e)
{
var drawingRect = GetDrawingImageRect(imageRect);
using (var mxRotation = new Matrix())
using (var mxTransform = new Matrix())
{
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
mxRotation.RotateAt(rotationAngle, GetDrawingImageCenterPoint(drawingRect));
mxTransform.Multiply(mxRotation);
e.Graphics.Transform = mxTransform;
e.Graphics.DrawImage(drawingImage, drawingRect);
}
}
#region Drawing Methods
public RectangleF GetScaledRect(RectangleF rect, float scaleFactor) =>
new RectangleF(rect.Location,
new SizeF(rect.Width * scaleFactor, rect.Height * scaleFactor));
public RectangleF GetDrawingImageRect(RectangleF rect) =>
GetScaledRect(rect, zoomFactor);
public PointF GetDrawingImageCenterPoint(RectangleF rect) =>
new PointF(rect.X + rect.Width / 2f, rect.Y + rect.Height / 2f);
#endregion
}
}
[DesignerCategory("Code")]
public class PictureBoxEx : PictureBox
{
public PictureBoxEx() : this(new Size(525, 700)) { }
public PictureBoxEx(Size size)
{
SetStyle(ControlStyles.Selectable | ControlStyles.UserMouse, true);
this.BorderStyle = BorderStyle.FixedSingle;
this.Size = size;
}
}

What is best way to write in windows forms with pen-tablet like wacom intuos in C#

i have a problem.
I'm writing a program that writes on it with a stylus.
First, i create a windows form with a panel.
second, this code:
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.Diagnostics;
using System.Drawing.Drawing2D;
namespace testWrite
{
public partial class Form1 : Form
{
Graphics g;
int x = -1;
int y = -1;
bool moving = false;
Pen pen;
public Form1()
{
InitializeComponent();
g = panel1.CreateGraphics();
pen = new Pen(Color.Black, 5);
pen.SetLineCap(System.Drawing.Drawing2D.LineCap.Round, System.Drawing.Drawing2D.LineCap.Round, System.Drawing.Drawing2D.DashCap.Round);
pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
g.DrawLine(pen, new Point(x, y), e.Location);
x = e.X;
y = e.Y;
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
x = -1;
y = -1;
moving = false;
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
x = e.X;
y = e.Y;
moving = true;
}
}
}
I use this app with a Wacom intuos
But the result is not so good because a few words are lost...haizzz
toi tên la trần
quang hieu
hello heloo
especially, when i write fast or the text is small.
when i write in Microsoft Paint, it is very good
What is best way to to write in windows forms with pen-tablet like wacom intuos?
UPDATE 1:
With cmt from TaW.
Thanks for your help. But, that's not what I need...
i was change my code to:
public partial class Form1 : Form
{
List<Point> curPoints = new List<Point>();
List<List<Point>> allPoints = new List<List<Point>>();
public Form1()
{
InitializeComponent();
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
// here we should check if the distance is more than a minimum!
curPoints.Add(e.Location);
// let it show
panel1.Invalidate();
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
if (curPoints.Count > 1)
{
// ToList creates a copy
allPoints.Add(curPoints.ToList());
curPoints.Clear();
}
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (curPoints.Count > 1)
{
// begin fresh line or curve
curPoints.Clear();
// startpoint
curPoints.Add(e.Location);
}
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
// here you can use DrawLines or DrawCurve
// current line
if (curPoints.Count > 1) e.Graphics.DrawCurve(Pens.Red, curPoints.ToArray());
// other lines or curves
foreach (List<Point> points in allPoints)
if (points.Count > 1) e.Graphics.DrawCurve(Pens.Red, points.ToArray());
}
}
But nothing better. The result is worse...
I tried to write: "Hello my name is Hieu", but is not run...
Looks like a pen-tablet differs from a mouse when use to write. Because, with mouse i feel that is better in this code...
UPDATE 2:
With code by Idle_Mind. It will be fine if i set pen-tablet:
With setting "Click", it is not OK
How to fix it, i don't want to set "Double Click" to my pen !
Here's my version...worked great for me. You might need to adjust your tablet settings so that it picks up everything correctly:
public partial class FormTablet : Form
{
private Point lastPoint;
private GraphicsPath GP = null;
private List<GraphicsPath> GPs = new List<GraphicsPath>();
public FormTablet()
{
InitializeComponent();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
lastPoint = new Point(e.X, e.Y);
GP = new GraphicsPath();
GP.AddLine(lastPoint, lastPoint);
GPs.Add(GP);
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point pt = new Point(e.X, e.Y);
GP.AddLine(lastPoint, pt);
lastPoint = pt;
pictureBox1.Invalidate();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
GP = null;
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (checkBox1.Checked)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
}
using(Pen p = new Pen(Color.Black, (int)numericUpDown1.Value))
{
p.LineJoin = LineJoin.Round;
p.MiterLimit = p.Width / 2;
foreach (GraphicsPath path in GPs)
{
if (path.PathPoints.Count() > 2)
{
// draw the path
e.Graphics.DrawPath(p, path);
}
else
{
// just draw a single dot
Rectangle rc = new Rectangle(Point.Round(path.PathPoints[0]), new Size(1, 1));
rc.Inflate((int)numericUpDown1.Value, (int)numericUpDown1.Value);
e.Graphics.FillEllipse(Brushes.Black, rc);
}
}
}
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
private void button1_Click(object sender, EventArgs e)
{
GPs.Clear();
pictureBox1.Invalidate();
}
}

Why are my graphics not appearing on the panel c#

The code displayed below is used for drawing different shapes, the problem that i am facing is that when i use the same code to display the drawings on the windows form it displays,,,but when i try to display them on a panel..it shows as blank during runtime..please have a look at the code and tell me where i am going wrong. Thank in advance to whoever replies to this post!
private ArrayList ds;
private Point mold;
private Point mcurr;
private int mshape;
private float mwidth;
private Color mcolor;
public Form1()
{
InitializeComponent();
ds = new ArrayList();
mshape = 0;
//mwidth = 1;
//mcolor = Color.Black;
DoubleBuffered = true;
}
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.SelectionFont = new Font("Arial", 9);
richTextBox1.SelectionBullet = true;
richTextBox1.SelectionColor = Color.Red;
}
private Rectangle rec(Point p1, Point p2)
{
Rectangle a = new Rectangle();
a.X = (p1.X > p2.X ? p2.X : p1.X);
a.Y = (p1.Y > p2.Y ? p2.Y : p1.Y);
a.Width = Math.Abs(p1.X - p2.X);
a.Height = Math.Abs(p1.Y - p2.Y);
return a;
}
private void draw(Graphics e, Point mold, Point mcur, int mshape, float mwidth, Color mcolor)
{
Pen p = new Pen(mcolor, mwidth);
switch (mshape)
{
case 0:
e.DrawRectangle(p, rec(mold, mcur));
break;
case 1:
e.DrawEllipse(p, rec(mold, mcur));
break;
case 2:
e.DrawLine(p, mold, mcur);
break;
case 3:
//this.Invalidate();
break;
}
}
private void toolStripButtonRectangle_Click(object sender, EventArgs e)
{
mshape = 0;
}
private void toolStripButtonEllipse_Click(object sender, EventArgs e)
{
mshape = 1;
}
private void toolStripButtonLine_Click(object sender, EventArgs e)
{
mshape = 2;
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
panel1.Cursor = Cursors.Cross;
if (e.Button == MouseButtons.Left)
{
this.mold = e.Location;
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
panel1.Cursor = Cursors.Default;
Class1 a = new Class1(mold, mcur, mshape, mwidth, mcolor);/// create a class
ds.Add(a);
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mcur = e.Location;
Invalidate();
}
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
foreach (Class1 a in ds)
{
draw(e.Graphics, a.old, a.cur, a.shape, a.width, a.color);
}
draw(e.Graphics, mold, mcur, mshape, mwidth, mcolor);
}
private void btn_Clear_Click(object sender, EventArgs e)
{
Graphics erase = panel1.CreateGraphics();
erase.Clear(panel1.BackColor);
// g1.Clear(Color.Transparent);
}
}
}

How to draw a line on panel overlapped by other panel?

IDE: visual studio, c#, Windows from application
I am trying to draw a line on a panel. I am able to draw line on panel1 by clicking on it.
//Code
public partial class Form1 : Form
{
static int px=5, py=5;
public Form1()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Red, 5, 5, px, py);
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
initilizeXY(e.X, e.Y);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
}
private void initilizeXY( int pxx, int pyy)
{
px = pxx;
py = pyy;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
panel1.Refresh();
}
private void panel2_MouseDown(object sender, MouseEventArgs e)
{
initilizeXY(e.X, e.Y);
}
}
// by this code i am able to draw a line on mouse down on panel 1.
but due to some requirement changes there is another panel ( panel2) which is partially overlapping panel1.
Now i want to draw the same line on panel1 if the user click on panel1 or panel2.
Please suggest how make this work done?
This code works:
Point bgn1 = new Point(5, 5);
Point end1 = new Point(5, 5);
Point bgn2 = new Point(0, 0);
Point end2 = new Point(0, 0);
private void Form1_Load(object sender, EventArgs e)
{
Point pnt, pntscr;
pnt.X = 5;
pnt.Y = 5;
pntscr = Panel1.PointToScreen(pnt);
bgn2 = Panel2.PointToClient(pntscr);
end2 = bgn2;
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
end1.X = e.X;
end1.Y = e.Y;
Point pntscr;
pntscr = Panel1.PointToScreen(end1);
end2 = Panel2.PointToClient(pntscr);
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
panel1.Refresh();
panel2.Refresh();
}
private void panel2_MouseDown(object sender, MouseEventArgs e)
{
end2.X = e.X;
end2.Y = e.Y;
Point pntscr;
pntscr = Panel2.PointToScreen(end2);
end1 = Panel1.PointToClient(pntscr);
}
private void panel2_MouseUp(object sender, MouseEventArgs e)
{
panel1.Refresh();
panel2.Refresh();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Red, bgn1, end1);
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Red, bgn2, end2);
}
valter
EDIT: Fixed the code to work properly.
Tested in a scenario when panel2 overlaps panel1 on the right side of panel1, starting a little lower than panel1's top.
Writing this bit of code I assumed panel2's X and Y coords are greater than panel1's. Perhaps you should reverse the offset calculation if it's the other way around.
public partial class Form1 : Form
{
static int px = 5, py = 5;
static int p2x = 0, p2y = 0;
int offsetX;
int offsetY;
public Form1()
{
InitializeComponent();
offsetX = panel2.Location.X - panel1.Location.X;
offsetY = panel2.Location.Y - panel1.Location.Y;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Red, 5, 5, px, py);
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
initilizeXY(e.X, e.Y);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void initilizeXY(int pxx, int pyy)
{
px = pxx;
py = pyy;
}
private void initilizeXY2(int pxx, int pyy)
{
p2x = pxx;
p2y = pyy;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
panel1.Refresh();
panel2.Refresh();
}
private void panel2_MouseDown(object sender, MouseEventArgs e)
{
initilizeXY(e.X+offsetX, e.Y+offsetY);
initilizeXY2(e.X, e.Y);
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
if (px > offsetX && py > offsetY)
{
int p2start = findIntersectFromLineEquation(new Point(5, 5), new Point(px, py));
e.Graphics.DrawLine(Pens.Red, 0, p2start - offsetY, p2x, p2y);
}
}
private int findIntersectFromLineEquation(Point start, Point end)
{
if (start.X == end.X || start.Y == end.Y)
return 0;
double a = (double)(end.Y - start.Y) / (double)(end.X - start.X);
double b = (double)(start.Y) - (double)(a * start.X);
return (int)(a * offsetX + b);
}
}
Remember to subscribe the events accordingly.

Categories

Resources