.net 停止或移动鼠标

.net 停止或移动鼠标,.net,winforms,gdi+,.net,Winforms,Gdi+,我有一个用鼠标移动图形对象的图形应用程序 在某些情况下,对象停止移动。然后我需要停止移动鼠标光标 可能吗鼠标位置属性似乎处于只读状态 例如 编辑,第二个版本,工作,但光标不“稳定”-闪烁: private void Form1_MouseMove(object sender, MouseEventArgs e) { if (e.X > 100) { Point mousePosition = this.PointToC

我有一个用鼠标移动图形对象的图形应用程序

在某些情况下,对象停止移动。然后我需要停止移动鼠标光标

可能吗<代码>鼠标位置属性似乎处于只读状态

例如

编辑,第二个版本,工作,但光标不“稳定”-闪烁:

    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;
        }
    }

您是否尝试过使用
光标位置

例如


您可以将其设置为一个常量值(如Vulcan所说)。

您是否尝试过使用
光标。位置

例如

您可以将其设置为常量(如Vulcan所说)。

您可以通过PInvoke使用该功能。如果束矩形足够小,鼠标将不会移动


编辑

例如:

[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)
    {

    }
}
您可以通过PInvoke使用该功能。如果束矩形足够小,鼠标将不会移动


编辑

例如:

[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)
    {

    }
}

我使用OnMouseMove作为System.Windows.Forms.MouseEventArgs,似乎e.Location与Cursor.Position不同步。。。另外,我在使用它时有可见的光标“反馈”…@serhio这很奇怪…它有多不同步?@serhio-hmm…不确定从哪里开始我找到了原因,但仍然不高兴,因为闪烁…)我使用OnMouseMove作为System.Windows.Forms.MouseEventArgs,似乎e.Location与Cursor.Position不同步。。。另外,我在使用它时有可见的光标“反馈”…@serhio这很奇怪…它有多不同步?@serhio-hmm…不确定从哪里开始我找到了原因,但仍然不高兴,因为闪烁…)ENET的用法是什么?你说的“足够小”是什么意思?如果没有,c#和vb的.net声明在本文的底部。@serhio这里是.net中的用法:(它甚至提供了所有正确的结构定义)。。。如果没有,光标将在该矩形内移动。如果矩形有一个像素那么大,光标将冻结。。NET的用法是什么?你说的“足够小”是什么意思?如果没有,c#和vb的.net声明在本文的底部。@serhio这里是.net中的用法:(它甚至提供了所有正确的结构定义)。。。如果没有,光标将在该矩形内移动。如果矩形大到一个像素,光标将冻结。您可以使用对ClipCursor的单个调用替换此代码,其中矩形为
{0,0,100,Form.Height}
(显然,从客户端坐标转换为屏幕坐标)。您可以使用对ClipCursor的单个调用替换此代码,其中矩形为
{0,0100,Form.Height}
(显然是从客户端坐标转换为屏幕坐标)。
[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)
    {

    }
}