首页 > 编程 > C# > 正文

C#中拖放功能的实现

2023-05-18 12:33:13
字体:
来源:转载
供稿:网友

C#中的拖放功能使我们在做一些时变得非常方便,下面就以一个实例讲解了具体的拖放操作的实现方法。

下面的代码没有给出注释,加入了一个ListBox,当文件拖放上来后,将内容显示在里面。

      private void lstFilePath_DragEnter(object sender, DragEventArgs e)
             {
                 if (e.Data.GetDataPresent(DataFormats.FileDrop))
                 {
                     e.Effect = DragDropEffects.Link;
                 }
                 else
                 {
                     e.Effect = DragDropEffects.None;
                 }
             }

             private void lstFilePath_DragDrop(object sender, DragEventArgs e)
             {
                 foreach (string strPath in (string[])e.Data.GetData(DataFormats.FileDrop))
                 {
                     lstFilePath.Items.Add(strPath);
                 }
             }

将整个窗体代码都复制下来,是一个复制的小程序,将拖放到LISTBOX里的文件复制到文本框里指定的位置,里面用到了一个外部控件,可以使用普通的button替换之。

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

     namespace PersonalDisk
     {
         public partial class frmDrag : Form
         {
             ///
             /// 获得/设置一个值,判断是否已经存在了一个类的实例
             ///
             public static bool IsExist=false;

             public frmDrag()
             {
                 InitializeComponent();
                 frmDrag.IsExist = true;
             }

             private void frmDrag_MouseDown(object sender, MouseEventArgs e)
             {
                 //如果鼠标指针在标题栏范围内并且按下了鼠标左键,则触发移动标题栏方法
                 if (e.Button == MouseButtons.Left  e.Y
             /// 复制一个目录下的所有文件或目录到一个新的目录下
             ///
             ///    源目录路径
             /// 目标目录路径

             private void CopyDirectory(string sourcePath, string destPath)
             {
                 try
                 {
                     //如果目标路径没有以/结尾则加之
                     if (destPath[destPath.Length - 1] != Path.DirectorySeparatorChar)
                     {
                         destPath += Path.DirectorySeparatorChar;
                     }
                     if (!Directory.Exists(destPath))
                     {
                         Directory.CreateDirectory(destPath);
                     }
                     string[] fileList = Directory.GetFileSystemEntries(sourcePath);
                     foreach (string file in fileList)
                     {
                         //如果是一个目录则
                         if (Directory.Exists(file))
                         {
                             CopyDirectory(file, destPath + Path.GetFileName(file));
                         }
                         else
                         {
                             File.Copy(file, destPath + Path.GetFileName(file),true);
                         }
                     }
                 }
                 catch(IOException ioe)
                 {
                     MessageBox.Show(ioe.Message, "复制文件时出错", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                 }
             }
         }
     }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表