问题如下:
我有一个窗体 FormA,里面有一个Button1...      还有一段代码codeA
另有一个窗体 FormB. 是MDI的子窗体. 一段代码codeB(Button1)我的问题是,
1. 我要拖FormA的按钮Button1到FormB上,
2.松开之后,然后在FormB上执行codeB,在FormA上执行一段代码codeA.类似"拖一个文件到windows回收站图标" 的那样的效果.有没有达人,能帮个忙给瞅瞅?或者是提供一下思路.

解决方案 »

  1.   

    呵呵,可以在FromB里建立一个和Button一样的Button然后把forma里的删除。
      

  2.   

    Form.AllowDrop = true;
    再好好利用
    Form.DragDrop
    DragEnter
    DragLeave
    DragOver四个事件
    一定可以完成的
      

  3.   

    只是要求用拖拽的方法.或者是说,我把button1拖到窗体FORMB的时候,B里面要执行一个方法,这个方法需要button1里面的一些东西..
      

  4.   

    To 5:能否再详细的说一些呢>?
      

  5.   

    如果我来作,我会这样分析
    " 或者是说,我把button1拖到窗体FORMB的时候,B里面要执行一个方法,这个方法需要button1里面的一些东西.."
    说明FromB中的响应方法要关联或依赖button类,如果是依赖,则其传入的参数应当是button1;delegate void delEventForm1();
        delegate void delEventForm2(Button btnInstance);
        /// <summary>
        /// 当鼠标离开form1时候触发框架窗体的离开事件,当鼠标离开form2的时候触发框架窗体的进入事件,此框架负责检测到拖动事件,并负责绘制动画效果
        /// </summary>
        class MainFrame
        {
            delEventForm1 delForm1Event = null;
            delEventForm2 delForm2Event = null;
            Form1 frm1Instance;
            Form2 frm2Instance;
            public void initFrame()
            {
                delForm1Event = new delEventForm1(frm1Instance.EventDrag);
                delForm2Event = new delEventForm2(frm2Instance.EventDrag);
            }
            public void EventForm1Leave()
            {
                delForm1Event();
            }
            public void EventForm2Entered()
            {
                delForm2Event(frm1Instance.btnInstance);
            }
        }
        class Form1 : System.Windows.Forms.Form
        {
            public Button btnInstance;
            public void EventDrag()
            {
                //do something while button was draged
            }
        }
        class Form2 : System.Windows.Forms.Form
        {
            public void EventDrag(Button btnInstance)
            {
                //do something using btnInstance
            }
        }
      

  6.   

    正在学习8L的方法,有一点问题.FORM1是在MDI窗体的.FORM2是MDI窗体之外的一个窗体...这样的话,MainFrame窗体的作用是什么?谢谢...
      

  7.   

    呵呵,我这里设定的是,无论你采用哪种模式,你都要有一个机制(类似与监控线程一样的东西),我这里设为
    MainFrame,它可以看作是两个form的容器类的东西,这样它可以在上一层(form1等的上一层)对事件进行分发和
    处理
      

  8.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace test_MoveButtonFromFormToForm
    {
        public partial class Form1 : Form
        {
            Form form = new Form();        bool beginMove = false,onNewform = false;        int startX, startY;
            public Form1()
            {
                InitializeComponent();            this.IsMdiContainer = true;
                this.Load += new EventHandler(Form1_Load);
                button1.MouseDown += new MouseEventHandler(button1_MouseDown);       
                button1.MouseMove += new MouseEventHandler(button1_MouseMove);
                button1.MouseUp += new MouseEventHandler(button1_MouseUp);
            }        void button1_MouseUp(object sender, MouseEventArgs e)
            {
                beginMove = false;            if (!onNewform)
                {
                    if (button1.Left > form.Left && button1.Top > form.Top && button1.Right < form.Right && button1.Bottom < form.Bottom)
                    {
                        button1.Left -= (this.Width-this.ClientSize.Width) / 2;
                        button1.Top -= this.Height - this.ClientSize.Height; ;
                        button1.Parent = form;
                        onNewform = true;
                    }
                }            
            }        void button1_MouseDown(object sender, MouseEventArgs e)
            {
                beginMove = true;
                startX = e.X;
                startY = e.Y;
            }        void button1_MouseMove(object sender, MouseEventArgs e)
            {
                if (beginMove)
                {
                    button1.Left += e.X - startX;
                    button1.Top += e.Y - startY;
                }
            }        void Form1_Load(object sender, EventArgs e)
            {
                form.MdiParent = this;
                form.Show();
            }
        }
    }
      

  9.   

    MARK一下。。以后可能会用到 
      

  10.   

    16L的方法 MS只是在一个窗体内的拖动吧..还是在等高手解决,第三天... 加上周末,第五天.
      

  11.   

    楼主可以参考下面的代码,代码是两个窗体Form1,Form2,可以建立一个应用程序把代码放到项目里启动Form1来运行程序,环境是VS2005。
    代码Form1.csusing System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace dragControl
    {
    public partial class Form1 : Form
    {
    private System.Windows.Forms.Button button1; public Form1()
    {
    InitializeComponent();
    }
    protected override void OnLoad(EventArgs e)
    {
    base.OnLoad(e);
    this.button1 = new System.Windows.Forms.Button();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(116, 65);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(75, 23);
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.UseVisualStyleBackColor = true;
    this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown); this.Controls.Add(this.button1);
    }
    private void button1_MouseDown(object sender, MouseEventArgs e)
    {
    this.button1.DoDragDrop(sender, DragDropEffects.Move);
    }
    protected override void OnDoubleClick(EventArgs e)
    {
    base.OnDoubleClick(e);
    Form2 frm = new Form2();
    frm.Show();
    }
    }
    }代码Form2.csusing System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace dragControl
    {
    public partial class Form2 : Form
    {
    public Form2()
    {
    InitializeComponent();
    }
    protected override void OnLoad(EventArgs e)
    {
    base.OnLoad(e);
    this.AllowDrop = true;
    this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form2_DragDrop);
    this.DragOver += new System.Windows.Forms.DragEventHandler(this.Form2_DragOver);
    }
    private void Form2_DragOver(object sender, DragEventArgs e)
    {
    if (e.AllowedEffect == DragDropEffects.Move)
    {
    e.Effect = DragDropEffects.Move;
    }
    } private void Form2_DragDrop(object sender, DragEventArgs e)
    {
    Button btn = e.Data.GetData("System.Windows.Forms.Button") as Button;
    if (btn != null)
    {
    if (btn.Parent != null)
    {
    btn.Parent.Controls.Remove(btn);
    btn.Location = this.PointToClient(new Point(e.X, e.Y));
    this.Controls.Add(btn);
    }
    }
    }
    }
    }
      

  12.   

    问问怎么实现将托到form2中的button再托回form1
      

  13.   

    同一个窗口(至少两个窗口属于一个进程)里的,应该不太难实现。
    (个人觉得桌面上的回收站实际上就是在同一个窗口desktop上的操作,)如果是不同的窗口,比如两个资源管理器(不知道白话是不是叫‘我的电脑’…)窗口之间的通过鼠标拖拽完成文件的移动。
    这个我觉得应该是一个常驻进程的功能。
      

  14.   

    winFrom 也有好多要学的东西也!不过这里高手如此之多,多多学习
      

  15.   

    加强一下。
    如果定义一个拖动类实现IDataObject接口,就可以实现多进程的Form中button的拖动。而且button也会响应原窗体中定义的事件。
      

  16.   

    我实现了一下多进程拖动。用了IcpChannel,如果不用的话,drop方无法访问被拖动按钮的属性。其实实现就是在drap方,加入下面一行就可以了:   RegisterChannel(new IcpChannel("icp://localhost:0"));drop方得到按钮后对Button类型利用Type.GetProperties()获取所有属性值,赋给一个新生成的按钮。
      

  17.   

    你把form2里面那些实现拖动的代码看懂,拷贝到form1里面就行了。