首页 新闻 会员 周边 捐助

在c#窗体中实现鼠标拖拽

0
悬赏园豆:5 [已解决问题] 解决于 2012-04-16 22:51
Form1中有panel1和panel2,想在panel1中绘制一个矩形,可以鼠标拖拽到panel2中~~~在线等,求代码呀~~
巴索罗缪库玛的主页 巴索罗缪库玛 | 初学一级 | 园豆:48
提问于:2012-04-14 10:52
< >
分享
最佳答案
0

我放了两个panel  可以拖着一个label.不知道这样满足的了你的要求不.?

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 TestPanel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            c = panel1;
        }

        Point pt;
        //bool moves = true;
        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            pt = Cursor.Position;
        }

        Control c = null;
        private void label1_MouseMove(object sender, MouseEventArgs e)
        {
            int mx = 0;
            int my = 0;
            int px = 0;
            int py = 0;
            int x = 0;
            int y = 0;
            if (e.Button == MouseButtons.Left)
            {
                x = Cursor.Position.X - this.Location.X - 14;
                y = Cursor.Position.Y - this.Location.Y - 32;
                px = Cursor.Position.X - pt.X;
                py = Cursor.Position.Y - pt.Y;
                mx = label1.Location.X + px;
                my = label1.Location.Y + py;
                label1.Location = new Point(mx, my);
                pt = Cursor.Position;

                if (panel1.Bounds.Contains(x, y))
                {
                    if (c.Equals(panel1)) return;
                    mx = label1.Location.X - panel1.Bounds.X;
                    my = label1.Location.Y - panel1.Bounds.Y;
                    label1.Location = new Point(mx, my);
                    c.Controls.Remove(label1);
                    panel1.Controls.Add(label1);
                    c = panel1;
                }
                else if (panel2.Bounds.Contains(x, y))
                {
                    if (c.Equals(panel2)) return;
                    mx = label1.Location.X - panel2.Bounds.X;
                    my = label1.Location.Y - panel2.Bounds.Y;
                    label1.Location = new Point(mx, my);
                    c.Controls.Remove(label1);
                    panel2.Controls.Add(label1);
                    c = panel2;
                }
                else if (this.Bounds.Contains(x, y))
                {
                    if (c.Equals(this)) return;
                    mx = label1.Location.X + c.Bounds.X;
                    my = label1.Location.Y + c.Bounds.Y;
                    label1.Location = new Point(mx, my);
                    c.Controls.Remove(label1);
                    this.Controls.Add(label1);
                    c = this;
                }
            }
        }

        private void label1_MouseUp(object sender, MouseEventArgs e)
        {
            //moves = true;
        }
    }
}

收获园豆:5
只会造轮子 | 老鸟四级 |园豆:2274 | 2012-04-14 15:41

我试了一下……拖不动呀~你能把代码加上注释发到我邮箱windyk508@163.com吗?谢谢了

至少得告诉我x,y,mx,my,px,py都指的是什么,-14,-32什么的那段一定要告诉我,有点看不懂了

巴索罗缪库玛 | 园豆:48 (初学一级) | 2012-04-14 22:50

@巴索罗缪库玛: 

                x = Cursor.Position.X - this.Location.X - 14;//得到鼠标在窗口中对应的位置
                y = Cursor.Position.Y - this.Location.Y - 32;//14和32是 窗口的左上角窗口的可用区域 的 左上角 的两个坐标差 这个我没有找到具体的属性取到它,所以就写死了
                px = Cursor.Position.X - pt.X;//取得鼠标相对移动的位置
                py = Cursor.Position.Y - pt.Y;//pt是存储上次鼠标移动后的位置
                mx = label1.Location.X + px;//把鼠标移动大小复给label,让label也移动鼠标移动的大小
                my = label1.Location.Y + py;
                label1.Location = new Point(mx, my);

               pt = Cursor.Position;//重新存储鼠标位置

 

                if (panel1.Bounds.Contains(x, y))//判断鼠标是否移动到了panel1中
                {
                    if (c.Equals(panel1)) return;//如果移动到了panel1中,并且上次移动也是在panel1中那么不做处理
                    mx = label1.Location.X - panel1.Bounds.X;//下面这几句是当鼠标的位置切换容器时,把label1也切换控件,并且从新初始化label1的位置
                    my = label1.Location.Y - panel1.Bounds.Y;
                    label1.Location = new Point(mx, my);
                    c.Controls.Remove(label1);
                    panel1.Controls.Add(label1);
                    c = panel1;//从新记录容器
                }

             下面两个if和这个是做一样的处理,

只会造轮子 | 园豆:2274 (老鸟四级) | 2012-04-15 14:31

@发粪图墙: 非常感谢你,确实是实现了,但是我接下来要做仿真动画,是不能用label的,只能是在panel中画一个矩形,然后可以用鼠标拖拽这个矩形,这回只有一个panel。如果能将这个问题帮我解决就更感谢啦~~

巴索罗缪库玛 | 园豆:48 (初学一级) | 2012-04-15 22:04

@巴索罗缪库玛: 我没做过画图的,不知道是什么情况,那东西应该也是根据坐标来做的吧。你把代码改改试试吧

只会造轮子 | 园豆:2274 (老鸟四级) | 2012-04-15 23:11

@发粪图墙: 好吧,不过谢谢你了

巴索罗缪库玛 | 园豆:48 (初学一级) | 2012-04-16 22:50
其他回答(1)
0

学习了。。。

KivenRo | 园豆:1734 (小虾三级) | 2012-04-15 08:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册