110,526
社区成员
发帖
与我相关
我的任务
分享
int maxX=this.Parent.Width-this.Width;
int maxY=this.Parent.Height-this.Height;
int x=this.Location.x;
int y=this.Loaction.y;
bool needToChangeLocation=false;
if(x<0)
{
x=0;
needToChangeLocation=true;
}
if(y<0)
{
y=0;
needToChangeLocation=true;
}
if(x>maxX)
{
x=maxX;
needToChangeLocation=true;
}
if(y>maxY)
{
y=maxY;
needToChangeLocation=true;
}
if(needToChangeLocation)
{
this.Loaction=new Point(x,y);
}
把这段代码放到LocationChanged事件里去,位置一变就执行