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);
}
}
winform 各种小功能的更多相关文章
- C#、Java中的一些小功能点总结(持续更新......)
前言:在项目中,有时候一些小的功能点,总是容易让人忽略,但是这些功能加在项目中往往十分的有用,因此笔者在这里总结项目中遇到的一些实用的小功能点,以备用,并持续更新...... 1.禁用DataGrid ...
- C#之winform 猜拳小游戏
C#之winform 猜拳小游戏 1.建立项目文件 2.进行界面布局 2.1 玩家显示(控件:label) 2.2 显示玩家进行选择的控件(控件:label) 2.3 电脑显示(控件:label) ...
- js小功能整理
/** * 判断是否包含字符串某字符串 * @param {[type]} str [被检测的字符串] * @param {[type]} substr [检测是否含有的字符串] * @return ...
- iOS--知识综合应用成就时髦小功能点
iOS--知识综合应用成就时髦小功能点
- Java实现点击一个Jlabel增加一个Jlabel的小功能
当界面生成以后,自己想做一个点击一个Jlabel增加一个Jlabel,即类似于QQ的添加好友以后可以及时的加进一个好友.自己做了好久,发现不能及时刷新.在网上查了一下,然后自己研究了一小会.发现需要v ...
- Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据
用servlet实现一个注册的小功能 ,后台获取数据. 注册页面: 注册页面代码 : <!DOCTYPE html> <html> <head> <meta ...
- android 常用小功能(第二版)
经历过一段岁月,转眼2013的半年都过去了,第二版整理好的小功能,答应大家发布的,直到今日,终于和大家相见了,第二版没有第一版多,大家也可以去参考第一版的内容,希望大家使用愉快! 目录: 1.获取当前 ...
- NSFileManager文件操作的十个小功能
NSFileManager文件操作的十个小功能 NSFileManager是一个单列类,也是一个文件管理器.可以通过NSFileManager创建文件夹.创建文件.写文件.读文件内容等等基本功能. 下 ...
- javascript实现的一个信息提示的小功能/
//什么状况.CSDN的排版怎么这么多状况,还是本人太次?调整几次都没弄好.最后一遍了-- 最近由于公司业务问题.须要做一些面向公众的平台,于是对UI要求会高一点, 对于传统的alert的这样的方式来 ...
- Visual Studio Debugger中七个鲜为人知的小功能
Visual Studio debugger是一个很棒的调试工具,可以帮助程序猿们快速地发现和解决问题.这里给大家简单介绍一下VS调试工具中的七个鲜为人知的小功能. 1. 一键跳转到指定语句 调 ...
随机推荐
- [复习随笔]python_dcgan网络复习小知识:模型定义
定义参数 dataroot - the path to the root of the dataset folder. We will talk more about the dataset in t ...
- [ABC281G] Farthest City
Problem Statement You are given positive integers $N$ and $M$. Find the number, modulo $M$, of simpl ...
- C++ Qt开发:自定义Dialog对话框组件
Qt 是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍自定义Dial ...
- 2.elasticsearch中的mapping
mapping 顾名思义,代表了映射关系.是文档中字段和数据类型的映射关系 为什么要了解mapping 虽然elasticsearch中已尽有的动态mapping(Dynamic Mapping),而 ...
- C#开源的一款友好的.NET SDK管理器
前言 今天推荐一款由C#开源的.友好的.NET SDK管理器:Dots. 工具介绍 Dots 是一款 .NET SDK 管理器,可让您轻松安装.卸载和切换 .NET SDK.它是一款跨平台工具,可在 ...
- JavaFx之触发激发鼠标事件(二十三)
JavaFx之触发激发鼠标事件(二十三) 有时候,我们不能直接触发/激发某个按钮的点击事件,因为发起方可能是子线程.使用屏幕点击又不优雅,javafx已经提供了事件的激发,即使在子线程中也能激发某个按 ...
- 前端系列:ES6-ES12新语法
目录 ECMAScript系列:简介 ECMAScript系列:ES6新特性 let 关键字 const 关键字 变量的解构赋值 模板字符串 简化对象写法 箭头函数 参数默认值 rest 参数 spr ...
- 神经网络优化篇:详解指数加权平均数(Exponentially weighted averages)
指数加权平均数 比如这儿有去年伦敦的每日温度,所以1月1号,温度是40华氏度,相当于4摄氏度.世界上大部分地区使用摄氏度,但是美国使用华氏度.在1月2号是9摄氏度等等.在年中的时候,一年365天,年中 ...
- CSS 基础 2 - Box Model 盒模型
CSS 基础 2 - Box Model 盒模型 Box Model 盒模型是网页布局的前提.网页布局的几种方式: Table(过时):在 CSS 出现之前,一般用表格布局(将表格边框线隐藏) Flo ...
- Linux系统快速入门(完整版)
LINUX基础知识 I.Linux概述 linux是啥? 一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和Unix的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的Un ...