问题如下:
我有一个窗体 FormA,里面有一个Button1... 还有一段代码codeA
另有一个窗体 FormB. 是MDI的子窗体. 一段代码codeB(Button1)我的问题是,
1. 我要拖FormA的按钮Button1到FormB上,
2.松开之后,然后在FormB上执行codeB,在FormA上执行一段代码codeA.类似"拖一个文件到windows回收站图标" 的那样的效果.有没有达人,能帮个忙给瞅瞅?或者是提供一下思路.
我有一个窗体 FormA,里面有一个Button1... 还有一段代码codeA
另有一个窗体 FormB. 是MDI的子窗体. 一段代码codeB(Button1)我的问题是,
1. 我要拖FormA的按钮Button1到FormB上,
2.松开之后,然后在FormB上执行codeB,在FormA上执行一段代码codeA.类似"拖一个文件到windows回收站图标" 的那样的效果.有没有达人,能帮个忙给瞅瞅?或者是提供一下思路.
再好好利用
Form.DragDrop
DragEnter
DragLeave
DragOver四个事件
一定可以完成的
" 或者是说,我把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
}
}
MainFrame,它可以看作是两个form的容器类的东西,这样它可以在上一层(form1等的上一层)对事件进行分发和
处理
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();
}
}
}
代码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);
}
}
}
}
}
(个人觉得桌面上的回收站实际上就是在同一个窗口desktop上的操作,)如果是不同的窗口,比如两个资源管理器(不知道白话是不是叫‘我的电脑’…)窗口之间的通过鼠标拖拽完成文件的移动。
这个我觉得应该是一个常驻进程的功能。
如果定义一个拖动类实现IDataObject接口,就可以实现多进程的Form中button的拖动。而且button也会响应原窗体中定义的事件。