码迷,mamicode.com
首页 > 移动开发 > 详细

鼠标拖动 移动坐标

时间:2015-06-29 13:17:05      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

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

namespace 鼠标拖动移动坐标
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private bool isok ;
        private int x; //鼠标点下去的横坐标
        private int y;//鼠标点下去的纵坐标
        private int cx;
        private int cy;
        private void Form1_MouseDown(object sender, MouseEventArgs e) //点下去
        {
            isok = true;
            x = Cursor.Position.X; //鼠标点下去时候窗体的横坐标
            y = Cursor.Position.Y;//鼠标点下去时候窗体的纵坐标
            cx = this.Location.X;//窗体初始横坐标
            cy = this.Location.Y;//窗体初始纵坐标
            
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)//松开
        {
            isok = false;
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)//移动
        {
            if(isok)
            {
                int movex = Cursor.Position.X; //移动过程的横坐标
                int movey = Cursor.Position.Y;//移动过程的纵坐标
                this.Location = new Point(cx+movex-x,cy+movey-y); //初始坐标+移动的坐标-鼠标最开始点下去的坐标
               
            }
        }

     
    }
}

  

鼠标拖动 移动坐标

标签:

原文地址:http://www.cnblogs.com/Mr-xue/p/4607198.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!