国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#中拖放功能的實(shí)現(xiàn)

2023-05-18 12:33:13
字體:
供稿:網(wǎng)友

C#中的拖放功能使我們在做一些時(shí)變得非常方便,下面就以一個(gè)實(shí)例講解了具體的拖放操作的實(shí)現(xiàn)方法。

下面的代碼沒有給出注釋,加入了一個(gè)ListBox,當(dāng)文件拖放上來后,將內(nèi)容顯示在里面。

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

將整個(gè)窗體代碼都復(fù)制下來,是一個(gè)復(fù)制的小程序,將拖放到LISTBOX里的文件復(fù)制到文本框里指定的位置,里面用到了一個(gè)外部控件,可以使用普通的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
         {
             ///
             /// 獲得/設(shè)置一個(gè)值,判斷是否已經(jīng)存在了一個(gè)類的實(shí)例
             ///
             public static bool IsExist=false;

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

             private void frmDrag_MouseDown(object sender, MouseEventArgs e)
             {
                 //如果鼠標(biāo)指針在標(biāo)題欄范圍內(nèi)并且按下了鼠標(biāo)左鍵,則觸發(fā)移動(dòng)標(biāo)題欄方法
                 if (e.Button == MouseButtons.Left  e.Y
             /// 復(fù)制一個(gè)目錄下的所有文件或目錄到一個(gè)新的目錄下
             ///
             ///    源目錄路徑
             /// 目標(biāo)目錄路徑

             private void CopyDirectory(string sourcePath, string destPath)
             {
                 try
                 {
                     //如果目標(biāo)路徑?jīng)]有以/結(jié)尾則加之
                     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)
                     {
                         //如果是一個(gè)目錄則
                         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, "復(fù)制文件時(shí)出錯(cuò)", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                 }
             }
         }
     }

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 吴旗县| 福贡县| 遵义市| 安塞县| 疏附县| 关岭| 巴马| 万安县| 阳春市| 乌兰浩特市| 江门市| 清徐县| 枞阳县| 象山县| 乌拉特后旗| 南漳县| 莱阳市| 长白| 偏关县| 仁化县| 海城市| 噶尔县| 平山县| 连城县| 巴马| 琼结县| 磐石市| 新巴尔虎右旗| 中卫市| 白银市| 怀安县| 繁峙县| 兰州市| 浙江省| 高平市| 四会市| 疏勒县| 铜川市| 新丰县| 龙海市| 安义县|