1

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?

Luc Morin
  • 5,302
  • 20
  • 39
Fylps
  • 13
  • 4
  • Where do you get Fg from? – DavidG Dec 03 '15 at 00:32
  • Sorry about that Fg is the instance of my graphics that was declared in my form: – Fylps Dec 03 '15 at 00:35
  • Fg is the instance of my graphics class that I created in my form so basically (within the form) Fg=this.createGraphics(); and it is used as the parameter for my method afterwards. – Fylps Dec 03 '15 at 00:36
  • When you say wrong spot, can you be more specific? – DavidG Dec 03 '15 at 00:38
  • Lets say i click in the middle of the Form it will put the line an inch lower to the right, but the change is relatively random – Fylps Dec 03 '15 at 00:39
  • Are you drawing on the form, or on a control? – John Alexiou Dec 03 '15 at 00:41
  • The drawing is done directly on the form(which contains a menu bar if that changes anything) – Fylps Dec 03 '15 at 00:43
  • 1
    Note that [MousePosition](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseposition(v=vs.110).aspx) returns the result in screen (not form) relative coordinates. – 500 - Internal Server Error Dec 03 '15 at 00:56
  • Yeah I realised that but I couldnt find another way of getting any type of coordinate. – Fylps Dec 03 '15 at 00:58
  • convert the MousePosition to control position using form.PointToClient(...) – smertrios Dec 03 '15 at 01:05
  • The problem is I cant seem to be able to access PointToClient() within my method. I try to call Form1.PointToClient but get nothing, only option that makes sense in those given is MousePosition. – Fylps Dec 03 '15 at 01:07
  • Also your Form1 object is the default instance of your form, it is **not** the form you are looking at, you will need to pass in your actual form object. – Mark Hall Dec 03 '15 at 01:08
  • Ok thats what ive seen but cant seem to understand, how can I get any type of handle for a form that is only create at the beginning of the program? – Fylps Dec 03 '15 at 01:10
  • Your MouseDown event handler's `e` argument has information about the coordinate of the event in its properties `X` and `Y`. – 500 - Internal Server Error Dec 03 '15 at 01:14
  • Just add another parameter to your method, either pass in the Form instance or the mouse coordinates like @500-InternalServerError suggests. – Mark Hall Dec 03 '15 at 01:16
  • I know i could do that but I specified that the method could only take the instance of Graphics as parameter no other. Also how would i access the event handler e from within the method of my class? – Fylps Dec 03 '15 at 01:22
  • you said that the method is "called everytime there is a click" on the form. so why can't you pass it the click event args? It'd probably help if you give us more code context. – smertrios Dec 03 '15 at 01:42
  • I am restricted by a rule where as the method from my class can only accept as a parameter the Grahphics Fg, nothing else and the drawing of the line must be done within this method. – Fylps Dec 03 '15 at 01:48
  • Is this class initialized in your form? If so is it permissible with the restrictions that you are specifying to create a constructor and pass in the instance of the owner/parent form to it when your class is initialized. That way your class will have knowledge of your Form. – Mark Hall Dec 03 '15 at 01:50
  • I think that is allowed how would go about doing that? – Fylps Dec 03 '15 at 01:51

1 Answers1

1

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);
    }
}
John Alexiou
  • 28,472
  • 11
  • 77
  • 133
  • Thanks so much for the help I never completely understood how to use the current form! Great help from all!!! – Fylps Dec 03 '15 at 02:09