实例介绍
【实例简介】
向窗体中拖放图片并显示出来,举一反三可实现QQ聊天时,向聊天窗口拖放文件的实例
【实例截图】

【核心代码】
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static bool Var_Style = true;
public static string tempstr="";
private System.Threading.Thread thdAddFile; //创建一个线程
private System.Threading.Thread thdOddDocument; //创建一个线程
public static TreeNode TN_Docu = new TreeNode();//单个文件的节点
private static TreeView Tem_TView;
/// <summary>
/// 在窗体背景中显示被拖放的图片
/// </summary>
/// <param Frm="Form">窗体</param>
/// <param e="DragEventArgs">DragDrop、DragEnter 或 DragOver 事件提供数据</param>
public void SetDragImageToFrm(Form Frm, DragEventArgs e)
{
if (Var_Style == true)
{
e.Effect = DragDropEffects.Copy;
String[] str_Drop = (String[])e.Data.GetData(DataFormats.FileDrop, true);
string tempstr;
Bitmap bkImage;
tempstr = str_Drop[0];
try
{
bkImage = new Bitmap(tempstr);
Frm.Size = new System.Drawing.Size(bkImage.Width 6, bkImage.Height 33);
Frm.BackgroundImage = bkImage;
}
catch { }
}
}
/// <summary>
/// 向TreeView控件添加被拖放的文件夹目录
/// </summary>
/// <param TV="TreeView">TreeView控件</param>
/// <param e="DragEventArgs">DragDrop、DragEnter 或 DragOver 事件提供数据</param>
public void SetDragImageToFrm(TreeView TV, DragEventArgs e)
{
if (Var_Style == false)
{
e.Effect = DragDropEffects.Copy;
String[] str_Drop = (String[])e.Data.GetData(DataFormats.FileDrop, true);
tempstr = str_Drop[0];//获取拖放文件夹的目录
thdAddFile = new Thread(new ThreadStart(SetAddFile)); //创建一个线程
thdAddFile.Start(); //执行当前线程
}
}
public delegate void AddFile();//定义托管线程
/// <summary>
/// 设置托管线程
/// </summary>
public void SetAddFile()
{
this.Invoke(new AddFile(RunAddFile));//对指定的线程进行托管
}
/// <summary>
/// 设置线程
/// </summary>
public void RunAddFile()
{
TreeNode TNode = new TreeNode();//实例化一个线程
Files_Copy(treeView1, tempstr, TNode, 0);
Thread.Sleep(0);//持起主线程
thdAddFile.Abort();//执行线程
}
#region 返回上一级目录
/// <summary>
/// 返回上一级目录
/// </summary>
/// <param dir="string">目录</param>
/// <returns>返回String对象</returns>
public string UpAndDown_Dir(string dir)
{
string Change_dir = "";
Change_dir = Directory.GetParent(dir).FullName;
return Change_dir;
}
#endregion
#region 显示文件夹下所有子文件夹及文件的名称
/// <summary>
/// 显示文件夹下所有子文件夹及文件的名称
/// </summary>
/// <param Sdir="string">文件夹的目录</param>
/// <param TNode="TreeNode">节点</param>
/// <param n="int">标识,判断当前是文件夹,还是文件</param>
private void Files_Copy(TreeView TV, string Sdir, TreeNode TNode, int n)
{
DirectoryInfo dir = new DirectoryInfo(Sdir);
try
{
if (!dir.Exists)//判断所指的文件或文件夹是否存在
{
return;
}
DirectoryInfo dirD = dir as DirectoryInfo;//如果给定参数不是文件夹则退出
if (dirD == null)//判断文件夹是否为空
{
return;
}
else
{
if (n == 0)
{
TNode = TV.Nodes.Add(dirD.Name);//添加文件夹的名称
TNode.Tag = 1;
}
else
{
TNode = TNode.Nodes.Add(dirD.Name);//添加文件夹里面各文件夹的名称
TNode.Tag = 1;
}
}
FileSystemInfo[] files = dirD.GetFileSystemInfos();//获取文件夹中所有文件和文件夹
//对单个FileSystemInfo进行判断,如果是文件夹则进行递归操作
foreach (FileSystemInfo FSys in files)
{
FileInfo file = FSys as FileInfo;
if (file != null)//如果是文件的话,进行文件的复制操作
{
FileInfo SFInfo = new FileInfo(file.DirectoryName "\\" file.Name);//获取文件所在的原始路径
TNode.Nodes.Add(file.Name);//添加文件
TNode.Tag = 1;
}
else
{
string pp = FSys.Name;//获取当前搜索到的文件夹名称
Files_Copy(TV, Sdir "\\" FSys.ToString(), TNode, 1);//如果是文件夹,则进行递归调用
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
#endregion
public void SetDragHandle(object sender, TreeView TV)
{
switch (Convert.ToInt16(((ToolStripMenuItem)sender).Tag.ToString()))
{
case 1:
{
panel_face.Visible = false;
Var_Style = true;
break;
}
case 2:
{
this.Width = 399;
this.Height = 272;
panel_face.Visible = true;
Var_Style = false;
break;
}
}
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
SetDragImageToFrm(this, e);
treeView1.Nodes.Clear();
SetDragImageToFrm(treeView1, e);
}
private void Tool_Ima_Click(object sender, EventArgs e)
{
SetDragHandle(sender, treeView1);
}
private void Form1_Load(object sender, EventArgs e)
{
Tem_TView = new TreeView();
Tem_TView = treeView1;
}
string Tem_Dir = "";
private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node.Tag == null)
Tem_Dir = "";
else
Tem_Dir = e.Node.Tag.ToString();
if (Tem_Dir == "")
{
Tem_Dir = UpAndDown_Dir(tempstr) "\\" e.Node.FullPath;
System.Diagnostics.Process.Start(@Tem_Dir);//打开当前文件
}
}
}
标签: QQ
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论