使用鼠标拖放复制文本

实现效果:

  

知识运用:
  TextBox控件的DoDragDrop方法   //用来开始拖放操作

  public DragDropEffects DoDragDrop (Object  data,DragDropEffects allowedEffects)

  DataObject类的GetData方法

  public virtual Object GetData (string fonmat)  //返回与指定数据格式关联的数据

  DragEventArgs类的Data属性和Effect属性

  public IDataObject Data {get;}    //获取一个数据对象 包含于对应拖动事件关联的数据

  public DragDropEffects Effect {get ; set;}    //获取或设置目标拖放操作    属性值: DragDropEffects枚举值之一

实现代码:

        private void textBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if ((e.Button & MouseButtons.Left) == MouseButtons.Left)        //如果按下的是左键
            {   //拖放文本
                DragDropEffects dropEffert = txt_1.DoDragDrop(txt_1.Text, DragDropEffects.Copy | DragDropEffects.Link);
            }
        }

        private void txt_2_DragEnter(object sender, DragEventArgs e)
        {
             e.Effect = DragDropEffects.Copy;                               //设置复制操作
        }

        private void txt_2_DragDrop(object sender, DragEventArgs e)
        {
            txt_2.Text = e.Data.GetData(DataFormats.Text).ToString();       //显示拖放文本
        }

  

猜你喜欢

转载自www.cnblogs.com/feiyucha/p/10289068.html