在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例常用C#方法 → 多种winform窗体技巧(向窗体拖放图片并显示/仿XP系统的任务栏菜单)

多种winform窗体技巧(向窗体拖放图片并显示/仿XP系统的任务栏菜单)

常用C#方法

下载此实例
  • 开发语言:C#
  • 实例大小:3.21M
  • 下载次数:60
  • 浏览次数:600
  • 发布时间:2015-11-06
  • 实例类别:常用C#方法
  • 发 布 人:shitieshan
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 窗体

实例介绍

【实例简介】

【实例截图】




【核心代码】

片段一:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace 仿XP系统的任务栏菜单
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private static Panel Var_Panel = new Panel();
        private static PictureBox Var_Pict = new PictureBox();
        private static int Var_i = 0;
        private Font Var_Font = new Font("宋体", 9); 

        private void pictureBox_1_Click(object sender, EventArgs e)
        {
            Var_i = Convert.ToInt16(((PictureBox)sender).Tag.ToString());
            switch (Var_i)
            {
                case 1:
                    {
                        Var_Panel = panel_Gut_1;
                        Var_Pict = pictureBox_1;
                        break;
                    }
                case 2:
                    {
                        Var_Panel = panel_Gut_2;
                        Var_Pict = pictureBox_2;
                        break;
                    }
                case 3:
                    {
                        Var_Panel = panel_Gut_3;
                        Var_Pict = pictureBox_3;
                        break;
                    }

            }
            if (Convert.ToInt16(Var_Panel.Tag.ToString()) == 0 || Convert.ToInt16(Var_Panel.Tag.ToString()) == 2)
            {
                Var_Panel.Tag = 1;//隐藏标识
                Var_Pict.Image = null;
                Var_Pict.Image = Properties.Resources.朝下按钮;
                Var_Panel.Visible = false;
            }
            else
            {
                if (Convert.ToInt16(Var_Panel.Tag.ToString()) == 1)
                {
                    Var_Panel.Tag = 2;//显示标识
                    Var_Pict.Image = null;
                    Var_Pict.Image = Properties.Resources.朝上按钮;
                    Var_Panel.Visible = true;
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox_1.Image = null;
            pictureBox_1.Image = Properties.Resources.朝上按钮;
            pictureBox_2.Image = null;
            pictureBox_2.Image = Properties.Resources.朝上按钮;
            pictureBox_3.Image = null;
            pictureBox_3.Image = Properties.Resources.朝上按钮;
            Var_Font = label_1.Font;
        }

        private void label_1_MouseEnter(object sender, EventArgs e)
        {
            ((Label)sender).ForeColor = Color.Gray;
            ((Label)sender).Font = new Font(Var_Font, Var_Font.Style | FontStyle.Underline);
        }

        private void label_1_MouseLeave(object sender, EventArgs e)
        {
            ((Label)sender).ForeColor = Color.Black;
            ((Label)sender).Font = new Font(Var_Font, Var_Font.Style);
        }
    }
}


片段二:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;//添加的命名空间,对文件进行操作
using System.Threading;//线程序的命名空间

namespace 向窗体中拖放图片并显示
{
    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);//打开当前文件
            }

        }
    }
}


标签: 窗体

实例下载地址

多种winform窗体技巧(向窗体拖放图片并显示/仿XP系统的任务栏菜单)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警