Dominikis
Dominikis

Reputation: 67

Getting mouse position according to form in c#

I am devoloping app in Visual Studio using C#. I set no border in my form and I want to make my form dragable.

enter image description here

I need to get position of mouse according to form to get my code work

enter image description here

This is my code to make it dragable

private void timer1_Tick(object sender, EventArgs e)
{
    //HERE CODE SET POSITION OF MY FORM
    this.Location = new Point(MousePosition.X - MOUSE POSITION ACCORDING TO FORM, MousePosition.Y - MOUSE POSITION ACCORDING TO FORM);
}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    timer1.Enabled = true;
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    timer1.Enabled = false;
}

Your answers will be appreciated.

Upvotes: 0

Views: 1384

Answers (1)

NineBerry
NineBerry

Reputation: 28499

Use the PointToClient() method of the form.

Upvotes: 2

Related Questions