gpt4 book ai didi

c# - 如何使鼠标卡住 c#

转载 作者:太空狗 更新时间:2023-10-29 23:58:31 25 4
gpt4 key购买 nike

我希望鼠标在按下时卡住(不能移动)谢谢

最佳答案

我用了一个tableLayoutPanel供大家引用(记得把代码实现到前面的Control):

选项 1:重置鼠标位置:

定义两个全局变量:

bool mousemove = true;
Point currentp = new Point(0, 0);

更新 mousemove 的 Handel MouseDown 事件:

private void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
{
int offsetX = (sender as Control).Location.X + this.Location.X;
int offsetY = (sender as Control).Location.Y + this.Location.Y;
mousemove = false;
currentp = new Point(e.X+offsetX, e.Y+offsetY); //or just use Cursor.Position
}

Handel MouseMove 禁用/启用移动:

 private void tableLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
{
if (!mousemove)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = currentp;
}
}

在 Mouseup 时重置 mousemove

private void tableLayoutPanel1_MouseUp(object sender, MouseEventArgs e)
{
mousemove = true;
}

OPTION2:限制鼠标裁剪矩形:

在按下 MouseDown 时限制它:

private void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = Cursor.Position;
Cursor.Clip = new Rectangle(Cursor.Position, new Size(0, 0));
}

在 MouseUp 之后释放它:

 private void tableLayoutPanel1_MouseUp(object sender, MouseEventArgs e)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = Cursor.Position;
Cursor.Clip = Screen.PrimaryScreen.Bounds;
}

关于c# - 如何使鼠标卡住 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3996382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com