这里是通过拖动panel里的名为component的picturebox,来实现整个panel的拖动
有时候panel设计得很紧凑,直接给panel设计拖动是不可行的,因为无法触发点击事件,点击到的只是panel内的控件;panel位置改变,里面的控件也会跟着移动
拖动单个控件的原理是一样的;
Point pt;
private void component_MouseDown(object sender, MouseEventArgs e)
{
pt = Cursor.Position;
}
private void component_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int px = Cursor.Position.X - pt.X;
int py = Cursor.Position.Y - pt.Y;
panelC.Location = new Point(panelC.Location.X + px, panelC.Location.Y + py);
pt = Cursor.Position;
}
}