Winform 拖拽控件

private Point mouse_offset;
        private void label1_MouseDown(object o, MouseEventArgs e)
        {
            mouse_offset = new Point(-e.X, -e.Y);//
        }
        private void label1_MouseMove(object o, MouseEventArgs e)
        {

            ((Control)o).Cursor = Cursors.Arrow;//设置拖动时鼠标箭头
            if (e.Button == MouseButtons.Left)
            {
                Point mousePos = Control.MousePosition;
                mousePos.Offset(mouse_offset.X, mouse_offset.Y);//设置偏移
                ((Control)o).Location = ((Control)o).Parent.PointToClient(mousePos);
            }
        }
这个是拖拽label的例子  PictureBox也是一样的原理  注意边框的宽度

你可能感兴趣的:(Winform 拖拽控件)