我的movable_picturebox.cs文件包含:
public class movable_picturebox : PictureBox
{
public movable_picturebox(IContainer container)
{
container.Add(this);
}
Point point;
protected override void OnMouseDown(MouseEventArgs e)
{
point = e.Location;
base.OnMouseDown(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
this.Left += e.X - point.X;
this.Top += e.Y - point.Y;
}
}
}
现在,当我在winforms中添加我的moveable_picturebox元素时,可以通过在需要的地方按住鼠标将其移动。
我需要怎么做才能停止此活动?例如:我启动程序我将图片框移到特定位置,我需要停止此事件,才能移动此图片框。我需要再次覆盖此事件,但是我该怎么做呢?和代码应该包含什么?
而不是再次重写该方法,只需在现有方法中添加另一个条件检查。在布尔值中设置是否设置canMovePictureBox
,然后将函数更改为
protected override void OnMouseMove(MouseEventArgs e)
{
if(e.Button == MouseButtons.Left && canMovePictureBox)
{
this.Left += e.X - point.X;
this.Top += e.Y - point.Y;
}
}