.NET/android/java/iOS AES通用加密解密
移动端越来越火了,我们在开发过程中,总会碰到要和移动端打交道的场景,比如.NET和android或者iOS的打交道。为了让数据交互更安全,我们需要对数据进行加密传输。今天研究了一下,把几种语言的加密都实践了一遍,实现了.NET,java(android),iOS都同一套的加密算法,下面就分享给大家。 AES加密有多种算法模式,下面提供两套模式的可用源码。 加密方式: 先将文本AES加密 返回Base64转码 解密方式: 将数据进行Base64解码 进行AES解密 一、CBC(Cipher Block Chaining,加密块链)模式 是一种循环模式,前一个分组的密文和当前分组的明文异或操作后再加密,这样做的目的是增强破解难度. 密钥 密钥偏移量 java/adroid加密AESOperator类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
package com.bci.wx.base.util; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * AES 是一种可逆加密算法,对用户的敏感信息加密处理 对原始数据进行AES加密后,在进行Base64编码转化; */ public class AESOperator { /* * 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式,key需要为16位。 */ private String sKey = "smkldospdosldaaa";//key,可自行修改 private String ivParameter = "0392039203920300";//偏移量,可自行修改 private static AESOperator instance = null; private AESOperator() { } public static AESOperator getInstance() { if (instance == null) instance = new AESOperator(); return instance; } public static String Encrypt(String encData ,String secretKey,String vector) throws Exception { if(secretKey == null) { return null; } if(secretKey.length() != 16) { return null; } Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] raw = secretKey.getBytes(); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); IvParameterSpec iv = new IvParameterSpec(vector.getBytes());// 使用CBC模式,需要一个向量iv,可增加加密算法的强度 cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(encData.getBytes("utf-8")); return new BASE64Encoder().encode(encrypted);// 此处使用BASE64做转码。 } // 加密 public String encrypt(String sSrc) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] raw = sKey.getBytes(); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// 使用CBC模式,需要一个向量iv,可增加加密算法的强度 cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8")); return new BASE64Encoder().encode(encrypted);// 此处使用BASE64做转码。 } // 解密 public String decrypt(String sSrc) throws Exception { try { byte[] raw = sKey.getBytes("ASCII"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes()); cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密 byte[] original = cipher.doFinal(encrypted1); String originalString = new String(original, "utf-8"); return originalString; } catch (Exception ex) { return null; } } public String decrypt(String sSrc,String key,String ivs) throws Exception { try { byte[] raw = key.getBytes("ASCII"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec iv = new IvParameterSpec(ivs.getBytes()); cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密 byte[] original = cipher.doFinal(encrypted1); String originalString = new String(original, "utf-8"); return originalString; } catch (Exception ex) { return null; } } public static String encodeBytes(byte[] bytes) { StringBuffer strBuf = new StringBuffer(); for (int i = 0; i < bytes.length; i++) { strBuf.append((char) (((bytes[i] >> 4) & 0xF) + ((int) 'a'))); strBuf.append((char) (((bytes[i]) & 0xF) + ((int) 'a'))); } return strBuf.toString(); } public static void main(String[] args) throws Exception { // 需要加密的字串 String cSrc = "[{\"request_no\":\"1001\",\"service_code\":\"FS0001\",\"contract_id\":\"100002\",\"order_id\":\"0\",\"phone_id\":\"13913996922\",\"plat_offer_id\":\"100094\",\"channel_id\":\"1\",\"activity_id\":\"100045\"}]"; // 加密 long lStart = System.currentTimeMillis(); String enString = AESOperator.getInstance().encrypt(cSrc); System.out.println("加密后的字串是:" + enString); long lUseTime = System.currentTimeMillis() - lStart; System.out.println("加密耗时:" + lUseTime + "毫秒"); // 解密 lStart = System.currentTimeMillis(); String DeString = AESOperator.getInstance().decrypt(enString); System.out.println("解密后的字串是:" + DeString); lUseTime = System.currentTimeMillis() - lStart; System.out.println("解密耗时:" + lUseTime + "毫秒"); } } |
.NET AES加密解密:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
using System; using System.Security.Cryptography; using System.Text; namespace AESDome { class Program { private static string key = "smkldospdosldaaa"; //key,可自行修改 private static string iv = "0392039203920300"; //偏移量,可自行修改 static void Main(string[] args) { string encrytpData = Encrypt("abc", key, iv); Console.WriteLine(encrytpData); string decryptData = Decrypt("5z9WEequVr7qtd+WoxV+Kw==", key, iv); Console.WriteLine(decryptData); Console.ReadLine(); } public static string Encrypt(string toEncrypt, string key, string iv) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key); byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv); byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt); RijndaelManaged rDel = new RijndaelManaged(); rDel.BlockSize = 128; rDel.KeySize = 256; rDel.FeedbackSize = 128; rDel.Padding = PaddingMode.PKCS7; rDel.Key = keyArray; rDel.IV = ivArray; rDel.Mode = CipherMode.CBC; ICryptoTransform cTransform = rDel.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } public static string Decrypt(string toDecrypt, string key, string iv) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key); byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv); byte[] toEncryptArray = Convert.FromBase64String(toDecrypt); // 这里的模式,请保持和上面加密的一样。但源代码里,这个地方并没有修正,虽然也能正确解密。看到博客的朋友,请自行修改。 // 这是个人疏忽的地址,感谢@jojoka 的提醒。 RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.IV = ivArray; rDel.Mode = CipherMode.CBC; rDel.Padding = PaddingMode.Zeros; rDel.BlockSize = 128; rDel.KeySize = 256; rDel.FeedbackSize = 128; rDel.Padding = PaddingMode.PKCS7; rDel.Key = keyArray; rDel.IV = ivArray; rDel.Mode = CipherMode.CBC; ICryptoTransform cTransform = rDel.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return UTF8Encoding.UTF8.GetString(resultArray); } } } |
iOS源码,请下载源码,源码里有包含。 java,.net,iOS,android通用AES加密解密源码:AES_CBC_ECB_android_java_ios_net通用模式 二、ECB(Electronic Code Book,电子密码本)模式 是一种基础的加密方式,密文被分割成分组长度相等的块(不足补齐),然后单独一个个加密,一个个输出组成密文。 只需要提供密码即可。 iOS,android,java已调通源码:AES_CBC_ECB_android_java_ios_net通用模式 AES在线加解密验证工具: http://www.seacha.com/tools/aes.html from:http://www.cnblogs.com/jys509/p/4768120.html
View Detailssvn服务器端回退版本
由于误操作,不小心将错误的代码提交到了svn上,于是想在服务器上撤销本次提交,经过尝试,发现进行以下步骤的操作即可彻底删除本次提交: 1.首先找到本次提交后生成的版本号,例如为r224. 2.登录到svn服务器上,进入到项目的svn目录. 3.进入db目录,删除此目录下的rep-cache.db。并修改此目录下的current文件,将其修改为上一个版本,例如233. 4.删除db/revs/0/目录下的224文件。 5.删除db/revprops/0/目录下的224文件。 至此,服务器端已经回退到r223版本,此时可以将本地下载到的代码删除,重新从svn服务器上下载一份代码,然后基于此代码继续修改提交。 如果在windows下使用TortoiseSVN,还需要删除本地的缓存,不然会出现再次提交后,其日志显示出现错误的情况,删除方法为点击TortoiseSVN的settings,在选择"Saved Data",点击Clear清除Log message即可。 from:http://blog.csdn.net/zm_21/article/details/25719087
View DetailsWindows 10正式版官方原版ISO镜像下载!
UPDATE(2015.11.13): Windows 10 TH2 11月更新正式推送后,微软发布了最新的ISO镜像下载工具,大家可使用该工具制作安装U盘/DVD光盘,同时网上也出现了ISO镜像的直接下载,均包括简体中文、英文等各种语言,需要的朋友赶紧收藏吧,可以直接替代RTM正式版了。 官方下载:Windows 10 TH2原版简体中文ISO镜像! 网络下载:Windows 10 TH2正式版官方原版镜像! 随着Windows 10升级推送开启,微软已经在官网发布了ISO镜像下载工具。大家可使用以下工具制作安装U盘/DVD。 开始之前,请确保: 1、稳定的网络连接; 2、足够的存储空间,硬盘、USB或者其它外部存储设备用于下载; 3、USB或者DVD(至少4GB空间)用于制作安装工具。 如果是首次安装,需要激活码(xxxxx-xxxxx-xxxxx-xxxxx-xxxxx),正版Win7/8.1用户可直接升级。 微软提供的下载工具包括: 1、优化的文件格式,可获得更快下载速度; 2、内建USB、DVD制作选项; 3、可转换ISO文件格式; Windows 10 ISO镜像下载工具:https://www.microsoft.com/zh-cn/software-download/windows10 update:鉴于很多用户反映微软服务器下载缓慢,我们将部分版本的ISO镜像传到了快科技的网盘,包括:64位简体中文家庭/专业版、32位简体中文家庭/专业版、64位简体中文企业版,后续版本持续更新。 欢迎大家下载: 《直接下载:Windows 10正式版官方原版镜像!》 from:http://news.mydrivers.com/1/440/440521.htm
View Detailssurfacepro系列平板重装系统详细教程
surfacepro的bios是uefi格式,常规的pe是无法进入的。因此我们必须找一个支持uef启动的pe镜像(地址:http://pan.baidu.com/s/1nt7l1H3)。下完以后先放一边,插上优盘,推荐4g以上。然后我们下载utraiso(地址:http://pan.baidu.com/s/1gd8JEM3),下载完成后打开软件。如图:选择”文件”-“打开”,找到我们刚下下载的那个pe镜像,双击确认。 2 然后再选择“启动”-“写入硬盘镜像”,如下图: 3 隐藏启动分区记得选“高端隐藏”,这样可以把pe的启动文件写入到创建的隐藏分区里面,不会误删,而且优盘可以正常存储其他内容。 4 完成以后拔出优盘,支持uefi启动的优盘就做成了。 END 方法/步骤2 1 接下来我们需要下载官方的恢复镜像(sp1:http://pan.baidu.com/s/1pJKEDDp,sp2:http://pan.baidu.com/s/1i3muWYx,sp3:http://pan.baidu.com/s/1gd1sjGB)然后后将恢复镜像解压,将sources文件夹里面的三个.swm文件单独提取出来放到一个文件夹里面。如下图: 然后下载Imagex命令行工具(http://pan.baidu.com/s/1hqGjjyk)解压以后选择安装卸载.exe,右键选择“以管理员身份运行”: 安装成功以后找到新建文件夹里面那三个.swm文件,选定其中一个,右键,选择“右键菜单二”-“合并swm”,如下图: 完成以后,新建文件夹里面会出现一个install.wim文件。 现在把优盘插入苏菲3,按音量-和开关键,等出现surface标志以后松开开关键,音量-按着不动,随后进入pe系统: 苏菲3初始系统有各类隐藏分区,这对于64g或者128g来说,隐藏分区占用了宝贵的硬盘空间,我们可以打开pe自带的diskgenius(此为普通版,可以下载专业版,http://pan.baidu.com/s/1o6zbZey),删除所有分区,保存更改。 然后选择“快速分区”分几个区按个人喜好,分区的时候记得勾选“创建新esp分区”,“对齐分区的整数倍”选择4096,然后点确定,如下图: 然后在d盘新建名为“backup”的文件夹,将install.win改名为“win8.1_pro”,放入backup文件夹里面。 然后我们返回桌面,打开”计算机”,看看是不是多出来一个97.7m的磁盘,如果没显示的话需要下载修复EFI分区工具,解压后打开“挂载eps分区”文件夹,双击运行“SHOW ESP.bat”。完成以后那个97.7m的磁盘空间应该就可以看见了。 然后我们找到pe自带的cmd,选择以管理员身份运行,下载这个dism命令文档(http://pan.baidu.com/s/1c0HDmYO),打开后复制如图那段命令,在cmd窗口粘贴,如图: 回车以后待进度到100提示完成,最后一步,打开修复EFI分区工具,选”自动修复efi分区”,打开软件,esp分区选择那个97.7m的盘符,然后点“开始修复”,选择c盘的windows目录,完成以后重启,至此安装结束。 该dism文档第二条命令是备份系统用的,系统整理完备以后,可以进pe系统后复制这条命令,系统就会自动在d盘backup文件夹创建名为“win8_pro”的镜像,会覆盖原文件,这点需要注意。 至于第三条命令,在正常系统中可以使用,功能是清理系统更新补丁后留下的陈旧备份文件,相当于垃圾清理,已管理员身份打开cmd,复制这条命令,回车即可。 附:没有备用键盘的同学,在pe里面敲不了回车的,可以下载这个虚拟键盘(http://pan.baidu.com/s/1ntyyb2L),打开即用。(注意:由于百度网盘误报文件有毒,有些压缩文件设了密码。统一为iqina) from:http://jingyan.baidu.com/article/ce09321b7d8e1e2bfe858f79.html
View Details微软发布Win10平板Surface Pro 4系统恢复镜像下载
IT之家讯 10月28日消息,微软官方网站开始提供Win10平板Surface Pro 4系统恢复镜像下载。从Win8开始Windows系统增加了原生重置功能,当你的设备系统出现无法修复的故障时可利用重置功能将系统恢复到出厂设置。针对Surface系列产品,微软还提供了专用的系统恢复镜像下载,以便在系统无法启动时使用。 点此进入微软官方下载页面,用户首先需要登录微软账户,之后输入设备序列号即可下载匹配的系统恢复镜像。根据微软官方页面的操作提示,用户可利用该镜像制作恢复驱动器,然后利用恢复驱动器恢复操作系统。 from:http://www.ithome.com/html/win10/185156.htm
View DetailsWPF编程学习——窗口
本文目录 1.窗口的外观 2.窗口的位置 3.窗口的大小 4.窗口的可见性和状态 5.窗口的生命周期 1.窗口的外观 WPF中默认窗口框架的外观,主要取决于Icon、Title、WindowsStyle、ResizeMode等属性。 Icon 指定窗口的图标; Title 指定窗口的标题; WindowStyle 指定窗口样式,有4个取值: None,无边框;(当ResizeMode属性为NoResize时,仅剩下窗口核心。) SingleBorderWindow,单边框【默认】; ThreeDBorderWindow,3D边框; ToolBorderWindow,工具箱窗口; ResizeMode 是指定大小调节样式,有4个取值: NoResize,不可调节,同时没有最大最小按钮; CanMinimize,不可调节。但可以最小化;(此时最大化按钮不可用) CanResize,可调节【默认】; CanResizeWithGrid,可根据网格调节;(窗口右下脚显示可调节网格) 2.窗口的位置 WindowStartLocation 指定窗口初始位置,有3个取值: Manual,手工指定位置,表示可以通过设置其Top、Left属性值来决定窗口的初始位置; CenterScreen,屏幕中央; CenterOwner,父窗体中央; TopMost 调节窗口的前后顺序,属性值为true时,窗口位于最前。 TopMost值为true的窗口,位于TopMost值为false的窗口之前(如下图记事本与示例窗口); TopMost值都为true的窗口,获得焦点的窗口位于前(如下图QQ与示例窗口)。 3.窗口的大小 Width、Height,分别表示窗口的宽度和高度,称为“尺寸属性”。 MaxWidth、MinWidth、MaxHeight、MinHeight,分别表示窗口最大宽度、最小宽度、最大高度、最小高度。可以通过得到和更改这些属性值,来获取和改变窗口的大小和长宽范围。 ActualWidth、ActualHeight,分别表示窗口的实际宽度和实际高度,称为“实际尺寸属性”。 实际尺寸属性是根据当前窗口大小、最小化时窗口大小和最大化时窗口大小来计算得到的,其值是只读的,也就是说,不能通过改变ActualWidth、ActualHeight的值来更改窗口大小。 SizeToContent,表示窗口大小由内容决定,有4个取值: Manual,手工【默认】; Width,窗体宽度由内容决定; Height,窗体高度由内容决定; WidthAndHeight,窗体大小由内容决定; 如果内容尺寸超过了窗口的最大或最小范围,还是以最大/最小范围为主。如果手工指定了窗口的Width、Height 属性,那么SizeToContent将被忽略。 ReSize,窗口大小的可调整性(第1部分已提到)。 4.窗口的可见性和状态 Visibility,窗口可见性,有4个枚举值: Visiable,可见; Hidden,隐藏; Collapsed,折叠。 虽然窗口类认为Collapsed与Hidden一样,但二者区别在于,Hidden仅仅将元素设为不可见,但是元素在画面上依然占有空间;而Collapsed,在不可视的基础上,能将元素在画面上的占位符清除,元素彻底不影响画面。 Show、Hide,显示窗口和隐藏窗口的两个方法。如果窗口的ShowInTaskbar属性值为true,Hide不但隐藏窗口本身,同时隐藏其在任务栏上的图标。 WindowState,窗口状态属性,有3个枚举值: Normal,正常; Maximized,最大化; Minimized,最小化; RestoreBounds,获取窗口在最小化或最大化之前的大小和位置,有4个枚举值,Top、Left、Width、Height。
1 2 3 4 |
<span style="color:#008000;line-height:1.5 !important;">//</span><span style="color:#008000;line-height:1.5 !important;">输出当前窗口的RestoreBounds值</span><span style="color:#008000;line-height:1.5 !important;"> </span><span style="color:#0000FF;line-height:1.5 !important;">private</span> <span style="color:#0000FF;line-height:1.5 !important;">void</span> button1_Click(<span style="color:#0000FF;line-height:1.5 !important;">object</span> sender, RoutedEventArgs e) { MessageBox.Show(<span style="color:#0000FF;line-height:1.5 !important;">this</span>.RestoreBounds.ToString()); } |
该主窗口的Top:75,Left:75,Width:525,Height:350 只有窗口在Normal状态下移动或调整时,RestoreBounds的值才会改变。于是可以在窗口关闭时将RestoreBounds属性值保存到配置文件,下一次启动程序窗口时,读取上次保存的窗口大小、位置,来初始化窗口,以此实现保存用户配置等功能。MSDN上的例子:http://msdn.microsoft.com/zh-cn/library/system.windows.window.restorebounds.aspx 。但推荐使用config文件来保存配置,更方便。 应用程序窗口在上次关闭处启动 向资源中添加两个变量MainRestoreBounds和MainWindowState,对应类型如图所示,用于保存主窗口的RestoreBounds属性值。 XAML
1 |
<span style="color:#0000FF;line-height:1.5 !important;"><</span><span style="color:#800000;line-height:1.5 !important;">Window </span><span style="color:#FF0000;line-height:1.5 !important;">x:Class</span><span style="color:#0000FF;line-height:1.5 !important;">="WpfApplication1.MainWindow"</span><span style="color:#FF0000;line-height:1.5 !important;"> xmlns</span><span style="color:#0000FF;line-height:1.5 !important;">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span><span style="color:#FF0000;line-height:1.5 !important;"> xmlns:x</span><span style="color:#0000FF;line-height:1.5 !important;">="http://schemas.microsoft.com/winfx/2006/xaml"</span><span style="color:#FF0000;line-height:1.5 !important;"> Width</span><span style="color:#0000FF;line-height:1.5 !important;">="360"</span><span style="color:#FF0000;line-height:1.5 !important;"> Height</span><span style="color:#0000FF;line-height:1.5 !important;">="240"</span><span style="color:#FF0000;line-height:1.5 !important;"> Closing</span><span style="color:#0000FF;line-height:1.5 !important;">="Window_Closing"</span><span style="color:#0000FF;line-height:1.5 !important;">></span> <span style="color:#0000FF;line-height:1.5 !important;"></</span><span style="color:#800000;line-height:1.5 !important;">Window</span><span style="color:#0000FF;line-height:1.5 !important;">></span> |
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span style="color:#0000FF;line-height:1.5 !important;">using</span> System; <span style="color:#0000FF;line-height:1.5 !important;">using</span> System.Collections.Generic; <span style="color:#0000FF;line-height:1.5 !important;">using</span> System.Linq; <span style="color:#0000FF;line-height:1.5 !important;">using</span> System.Text; <span style="color:#0000FF;line-height:1.5 !important;">using</span> System.Windows; <span style="color:#0000FF;line-height:1.5 !important;">using</span> System.Windows.Controls; <span style="color:#0000FF;line-height:1.5 !important;">using</span> System.Windows.Data; <span style="color:#0000FF;line-height:1.5 !important;">using</span> System.Windows.Documents; <span style="color:#0000FF;line-height:1.5 !important;">using</span> System.Windows.Input; <span style="color:#0000FF;line-height:1.5 !important;">using</span> System.Windows.Media; <span style="color:#0000FF;line-height:1.5 !important;">using</span> System.Windows.Media.Imaging; <span style="color:#0000FF;line-height:1.5 !important;">using</span> System.Windows.Navigation; <span style="color:#0000FF;line-height:1.5 !important;">using</span> System.Windows.Shapes; <span style="color:#0000FF;line-height:1.5 !important;">namespace</span> WpfApplication1 { <span style="color:#0000FF;line-height:1.5 !important;">public</span> <span style="color:#0000FF;line-height:1.5 !important;">partial</span> <span style="color:#0000FF;line-height:1.5 !important;">class</span> MainWindow : Window { <span style="color:#0000FF;line-height:1.5 !important;">public</span> MainWindow() { InitializeComponent(); <span style="color:#008000;line-height:1.5 !important;">//</span><span style="color:#008000;line-height:1.5 !important;">读取配置文件</span><span style="color:#008000;line-height:1.5 !important;"> </span> <span style="color:#0000FF;line-height:1.5 !important;">try</span> { <span style="color:#008000;line-height:1.5 !important;">//</span><span style="color:#008000;line-height:1.5 !important;">设置位置、大小</span><span style="color:#008000;line-height:1.5 !important;"> </span> Rect restoreBounds = Properties.Settings.Default.MainRestoreBounds; <span style="color:#0000FF;line-height:1.5 !important;">this</span>.WindowState = WindowState.Normal; <span style="color:#0000FF;line-height:1.5 !important;">this</span>.Left = restoreBounds.Left; <span style="color:#0000FF;line-height:1.5 !important;">this</span>.Top = restoreBounds.Top; <span style="color:#0000FF;line-height:1.5 !important;">this</span>.Width = restoreBounds.Width; <span style="color:#0000FF;line-height:1.5 !important;">this</span>.Height = restoreBounds.Height; <span style="color:#008000;line-height:1.5 !important;">//</span><span style="color:#008000;line-height:1.5 !important;">设置窗口状态</span><span style="color:#008000;line-height:1.5 !important;"> </span> <span style="color:#0000FF;line-height:1.5 !important;">this</span>.WindowState = Properties.Settings.Default.MainWindowState; } <span style="color:#0000FF;line-height:1.5 !important;">catch</span> { } } <span style="color:#0000FF;line-height:1.5 !important;">private</span> <span style="color:#0000FF;line-height:1.5 !important;">void</span> Window_Closing(<span style="color:#0000FF;line-height:1.5 !important;">object</span> sender, System.ComponentModel.CancelEventArgs e) { <span style="color:#008000;line-height:1.5 !important;">//</span><span style="color:#008000;line-height:1.5 !important;">保存当前位置、大小和状态,到配置文件</span><span style="color:#008000;line-height:1.5 !important;"> </span> Properties.Settings.Default.MainRestoreBounds = <span style="color:#0000FF;line-height:1.5 !important;">this</span>.RestoreBounds; Properties.Settings.Default.MainWindowState = <span style="color:#0000FF;line-height:1.5 !important;">this</span>.WindowState; Properties.Settings.Default.Save(); } } } |
5.窗口的生命周期 关于各事件的描述: Initialized:当窗口的FrameworkElement底层初始化时触发,即InitializeComponent方法调用时触发。 LocationChanged:窗口被移动时触发。 Activated:窗口被激活时触发。 Deactivated:窗口处于非激活时(即其他窗口处于激活时)触发。 Loaded:显示窗口之前触发。 ContentRendered:当内容显示的时候触发。 Closing:尝试关闭窗口时触发,可以将参数CancelEventArgs的Cancel的属性设置为true,取消关闭操作。 Closed:在窗口关闭后触发该事件,无法取消。 Unloaded:当关闭窗口并且从可视化树移除后触发。 from:http://www.cnblogs.com/libaoheng/archive/2011/11/18/2253751.html
View DetailsWPF中的Timer与DispatcherTimer的区别与应用
WPF / Silverlight中的 Timer 与 DispatcherTimer 有什么区别呢? 这里我给大家简单介绍一下他们在使用和实现上的区别。 在一个应用程序中,Timer会重复生成time事件,而DispatcherTimer是一个集成到了Dispatcher队列中的时钟,这可以使它被按照指定的时间间隔以指定的priority定期执行。 对于一个Timer时钟事件,系统并不能保证在时间间隔到达后被立即执行,但是能够确保在时间间隔到达之前不被执行。这是因为DispatcherTimer像其他操作一样被放置在了Dispatcher队列中。何时执行DispatcherTimer事件依赖于队列中的其他任务以及他们的优先级. 如果一个WPF应用程序使用了Timer时钟,那么它的事件必须在一个单独的时钟线程中运行,而不是在UI线程中,这对于WPF应用程序毫无用处——你没法在UI线程之外直接访问UI元素,而只能通过Invoke或者BeginInvoke将操作发送给Dispatcher 对象,委托Dispatcher去执行UI操作。 看到这里,你大概知道了为什么我们在WPF中应该用DispatcherTimer而不是Timer了:DispatcherTimer与Dispatcher运行于同一个线程中——UI线程,而且具有相同的DispatcherPriority优先级。 所以,在WPF/Silverlight应用中,正确的做法如下所示:
1 2 3 4 |
DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(10); //10秒 timer.Tick += new EventHandler(timer_Tick); timer.Start(); |
1 2 3 4 |
private void timer1_Tick(object sender, EventArgs e) { //(你的定时处理) } |
参考:http://www.roboby.com/the_different_bitween_timer_and_dispatchertimer_in_wpf.html
View Details修改placeholder的颜色
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
::-webkit-input-placeholder { /* WebKit browsers */ color: #e00; } :-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color: #e00; } ::-moz-placeholder { /* Mozilla Firefox 19+ */ color: #e00; } :-ms-input-placeholder { /* Internet Explorer 10+ */ color: #e00; } |
CHROME下去掉保存密码后输入框变成黄色背景样式
用阴影实现:
1 |
input:-webkit-autofill, input:-webkit-autofill:hover, input:-webkit-autofill:focus { box-shadow:0 0 0 60px #fff inset; -webkit-text-fill-color: #333; } |
如果不想保存密码的话加个autocomplete="off"到form或者input就行了。 from:http://jinzhe.net/post/26.html
View Details