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

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

namespace  WindowsApplication2
ExpandedBlockStart.gifContractedBlock.gif
{
    
public partial class Form1 : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            InitializeComponent();
        }


        
protected override void OnLoad( EventArgs e )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
base.OnLoad( e );

            
this.button1 = new System.Windows.Forms.Button();
            
// 
            
// button1
            
// 
            this.button1.Location = new System.Drawing.Point( 5337 );
            
this.button1.Name = "button1";
            
this.button1.Size = new System.Drawing.Size( 7523 );
            
this.button1.TabIndex = 0;
            
this.button1.Text = "button1";
            
this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler( this.button1_MouseDown );
            
this.Controls.Add( this.button1 );
        }


        
private void button1_MouseDown( object sender, MouseEventArgs e )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.button1.DoDragDrop( sender, DragDropEffects.Move );

ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//*此处如果不写,按钮就会消失,后来设断电跟踪发现按钮已经从Controls中移除,Controls.Count = 0
             *执行 OnLoad( e ); 后,Controls.Count = 1, 按钮会重新加载;可以根据需求决定是否写
            
*/

            OnLoad( e );
        }


        
protected override void OnDoubleClick( EventArgs e )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
base.OnDoubleClick( e );
            Form2 frm 
= new Form2();
            frm.Show();
        }


    }

}

 

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()

        {

            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 );
                }
            }
        }
    }
}

 

关于将 Form1 中的按钮保留的处理方法,感觉有更好的解决方法,如果有达人看见,希望不吝赐教。
 

 

 

 

 

 

转载于:https://www.cnblogs.com/ttc/archive/2008/08/21/1273172.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值