Basically I have created a Class with a method that is called everytime there is a click on my form(it is supposed to draw a single line where I clicked) it goes as follows:
public void Dessiner(Graphics Fg)
{
Point p = Form1.MousePosition;
Fg.DrawLine(MyPen,p.X,p.Y,p.X+2,p.Y+2);
}
The problem is when I call this method within my Forms' mousedown event it places the line at the wrong spot everytime.
Notes: the method can only take the graphics Fg, and the drawing of the line MUST be done within the method of the class.
What am I doing wrong?
You need to transform the coordinates with PointToClient()
public partial class Form1 : Form
{
DrawingHelper dh;
public Form1()
{
InitializeComponent();
dh=new DrawingHelper(this);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
dh.Desser(this.CreateGraphics());
}
}
public class DrawingHelper
{
Form form;
public DrawingHelper(Form form)
{
this.form =form;
}
public void Desser(Graphics Fg)
{
var pt=form.PointToClient(Form.MousePosition);
Fg.DrawLine(Pens.Black, pt.X,pt.Y, pt.X+2, pt.Y+2);
}
}