0

How i can set starting point of coordinate system in top left corner of Form ? Because it set starting point on top left corner of monitor not form but I need starting point in top left corner of Form..

Here is code what i'm try to do:

int x, y;
string _x, _y;

private void GetCursor()
        {
            _x = MousePosition.X.ToString();
            x = int.Parse(_x);
            label2.Text = _x;
            _y = MousePosition.Y.ToString();
            y = int.Parse(_y);
            label4.Text = _y;
        }

        private void MoveButton()
        {
            button1.Location = new Point(x,y); 

        }
    private void timer1_Tick(object sender, EventArgs e)
    {
        GetCursor();
        MoveButton();
    }   

Thanks.

Hury H
  • 31
  • 7

2 Answers2

0

You can use the Control.PointToClient method

Point localPoint = myForm.PointToClient(Cursor.Position);
label2.Text = localPoint.X.ToString();
label4.Text = localPoint.Y.ToString();
keyboardP
  • 68,824
  • 13
  • 156
  • 205
0

First Add Class (Win32.cs)

public class Win32
{ 
    [DllImport("User32.Dll")]
    public static extern long SetCursorPos(int x, int y);

    [DllImport("User32.Dll")]
    public static extern bool ClientToScreen(IntPtr hWnd, ref POINT point);

    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int x;
        public int y;
    }
}

Then Call it From event :

Win32.POINT p = new Win32.POINT();
p.x = Convert.ToInt16(txtMouseX.Text);
p.y = Convert.ToInt16(txtMouseY.Text);

Win32.ClientToScreen(this.Handle, ref p);
Win32.SetCursorPos(p.x, p.y);

With txtMouseX and txtMouseY is custom parameter. I think this should at (0, 0).

Ave
  • 4,338
  • 4
  • 40
  • 67