Ada*_*m P 3 c# mouse controls position flicker
当用户单击并拖动控件时,我试图让控件跟随光标.问题是1.)控制器不会移动到鼠标的位置,以及2.)控制器在整个地方闪烁和飞行.我尝试了几种不同的方法,但到目前为止都失败了.
我试过了:
protected override void OnMouseDown(MouseEventArgs e)
{
while (e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.Location = e.Location;
}
}
Run Code Online (Sandbox Code Playgroud)
和
protected override void OnMouseMove(MouseEventArgs e)
{
while (e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.Location = e.Location;
}
}
Run Code Online (Sandbox Code Playgroud)
但这些都不奏效.任何帮助表示赞赏,并提前感谢!
Mus*_*sis 10
这是怎么做的:
private Point _Offset = Point.Empty;
protected override void MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_Offset = new Point(e.X, e.Y);
}
}
protected override void MouseMove(object sender, MouseEventArgs e)
{
if (_Offset != Point.Empty)
{
Point newlocation = this.Location;
newlocation.X += e.X - _Offset.X;
newlocation.Y += e.Y - _Offset.Y;
this.Location = newlocation;
}
}
protected override void MouseUp(object sender, MouseEventArgs e)
{
_Offset = Point.Empty;
}
Run Code Online (Sandbox Code Playgroud)
_Offset 这里使用的目的有两个:跟踪最初点击鼠标时控件上鼠标的位置,并跟踪鼠标按钮是否关闭(这样当鼠标光标时控件不会被拖动)过去了,按钮没有关闭).
你绝对不希望if将此代码中的whiles 切换为s,因为它会产生影响.
| 归档时间: |
|
| 查看次数: |
10896 次 |
| 最近记录: |