C#中如何实现拖动PictureBox控件

时间:2022-09-28 15:46:43

思路是这样的.得有三个变量.
记录x坐标: int xPos;
记录y坐标: int yPos;
记录是否按下鼠标: bool MoveFlag;

//在picturebox的鼠标按下事件里,记录三个变量.
private void picBox_MouseDown(object sender, MouseEventArgs e)
{
    MoveFlag = true;//已经按下.
    xPos = e.X;//当前x坐标.
    yPos = e.Y;//当前y坐标.
}

//在picturebox的鼠标按下事件里.
private void picBox_MouseUp(object sender, MouseEventArgs e)
{
    MoveFlag = false;
}

//在picturebox鼠标移动
private void picBox_MouseMove(object sender, MouseEventArgs e)
{
    if (MoveFlag)
    {
        picBox.Left += Convert.ToInt16(e.X - xPos);//设置x坐标.
        picBox.Top += Convert.ToInt16(e.Y - yPos);//设置y坐标.
    }
}

这样就可以实现控件在容器里的移动


http://www.cnblogs.com/lyl6796910/archive/2012/06/27/2566359.html