⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c_shanrp.txt

📁 这是我所搜集的关于c#拖放技术的一些技术资料
💻 TXT
字号:
C#拖放技术相关方法和事件
3G双线全能空间1元认购 开网店-找友创互联 
http://tech.ddvip.com   2008年11月04日    社区交流

关键字: 枚举类型 多线程 ocket SpiderMonkey 观察者模式 
C#拖放技术相关方法和事件。 
  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事件允许拖动源确定是否应取消拖放操作
 


  下面对拖放事件中比较重要的事件进行详细介绍。

  (1)DragEnter事件

  当用户在拖放操作过程中首次将鼠标光标拖到控件上时,触发该事件。

  语法:

  public event DragEventHandler DragEnter

  该事件为DragEventHandler委托类型,该委托是专门设计用以处理控件的DragEnter、DragDrop、GiveFeedback、DragLeave和DragOver等相关事件的方法。DragEventHandler委托类型的定义语法如下:

  public delegate void DragEventHandler(object sender,DragEventArgs e)

  该委托封装的方法必须接受两个参数,第一个是object类型对象,该对象用来指定拖放目标对象;第二个为DragEventArgs 类型参数e,它主要包含拖动操作的相关数据。DragEventArgs 类型参数e的相关属性值及说明如表所示。

  表 DragEventArgs类型参数e的属性值及说明

    名称
   说明
 
  AllowedEffect
   获取拖动事件的发起方(或源)所允许的拖放操作
 
  Data
   获取IDataObject,它包含与此事件关联的数据
 
  Effect
   获取或设置拖放操作中目标放置效果
 
  KeyState
   获取Shift、Ctrl 和 Alt键的当前状态以及鼠标按钮的状态
 
  X
   获取鼠标指针在屏幕坐标系中的x坐标
 
  Y
   获取鼠标指针在屏幕坐标系中的y坐标
 


  (2)QueryContinueDrag事件

  在拖放操作过程中,当键盘或鼠标按钮状态发生变化时,触发该事件。

  语法:

  public event QueryContinueDragEventHandler QueryContinueDrag

  委托定义语法如下:

  public delegate void DragEventHandler(object sender, QueryContinueDragEventArgs e)

  该委托封装的方法必须接受两个参数,第一个是object类型对象,该对象用来指定为拖放目标对象;第二个为QueryContinueDragEventArgs 类型参数e,它主要包含拖动操作的相关数据。QueryContinueDragEventArgs 类型参数e有一个Action属性,该属性的属性值用来确定是否继续拖动、放置数据或取消操作。Action属性值及说明如表32.11所示。

  表 QueryContinueDragEventArgs.Action属性值及说明

    名称
   说明
 
  Continue
   该操作将继续
 
  Drop
   该操作以放置而告终
 
  Cancel
   操作被取消,没有放置消息
 



责编:豆豆技术应用
*******************
用C#.NET实现拖放操作
添加时间:2007-10-25 
在应用程序中,是通过处理一系列事件,如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(32, 24);
   this.listBox1.Name = "listBox1";
   this.listBox1.Size = new System.Drawing.Size(120, 280);
   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(248, 24);
   this.listBox2.Name = "listBox2";
   this.listBox2.Size = new System.Drawing.Size(120, 280);
   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(6, 14);
   this.ClientSize = new System.Drawing.Size(408, 333);
   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"));
  }
 }
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -