using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Ex02_9
{
    public partial class Form1 : Form
    {

        bool flag = false;
        int x;
        int y;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            flag = true;            //设置变量flag为true 
            x = e.X;                //获取鼠标的X坐标值并赋值给变量x
            y = e.Y;               //获取鼠标的Y坐标值并赋值给变量y
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (flag)           //如果鼠标已按下
            {
                               //当移动鼠标时,图像框跟着移动
                pictureBox1.Left = pictureBox1.Left + (e.X - x);   
                pictureBox1.Top = pictureBox1.Top + (e.Y - y);
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            flag = false;         //松开鼠标停止移动图像框
        }
    }
}
