c#winform图片跟随鼠标移动

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 图片跟随鼠标滑动
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }
        //设置鼠标按下是否可用
        private bool ismousedown=false;
        private Point zhizheng;//记录鼠标的坐标

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                zhizheng.X = e.X;
                zhizheng.Y = e.Y;
                ismousedown = true;
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (ismousedown)
            {
                int left=pictureBox1.Left+e.X-zhizheng.X;
                int Right = pictureBox1.Top + e.Y - zhizheng.Y;
                this.pictureBox1.Location = new Point(left, Right );
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ismousedown = false;
            }
        }

    }
}
 

你可能感兴趣的:(C#,图片,鼠标,WinForm,休闲)