winform 各种小功能

1.  实现待查询功能的combox
 private void Frm_Main_Load(object sender, EventArgs e)
        {
            cbox_Find.Items.Clear();//清空ComboBox集合
            cbox_Find.Items.Add("C#编程词典");//向ComboBox集合添加元素
            cbox_Find.Items.Add("C#编程宝典");//向ComboBox集合添加元素
            cbox_Find.Items.Add("C#视频学");//向ComboBox集合添加元素
            cbox_Find.Items.Add("C#范例宝典");//向ComboBox集合添加元素
            cbox_Find.Items.Add("C#从入门到精通");//向ComboBox集合添加元素
            cbox_Find.Items.Add("C#范例大全");//向ComboBox集合添加元素
        }

        private void btn_Begin_Click(object sender, EventArgs e)
        {
            cbox_Find.AutoCompleteMode = //设置自动完成的模式
                AutoCompleteMode.SuggestAppend;
            cbox_Find.AutoCompleteSource = //设置自动完成字符串的源
                AutoCompleteSource.ListItems;
        }

2  在combox下拉列表中显示图片

        private ImageList G_ImageList;//声明ImageList字段


        private void cbox_DisplayPictures_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (G_ImageList != null)//判断ImageList是否为空
            {
                Graphics g = e.Graphics;//得到绘图对象
                Rectangle r = e.Bounds;//得到绘图范围
                Size imageSize = G_ImageList.ImageSize;//获取图像大小
                if (e.Index >= 0)//判断是否有绘制项
                {
                    Font fn = new Font("宋体", 10, FontStyle.Bold);//创建字体对象
                    string s = cbox_DisplayPictures.Items[e.Index].ToString();//得到绘制项的字符串
                    DrawItemState dis = e.State;
                    if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
                    {
                        e.Graphics.FillRectangle(new SolidBrush(Color.LightYellow), r);//画条目背景
                        G_ImageList.Draw(e.Graphics, r.Left, r.Top, e.Index);//绘制图像
                        e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black),//显示字符串
                            r.Left + imageSize.Width, r.Top);
                        e.DrawFocusRectangle();//显示取得焦点时的虚线框
                    }
                    else
                    {
                        e.Graphics.FillRectangle(new SolidBrush(Color.LightGreen), r);//画条目背景
                        G_ImageList.Draw(e.Graphics, r.Left, r.Top, e.Index);//绘制图像
                        e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black),//显示字符串 
                            r.Left + imageSize.Width, r.Top);
                        e.DrawFocusRectangle();//显示取得焦点时的虚线框 
                    }
                }
            }
        }

        private void btn_Begin_Click(object sender, EventArgs e)
        {
            btn_Begin.Enabled = false;//停用开始按钮
            cbox_DisplayPictures.DrawMode = DrawMode.OwnerDrawFixed;//设置绘制元素方式
            cbox_DisplayPictures.DropDownStyle = //设置组合框样式
                ComboBoxStyle.DropDownList;
            cbox_DisplayPictures.Items.Add("小车");//添加项
            cbox_DisplayPictures.Items.Add("卡车");//添加项
            cbox_DisplayPictures.Items.Add("工具");//添加项
            cbox_DisplayPictures.Items.Add("朋友");//添加项
            G_ImageList = new ImageList();//创建ImageList对象
            G_ImageList.Images.Add(global::PicturesInComboBox.Properties.Resources.a);//添加图片
            G_ImageList.Images.Add(global::PicturesInComboBox.Properties.Resources.b);//添加图片
            G_ImageList.Images.Add(global::PicturesInComboBox.Properties.Resources.c);//添加图片
            G_ImageList.Images.Add(global::PicturesInComboBox.Properties.Resources.d);//添加图片
        }

3.实现气泡提示窗口

        private void clewButton_Click(object sender,EventArgs e)
        {
            this.notifyIcon1.Visible = true;//设置提示控件可见
            this.notifyIcon1.ShowBalloonTip(1000,"当前时间:",DateTime.Now.ToLocalTime().ToString(),ToolTipIcon.Info);//显示气泡提示
        }

        private void closeButton_Click(object sender,EventArgs e)
        {
            this.notifyIcon1.Visible = false;//设置提示控件不可见
        }

        private void notifyIcon1_MouseMove(object sender,MouseEventArgs e)
        {
            this.notifyIcon1.ShowBalloonTip(1000, "当前时间:", DateTime.Now.ToLocalTime().ToString(), ToolTipIcon.Info);//显示气泡提示
        }

 4.在状态栏实时显示系统运行时间

        private DateTime G_DateTime;//声明时间字段

        private void Frm_Main_Load(object sender, EventArgs e)
        {
            G_DateTime = DateTime.Now;//得到系统当前时间
            Thread P_th = new Thread(//创建线程
                () =>//使用Lambda表达式
                {
                    while (true)//无限循环
                    {
                        TimeSpan P_TimeSpan =//得到时间差
                            DateTime.Now - G_DateTime;
                        Invoke(//调用窗体线程
                            (MethodInvoker)(() =>//使用Lambda表达式
                            {
                                tssLabel_Time.Text =//显示程序启动时间
                                    string.Format(
                                    "系统已经运行: {0}天{1}小时{2}分{3}秒",
                                    P_TimeSpan.Days, P_TimeSpan.Hours, 
                                    P_TimeSpan.Minutes, P_TimeSpan.Seconds);
                            }));
                        Thread.Sleep(1000);//线程挂起1秒钟
                    }
                });
            P_th.IsBackground = true;//设置为后台线程
            P_th.Start();//开始执行线程
        }

 5. 打开指定word文档

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "*.doc|*.doc|*.docx|*.docx";//筛选文件
            openFileDialog1.InitialDirectory = @"I:\软谋12后端\20190424Advanced12Course47SOA-WebServcie\20190424Advanced12Course47SOA-WebServcie\";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)//弹出打开文件对话框
            {
                string fileName = openFileDialog1.FileName;
                
                Process.Start(fileName);
            }
        }

 6. 利用图片对文本进行加密解密

        //打开图片
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "jpg,bmp,gif|*.jpg;*.gif;*.bmp";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.ImageLocation = openFileDialog1.FileName;
            }
        }
        //打开加密文件
        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "文本文件|*.txt";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
            }
        }
        // 加密
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                if (pictureBox1.ImageLocation == null)
                { MessageBox.Show("请选择一幅图片用于加密"); return; }
                if (textBox1.Text == "")
                { MessageBox.Show("请选择加密文件路径"); return; }
                //图片流
                FileStream fsPic = new FileStream(pictureBox1.ImageLocation, FileMode.Open, FileAccess.Read);
                //加密文件流
                FileStream fsText = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
                //初始化Key IV
                byte[] bykey = new byte[16];
                byte[] byIv = new byte[8];
                fsPic.Read(bykey, 0, 16);
                fsPic.Read(byIv, 0, 8);
                //临时加密文件
                string strPath = textBox1.Text;//加密文件的路径
                int intLent = strPath.LastIndexOf("\\") + 1;
                int intLong = strPath.Length;
                string strName = strPath.Substring(intLent, intLong - intLent);//要加密的文件名称
                string strLinPath = "C:\\" + strName;//临时加密文件路径,所以被加密的文件不可以放在C盘的根目录下
                FileStream fsOut = File.Open(strLinPath, FileMode.Create, FileAccess.Write);
                //开始加密
                RC2CryptoServiceProvider desc = new RC2CryptoServiceProvider();//des进行加
                BinaryReader br = new BinaryReader(fsText);//从要加密的文件中读出文件内容
                CryptoStream cs = new CryptoStream(fsOut, desc.CreateEncryptor(bykey, byIv), CryptoStreamMode.Write);//写入临时加密文件
                cs.Write(br.ReadBytes((int)fsText.Length), 0, (int)fsText.Length);//写入加密流
                cs.FlushFinalBlock();
                cs.Flush();
                cs.Close();
                fsPic.Close();
                fsText.Close();
                fsOut.Close();
                File.Delete(textBox1.Text.TrimEnd());//册除原文件
                File.Copy(strLinPath, textBox1.Text);//复制加密文件
                File.Delete(strLinPath);//册除临时文件
                MessageBox.Show("加密成功");
                pictureBox1.ImageLocation = null;
                textBox1.Text = "";
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }

        }

        //解密
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                //图片流
                FileStream fsPic = new FileStream(pictureBox1.ImageLocation, FileMode.Open, FileAccess.Read);
                //解密文件流
                FileStream fsOut = File.Open(textBox1.Text, FileMode.Open, FileAccess.Read);
                //初始化Key IV
                byte[] bykey = new byte[16];
                byte[] byIv = new byte[8];
                fsPic.Read(bykey, 0, 16);
                fsPic.Read(byIv, 0, 8);
                //临时解密文件
                string strPath = textBox1.Text;//加密文件的路径
                int intLent = strPath.LastIndexOf("\\") + 1;
                int intLong = strPath.Length;
                string strName = strPath.Substring(intLent, intLong - intLent);//要加密的文件名称
                string strLinPath = "C:\\" + strName;//临时解密文件路径
                FileStream fs = new FileStream(strLinPath, FileMode.Create, FileAccess.Write);
                //开始解密
                RC2CryptoServiceProvider desc = new RC2CryptoServiceProvider();//des进行解
                CryptoStream csDecrypt = new CryptoStream(fsOut, desc.CreateDecryptor(bykey, byIv), CryptoStreamMode.Read);//读出加密文件
                BinaryReader sr = new BinaryReader(csDecrypt);//从要加密流中读出文件内容
                BinaryWriter sw = new BinaryWriter(fs);//写入解密流
                sw.Write(sr.ReadBytes(Convert.ToInt32(fsOut.Length)));//
                sw.Flush();
                sw.Close();
                sr.Close();
                fs.Close();
                fsOut.Close();
                fsPic.Close();
                csDecrypt.Flush();

                File.Delete(textBox1.Text.TrimEnd());//册除原文件
                File.Copy(strLinPath, textBox1.Text);//复制加密文件
                File.Delete(strLinPath);//册除临时文件
                MessageBox.Show("解密成功");
                pictureBox1.ImageLocation = null;
                textBox1.Text = "";
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }

 

posted @ 2022-08-03 08:46  明志德道  阅读(268)  评论(0编辑  收藏  举报