VS 2005 WinForm 按钮拖放
最近做一个项目,要求是仿照Visual Studio 集成开发环境 (IDE)那样的按钮拖放功能(或者其它控件拖放),在网上和书中找到一些例子,网上参考: http://topic.csdn.net/u/20080306/15/2532d0a0-2c72-46d2-97731ab6c79391f2.html
参考书:Microsoft C# Windows 程序设计(下册)[美]Charles Petzold 著 北京大学出版社
代码 :
Form1.cs
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
Form1.Designer.cs
namespace WindowsApplication2
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose( bool disposing )
{
if( disposing && ( components != null ) )
{
components.Dispose();
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF( 6F, 12F );
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size( 467, 249 );
this.Controls.Add( this.button1 );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout( false );
}
#endregion
private System.Windows.Forms.Button button1;
}
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose( bool disposing )
{
if( disposing && ( components != null ) )
{
components.Dispose();
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF( 6F, 12F );
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size( 467, 249 );
this.Controls.Add( this.button1 );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout( false );
}
#endregion
private System.Windows.Forms.Button button1;
}
}
Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
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 )
{
/* 这句话可以让 Form1.cs 中的 button1 Remove;如果不写,执行 Form1.cs 中的
* this.button1.DoDragDrop( sender, DragDropEffects.Move )后,button1也会移除
* 在此我的需求是保留,所以不写。
*/
// btn.Parent.Controls.Remove( btn );
btn.Location = this.PointToClient( new Point( e.X, e.Y ) );
this.Controls.Add( btn );
}
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
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 )
{
/* 这句话可以让 Form1.cs 中的 button1 Remove;如果不写,执行 Form1.cs 中的
* this.button1.DoDragDrop( sender, DragDropEffects.Move )后,button1也会移除
* 在此我的需求是保留,所以不写。
*/
// btn.Parent.Controls.Remove( btn );
btn.Location = this.PointToClient( new Point( e.X, e.Y ) );
this.Controls.Add( btn );
}
}
}
}
}
关于将 Form1 中的按钮保留的处理方法,感觉有更好的解决方法,如果有达人看见,希望不吝赐教。