CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2008
    Posts
    63

    Exclamation Change the mouse cursor location dynamically

    Hi,
    I want to restrict the mouse cursor movement only on Grid points. Grid Point may be float or int value. I have tried in the way i have added in code block, but even for int value it is not working. Is there any way to change mouse cursor dynamically to int or float values.

    for eg;

    The mouse should move in 2.5 multiples. It can be like points specified below

    100,50 102.5,50
    100,52.5 102.5,52.5


    I have try to implement it like this

    Code:
     if (!this.Cursor.Equals(Cursors.Default))
     {
          this.Cursor = new Cursor(Cursor.Current.Handle);
          Cursor.Position = new Point(CalValue.X, CalValue.X); // CalValue can be of Int or Float
          Cursor.Clip = new Rectangle(this.Location, this.Size);
      }
    Thanks

  2. #2
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Change the mouse cursor location dynamically

    Quote Originally Posted by hotsaravana View Post
    Code:
     if (!this.Cursor.Equals(Cursors.Default))
     {
          this.Cursor = new Cursor(Cursor.Current.Handle);
          Cursor.Position = new Point(CalValue.X, CalValue.X); // CalValue can be of Int or Float
          Cursor.Clip = new Rectangle(this.Location, this.Size);
      }
    why are you passing only X values in both the paramaters ?

  3. #3
    Join Date
    Sep 2008
    Posts
    63

    Re: Change the mouse cursor location dynamically

    Quote Originally Posted by vcdebugger View Post
    why are you passing only X values in both the paramaters ?
    Sorry I have posted wrongly...
    if (!this.Cursor.Equals(Cursors.Default))
    {
    this.Cursor = new Cursor(Cursor.Current.Handle);
    Cursor.Position = new Point(CalcValue.X , CalcValue.Y); // CalcValue can be float or int
    Cursor.Clip = new Rectangle(this.Location, this.Size);
    }


    When CalcValue is in float I want Cursor.Position value to be in float. But cursor.Position does not accept float value.

    Is there any other way to do so?

  4. #4
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Change the mouse cursor location dynamically

    There are no half pixels. That is why points are integer values. You need to round the values to the integer value you want, then set the cursor position

  5. #5
    Join Date
    Sep 2008
    Posts
    63

    Re: Change the mouse cursor location dynamically

    Quote Originally Posted by sotoasty View Post
    There are no half pixels. That is why points are integer values. You need to round the values to the integer value you want, then set the cursor position
    My Graphics unit is in millimeter, Not in Pixel.Whether it is possible to set float value to cursor position.

  6. #6
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Change the mouse cursor location dynamically

    Code:
    Cursor.Position = new Point(CalcValue.X , CalcValue.Y); // CalcValue can be float or int
    Points are in integers. You can not use a float value.

  7. #7
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Change the mouse cursor location dynamically

    Quote Originally Posted by hotsaravana View Post
    My Graphics unit is in millimeter, Not in Pixel.Whether it is possible to set float value to cursor position.
    try to map the small millimeter units to pixels because thats the smallest level possible.

  8. #8
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Change the mouse cursor location dynamically

    If you want to constrain cursor movement to whatever location, you could use the SetCapture ReleaseCapture ClipCursor APIs, or the Cursor.Clip property to constrain your cursor to any place.

  9. #9
    Join Date
    Sep 2008
    Posts
    63

    Re: Change the mouse cursor location dynamically

    Quote Originally Posted by HanneSThEGreaT View Post
    If you want to constrain cursor movement to whatever location, you could use the SetCapture ReleaseCapture ClipCursor APIs, or the Cursor.Clip property to constrain your cursor to any place.

    Code:
     protected override void OnMouseMove(MouseEventArgs e)
            {
                PointF[] MouseMove = new PointF[1];
                MouseMove[0].X = e.X;
                MouseMove[0].Y = e.Y;
                Graphics g = this.CreateGraphics();
                g.PageUnit = GraphicsUnit.Millimeter;
                g.TransformPoints(CoordinateSpace.Device, CoordinateSpace.Page, MouseMove);
                MouseMove[0].X = MouseMove[0].X / 2.5f;
                MouseMove[0].Y = MouseMove[0].Y / 2.5f;
                Point truncatePt = Point.Truncate(MouseMove[0]);
                MouseMove[0].X = truncatePt.X * 2.5f;
                MouseMove[0].Y = truncatePt.Y * 2.5f;
                this.Cursor = new Cursor(Cursor.Current.Handle);
                //Convert Milleter to Pixel 
                Point pp = new Point((int)(MouseMove[0].X * g.DpiX / 25.4f), (int)(MouseMove[0].Y * g.DpiY / 25.4f));
                Cursor.Position = pp;
                Cursor.Clip = new Rectangle(this.Location, this.Size);
                base.OnMouseMove(e);
            }

    I have tried as above. But the control is not moving to the child form , it switch in the parent form.

    Whether it is possible to change cursor on Mouse move using SetCapture, ReleaseCapture and ClipCursor APIs, if so please provide a sample code.....

    Thank You

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured