C# DragDropEffects類與拖動實現


把文件或者目錄直接拖放到你的程序上,這種效果用戶體驗不錯。
得到拖過來的路徑的代碼:(System.Array)e.Data.GetData(DataFormats.FileDrop)
然后你可以根據這些路徑復制粘貼了。
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 WinToXml
{
     public  partial  class FrmMain : Form
    {
         public FrmMain()
        {
            InitializeComponent();
        }
         // Winform窗體文本框實現拖拽獲得目錄或文件路徑(C#)

         protected  void SetAllTextBox(Control org)
        {
             foreach (Control txt  in org.Controls)
            {
                 if (txt  is TextBox)
                {
                    txt.AllowDrop =  true;
                    txt.DragDrop +=  new DragEventHandler(txt_ObjDragDrop);
                    txt.DragEnter +=  new DragEventHandler(txt_ObjDragEnter);
                }
                 else
                {
                     if (txt.Controls.Count >  0)
                    {
                        SetAllTextBox(txt);
                    }
                }
            }
        }

         private  void txt_ObjDragEnter( object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Link; // 拖動時的圖標
        }

         private  void txt_ObjDragDrop( object sender, DragEventArgs e)
        {
            ((TextBox)sender).Text
                = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue( 0).ToString();
        }

         private  void FrmMain_Load( object sender, EventArgs e)
        {
            SetAllTextBox( this);
        }
    }
}
url: http://greatverve.cnblogs.com/archive/2012/03/03/DragDropEffects.html
在應用程序中,是通過處理一系列事件,如DragEnter,DragLeave和DragDrop事件來實現在Windows應用程序中的拖放操作的。通過使用這些事件參數中的可用信息,可以輕松實現拖放操作。 
拖放操作在代碼中是通過三步實現的,首先是啟動拖放操作,在需要拖動數據的控件上實現MouseDown事件響應代碼,並調用DoDragDrop()方法;其次是實現拖放效果,在目標控件上添加DragEnter事件響應代碼,使用DragDropEffects枚舉類型實現移動或復制等拖動效果;最后是放置數據操作,在目標控件上添加DragDrop響應代碼,把數據添加到目標控件中。 
using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 

namespace DragDrop 

///   <summary>  
///  Form1 的摘要說明。 
///   </summary>  
public  class Form1 : System.Windows.Forms.Form 

    private System.Windows.Forms.ListBox listBox1; 
    private System.Windows.Forms.ListBox listBox2; 
    ///   <summary>  
   
///  必需的設計器變量。 
   
///   </summary>  
    private System.ComponentModel.Container components =  null

    public Form1() 
   { 
     //  
    
//  Windows 窗體設計器支持所必需的 
    
//  
    InitializeComponent(); 

     //  
    
//  TODO: 在 InitializeComponent 調用后添加任何構造函數代碼 
    
//  
   } 

    ///   <summary>  
   
///  清理所有正在使用的資源。 
   
///   </summary>  
    protected  override  void Dispose(  bool disposing ) 
   { 
     if( disposing ) 
    { 
      if (components !=  null
     { 
      components.Dispose(); 
     } 
    } 
     base.Dispose( disposing ); 
   } 

    #region Windows 窗體設計器生成的代碼 
    ///   <summary>  
   
///  設計器支持所需的方法 - 不要使用代碼編輯器修改 
   
///  此方法的內容。 
   
///   </summary>  
    private  void InitializeComponent() 
   { 
     this.listBox1 =  new System.Windows.Forms.ListBox(); 
     this.listBox2 =  new System.Windows.Forms.ListBox(); 
     this.SuspendLayout(); 
     //  
    
//  listBox1 
    
//  
     this.listBox1.ItemHeight =  12
     this.listBox1.Location =  new System.Drawing.Point( 3224); 
     this.listBox1.Name =  " listBox1 "
     this.listBox1.Size =  new System.Drawing.Size( 120280); 
     this.listBox1.TabIndex =  0
     this.listBox1.MouseDown +=  new System.Windows.Forms.MouseEventHandler( this.listBox1_MouseDown); 
     //  
    
//  listBox2 
    
//  
     this.listBox2.ItemHeight =  12
     this.listBox2.Location =  new System.Drawing.Point( 24824); 
     this.listBox2.Name =  " listBox2 "
     this.listBox2.Size =  new System.Drawing.Size( 120280); 
     this.listBox2.TabIndex =  0
     this.listBox2.DragDrop +=  new System.Windows.Forms.DragEventHandler( this.listBox2_DragDrop); 
     this.listBox2.DragEnter +=  new System.Windows.Forms.DragEventHandler( this.listBox2_DragEnter); 
     //  
    
//  Form1 
    
//  
     this.AutoScaleBaseSize =  new System.Drawing.Size( 614); 
     this.ClientSize =  new System.Drawing.Size( 408333); 
     this.Controls.Add( this.listBox1); 
     this.Controls.Add( this.listBox2); 
     this.Name =  " Form1 "
     this.Text =  " Form1 "
     this.Load +=  new System.EventHandler( this.Form1_Load); 
     this.ResumeLayout( false); 

   } 
    #endregion 

    ///   <summary>  
   
///  應用程序的主入口點。 
   
///   </summary>  
   [STAThread] 
    static  void Main() 
   { 
    Application.Run( new Form1()); 
   } 

    private  void Form1_Load( object sender, System.EventArgs e) 
   { 
     this.listBox1.AllowDrop =  true
     this.listBox2.AllowDrop =  true
     this.listBox1.Items.Add( " a "); 
     this.listBox1.Items.Add( " b "); 
     this.listBox1.Items.Add( " c "); 
   } 

    private  void listBox1_MouseDown( object sender, System.Windows.Forms.MouseEventArgs e) 
   { 
     this.listBox1.DoDragDrop( this.listBox1.Items[ this.listBox1.SelectedIndex],DragDropEffects.Move); 
   } 

    private  void listBox2_DragEnter( object sender, System.Windows.Forms.DragEventArgs e) 
   { 
     if(e.Data.GetDataPresent( " Text ")) 
    { 
     e.Effect = DragDropEffects.Move; 
    } 
   } 

    private  void listBox2_DragDrop( object sender, System.Windows.Forms.DragEventArgs e) 
   { 
     this.listBox2.Items.Add(e.Data.GetData( " Text ")); 
     this.listBox1.Items.Remove(e.Data.GetData( " Text ")); 
   } 

}
msdn: http://msdn.microsoft.com/zh-cn/library/system.windows.forms.dragdropeffects.aspx

1.方法

實現拖放效果時,C#中提供了一個系統方法DoDragDrop方法,用於實現開始拖放操作,該方法由Control類所定義,由於控件均直接或是間接派生於Control類,因此開發人員可以在任何可視化組件中調用DoDragDrop方法。DoDragDrop方法使用語法如下:

public DragDropEffects DoDragDrop ( Object data,DragDropEffects allowedEffects)

data:用戶所要拖動的數據內容。必須將所要拖動的內容傳入到這個方法的第一個參數位置。

allowedEffects:DragDropEffects枚舉值之一,此類型包含了拖動操作的效果。DragDropEffects枚舉值如表32.8所示。

表32.8   DragDropEffects枚舉值


枚舉值 說明 
All 從拖動源復制、移除數據,並將其滾動到放置目標中 
Copy 將數據復制到放置目標 
Link 將拖動源中的數據鏈接到放置目標 
Move 將拖動源的數據移動到放置目標 
None 放置目標不接受該數據 
Scroll 即將在放置目標中開始滾動,或當前正在滾動


開發人員在使用DoDragDrop方法時,必須指定參數allowedEffects為表**中的任何一個成員,另外,還可以使用位運算符,把其中的任何一個成員作為一個完整參數傳入,以得到所需的拖動效果,實現關鍵代碼如下:

DragDropEffects.Copy| DragDropEffects.None

2.事件

C#中提供了一個系統拖放事件,與拖放方法一起使用來達到更好的效果。常用的拖放事件如表所示。

表  拖放事件


名稱 說明 
DragEnter 當用戶在拖放操作過程中首次將鼠標光標拖到控件上時,會引發該事件 
DragDrop 在完成拖放操作時發生 
GiveFeedback 在執行拖動操作期間發生 
DragLeave 如果用戶移出一個窗口,則引發DragLeave事件 
DragOver 如果鼠標移動但停留在同一個控件中,則引發DragOver事件 
QueryContinueDrag 在拖放操作過程中,當鍵盤或鼠標按鈕狀態發生變化時,會引發QueryContinueDrag 事件。QueryContinueDrag事件允許拖動源確定是否應取消拖放操作


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM