zoukankan
html css js c++ java
拖放操作和文件复制小功能
一直都没试过拖放操作的用处,今天试了试,发现真的很好用!!
下面的代码没有打入注释,加入了一个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
{
///
<summary>
///
获得/设置一个值,判断是否已经存在了一个类的实例
///
</summary>
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
<=
25
)
{
Program.ReleaseCapture();
Program.SendMessage(
this
.Handle, Program.WM_SYSCOMMAND, Program.SC_MOVE
+
Program.HTCAPTION,
0
);
}
}
private
void
picControlClose_Click(
object
sender, EventArgs e)
{
frmDrag.IsExist
=
false
;
this
.Close();
}
private
void
picControlMin_Click(
object
sender, EventArgs e)
{
this
.WindowState
=
FormWindowState.Minimized;
}
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);
}
}
private
void
ExBtnClose_ClickEvent(
object
sender, EventArgs e)
{
picControlClose_Click(
null
,
null
);
}
private
void
ExBtnClear_ClickEvent(
object
sender, EventArgs e)
{
lstFilePath.Items.Clear();
}
private
void
ExBtnDel_ClickEvent(
object
sender, EventArgs e)
{
lstFilePath.Items.RemoveAt(lstFilePath.SelectedIndex);
}
private
void
picControlBring_Click(
object
sender, EventArgs e)
{
if
(
this
.TopMost
==
true
)
{
this
.TopMost
=
false
;
picControlBring.Image
=
PersonalDisk.Properties.Resources.btnBottom;
}
else
{
this
.TopMost
=
true
;
picControlBring.Image
=
PersonalDisk.Properties.Resources.btnTop;
}
}
private
void
ExBtnExecute_ClickEvent(
object
sender, EventArgs e)
{
ExBtnExecute.CtlEnabled
=
false
;
for
(
int
i
=
0
; i
<
lstFilePath.Items.Count; i
++
)
{
Application.DoEvents();
lstFilePath.SetSelected(i,
true
);
//
如果当前字符串是一个目录则
.
if
(Directory.Exists(lstFilePath.Items[i].ToString()))
{
CopyDirectory(lstFilePath.Items[i].ToString(), txtSelDrive.Text
+
"
\\
"
+
Path.GetFileName(lstFilePath.Items[i].ToString()));
lstFilePath.Items[i]
=
"
复制完成
"
;
}
else
{
if
(
!
File.Exists(lstFilePath.Items[i].ToString()))
continue
;
//
如果文件不存在继续下一个循环
File.Copy(lstFilePath.Items[i].ToString(), txtSelDrive.Text
+
"
\\
"
+
Path.GetFileName(lstFilePath.Items[i].ToString()));
lstFilePath.Items[i]
=
"
复制完成
"
;
}
}
lstFilePath.Items.Clear();
ExBtnExecute.CtlEnabled
=
true
;
}
private
void
txtSelDrive_DoubleClick(
object
sender, EventArgs e)
{
FolderSelDialog.ShowDialog(
this
);
txtSelDrive.Text
=
FolderSelDialog.SelectedPath;
}
///
<summary>
///
复制一个目录下的所有文件或目录到一个新的目录下
///
</summary>
///
<param name="sourcePath">
源目录路径
</param>
///
<param name="destPath">
目标目录路径
</param>
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);
}
}
}
}
查看全文
相关阅读:
Alamofire 4.0 & swift 3.0 学习
源码阅读分析 (深入浅出FaceBook POP动画引擎)
iOS App Programming Guide 要点记录
UIKit 之UIViewController & UIView
ios AFNetworking 3.0 源码阅读分析 (四)(完结篇&&AFHTTPSessionManager模块)
ios AFNetworking 3.0 源码阅读分析 (三)(AFSecurityPolicy模块)
ios AFNetworking 3.0 原码阅读分析 (二)(AFURLResponseSerialization模块)
ios AFNetworking 3.0 原码阅读分析 (一)(AFURLRequestSerialization模块)
在商城系统中使用设计模式----策略模式之在spring中使用观察者模式和发布/订阅
在商城系统中使用设计模式----策略模式之在spring中使用策略模式
原文地址:https://www.cnblogs.com/top5/p/1723512.html
最新文章
内部查询处理器错误:查询处理器无法生成查询计划
easyui enableFilter combobox级联 combotree
C# INI文件读写类
C# Winform ListView实现单元格双击复制内容到剪贴板
MS SQL计算最大公约数和最小公倍数函数
测试连接失败,因为初始化提供程序时发生错误,[DBNMPNTW] ConnectionOpen (CreateFile())
VBS列出windows更新列表
C#嵌套类
你真的了解URL encode吗?
关于iOS SDK中HTTPS强制握手的经验分享
热门文章
NSURLProtocol 的坑
HokeyPokey — WWDC讲师特供XCode插件高仿版的设计与实现
iOS中如何优雅的添加圆角和边框?
简单逆向天猫的思考
天猫首页UI层次与类设计分析
准备做两件事 1.重现天猫首页.m代码 2.写个iosChart的GPU版
没事画画图
仿写及比较标哥的iOS时钟动画
音视频相关概念
iOS-Core-Animation-Advanced-Techniques 要点小结。
Copyright © 2011-2022 走看看