我想做一个可以在窗体中拖动控件的程序,应该怎么做?控件名_MouseMove和控件名_DragDrop总是不尽人意
bgxy 2002-08-02 01:47:21 比如在窗体上有一个PictureBox控件,鼠标左键在控件中点击,划动鼠标可以让控件跟随鼠标一起移动,松开鼠标的点击就停止移动。
我试着写的代码是:
private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
//如果点击左键并移动
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.pictureBox1.Location = new System.Drawing.Point(e.X-pictureBox1.Location.X, e.Y-pictureBox1.Location.Y);
}
}
先不要考虑我的控件被移动之后的Location算法(e.X-pictureBox1.Location.X一定不对),请高手指点,谢谢!