怎么让一个button拖动后top,left不要出去panel1?
一个按钮,我把它放在panel1里面,可是拖动它的话,会被拖出panel1外面去,变成不可见了 怎么解决?怎么控制它?急。。
下面是我的代码,麻烦高手帮我修改下:
private bool isMove = false;
private int MouseDownX;
private int MouseDownY; private void lblCode_MouseMove(object sender, MouseEventArgs e)
{
if (isMove)//如果左键按下的话
{
((Button)sender).Location = new Point(((Button)sender).Left + e.X - MouseDownX, ((Button)sender).Top + e.Y - MouseDownY);
} private void lblCode_MouseDown(object sender, MouseEventArgs e)
{
isMove = true;
MouseDownX = e.X;
MouseDownY = e.Y;
}
private void lblCode_MouseUp(object sender, MouseEventArgs e)
{
isMove = false;
}
一个按钮,我把它放在panel1里面,可是拖动它的话,会被拖出panel1外面去,变成不可见了 怎么解决?怎么控制它?急。。
下面是我的代码,麻烦高手帮我修改下:
private bool isMove = false;
private int MouseDownX;
private int MouseDownY; private void lblCode_MouseMove(object sender, MouseEventArgs e)
{
if (isMove)//如果左键按下的话
{
((Button)sender).Location = new Point(((Button)sender).Left + e.X - MouseDownX, ((Button)sender).Top + e.Y - MouseDownY);
} private void lblCode_MouseDown(object sender, MouseEventArgs e)
{
isMove = true;
MouseDownX = e.X;
MouseDownY = e.Y;
}
private void lblCode_MouseUp(object sender, MouseEventArgs e)
{
isMove = false;
}
我也是才发现的
你先建立这个BUTTON
然后把这个BUTTON的属性LOCK 设置为TRUE
然后再建立这个PANEL
把PANEL置于底层
接着你就可以把BUTTON的LOCK 设置为FALSE了
{
if (isMove)//如果左键按下的话
{
Control _Control=(Control)sender;
Point _ControlLocation= new Point(((Button)sender).Left + e.X - MouseDownX, ((Button)sender).Top + e.Y - MouseDownY); if (_ControlLocation.X < 0) _ControlLocation.X = 0;
if (_ControlLocation.Y < 0) _ControlLocation.Y = 0; if (_ControlLocation.X + _Control.Width > _Control.Parent.ClientRectangle.Width) _ControlLocation.X = _Control.Parent.ClientRectangle.Width - _Control.Width;
if (_ControlLocation.Y + _Control.Height > _Control.Parent.ClientRectangle.Height) _ControlLocation.Y = _Control.Parent.ClientRectangle.Height - _Control.Height; _Control.Location = _ControlLocation;
}
}