views:

41

answers:

2

I have a graphical application in which I move a graphic object with the mouse.

In some conditions, the object stops moving. I need then to stop moving the mouse cursor too.

Is it possible? MousePosition property seems to be in ReadOnly.

Eg.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.X > 100)
        {
            Cursor.Position = new Point(100, Cursor.Position.Y);
        }
    }
}

EDIT, second version, works, but the cursor is not "stable" - flickering:

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.X > 100)
        {
            Point mousePosition = this.PointToClient(Cursor.Position);
            mousePosition.X = 100;
            Point newScreenPosition = this.PointToScreen(mousePosition);
            Cursor.Position = newScreenPosition;
        }
    }
+2  A: 

Have you tried using Cursor.Position?

E.g.

Cursor.Position = new Point(100, 100);

You could keep setting it to a constant value (like Vulcan said).

NickAldwin
Nice! Was not aware of this.
Vulcan Eager
I use OnMouseMove e As System.Windows.Forms.MouseEventArgsand seems that e.Location is not synchronized with Cursor.Position... Also I have visible cursor "feedback" when using it...
serhio
@serhio That's strange...how off-sync is it?
NickAldwin
@NickAldwin: see my edit
serhio
@serhio hmm...not sure where to go from there
NickAldwin
I found the cause, but still not happy, because of flickering... :)
serhio
+3  A: 

You could use the ClipCursor function via PInvoke. If the bouding rectangle is small enough, the mouse will not move.


EDIT

An example:

[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct RECT {
    public int left;
    public int top;
    public int right;
    public int bottom;
};


public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    [DllImport("user32.dll")]
    static extern bool ClipCursor([In()]ref RECT lpRect);

    [DllImport("user32.dll")]
    static extern bool ClipCursor([In()]IntPtr lpRect);


    private bool locked = false;

    private void button1_Click(object sender, EventArgs e)
    {

        if (locked) {
            ClipCursor(IntPtr.Zero );
        }
        else {
            RECT r;

            Rectangle t = new Rectangle(0, 0, 100, this.ClientSize.Height);
            t = this.RectangleToScreen(t);

            r.left = t.Left;
            r.top = t.Top;
            r.bottom = t.Bottom;
            r.right = t.Right;

            ClipCursor(ref r);
        }

        locked = !locked;

    }
    private void Form1_Load(object sender, EventArgs e)
    {

    }
}
GSerg
e.. what is the usage in .NET?
serhio
what do you mean "small enough"? and if not?
serhio
The .net declarations for c# and vb are at the bottom of this article.
GSerg
@serhio here's the usage in .NET: http://www.pinvoke.net/default.aspx/user32.ClipCursor(it even gives you all the proper struct definitions)
NickAldwin
... and if not, the cursor will move inside this rectangle. If the rectangle is as big as one pixel, the cursor will freeze.
GSerg
see the example. I need do not allow mouse pass over 100px left margin.
serhio
See the example.
GSerg