实例介绍
【实例简介】生成加密解密文件
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace EncryptFileTool
{
public partial class KeysightForm : FormBase
{
#region Field
//窗体圆角矩形半径
private int _radius = 5;
//是否允许窗体改变大小
private bool _canResize = true;
private Image _fringe = Image.FromFile(@"Res/fringe_bkg.png");
private Image _formBkg = Image.FromFile(@"Res/FormBkg/Kesight bg.jpg");
private Button btnDecryptFile;
private Button btnEncryptFile;
private TextBox txtPassword;
private Label label2;
private Button btnSelectFile;
private TextBox txtFileName;
private Label label1;
private OpenFileDialog openFileDialog1;
//系统按钮管理器
private SystemButtonManager _systemButtonManager;
#endregion
public KeysightForm()
{
InitializeComponent();
this.CenterToScreen();
FormExIni();
_systemButtonManager = new SystemButtonManager(this);
this.CanResize = false;
}
#region Properties
[DefaultValue(typeof(byte), "5")]
public int Radius
{
get
{
return _radius;
}
set
{
if (_radius != value)
{
_radius = value;
this.Invalidate();
}
}
}
public bool CanResize
{
get
{
return _canResize;
}
set
{
if (_canResize != value)
{
_canResize = value;
}
}
}
public override Image BackgroundImage
{
get
{
return _formBkg;
}
set
{
if (_formBkg != value)
{
_formBkg = value;
Invalidate();
}
}
}
internal Rectangle IconRect
{
get
{
if (base.ShowIcon && base.Icon != null)
{
return new Rectangle(8, 6, SystemInformation.SmallIconSize.Width, SystemInformation.SmallIconSize.Width);
}
return Rectangle.Empty;
}
}
internal Rectangle TextRect
{
get
{
if (base.Text.Length != 0)
{
return new Rectangle(IconRect.Right 2, 4, Width - (8 IconRect.Width 2), Font.Height);
}
return Rectangle.Empty;
}
}
internal SystemButtonManager SystemButtonManager
{
get
{
if (_systemButtonManager == null)
{
_systemButtonManager = new SystemButtonManager(this);
}
return _systemButtonManager;
}
}
#endregion
#region Overrides
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
if (!DesignMode)
{
if (MaximizeBox) { cp.Style |= (int)WindowStyle.WS_MAXIMIZEBOX; }
if (MinimizeBox) { cp.Style |= (int)WindowStyle.WS_MINIMIZEBOX; }
//cp.ExStyle |= (int)WindowStyle.WS_CLIPCHILDREN; //防止因窗体控件太多出现闪烁
cp.ClassStyle |= (int)ClassStyle.CS_DropSHADOW; //实现窗体边框阴影效果
}
return cp;
}
}
protected override void OnCreateControl()
{
base.OnCreateControl();
RenderHelper.SetFormRoundRectRgn(this, Radius);
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
RenderHelper.SetFormRoundRectRgn(this, Radius);
UpdateSystemButtonRect();
UpdateMaxButton();
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case Win32.WM_ERASEBKGND:
m.Result = IntPtr.Zero;
break;
case Win32.WM_NCHITTEST:
WmNcHitTest(ref m);
break;
default:
base.WndProc(ref m);
break;
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
SystemButtonManager.ProcessMouseOperate(e.Location, MouseOperate.Move);
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
SystemButtonManager.ProcessMouseOperate(e.Location, MouseOperate.Down);
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (e.Button == MouseButtons.Left)
{
SystemButtonManager.ProcessMouseOperate(e.Location, MouseOperate.Up);
}
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
SystemButtonManager.ProcessMouseOperate(Point.Empty, MouseOperate.Leave);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
//draw BackgroundImage
e.Graphics.DrawImage(_formBkg, ClientRectangle, new Rectangle(0, 0, _formBkg.Width, _formBkg.Height), GraphicsUnit.Pixel);
//draw form main part
RenderHelper.DrawFromAlphaMainPart(this, e.Graphics);
//draw system buttons
SystemButtonManager.DrawSystemButtons(e.Graphics);
//draw fringe
RenderHelper.DrawFormFringe(this, e.Graphics, _fringe, Radius);
//draw icon
if (Icon != null && ShowIcon)
{
e.Graphics.DrawIcon(Icon, IconRect);
}
//draw text
if (Text.Length != 0)
{
TextRenderer.DrawText(
e.Graphics,
Text, Font,
TextRect,
Color.White,
TextFormatFlags.SingleLine | TextFormatFlags.EndEllipsis);
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
if (_systemButtonManager != null)
{
_systemButtonManager.Dispose();
_systemButtonManager = null;
_formBkg.Dispose();
_formBkg = null;
_fringe.Dispose();
_fringe = null;
}
}
}
#endregion
#region Private Methods
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(KeysightForm));
this.btnDecryptFile = new System.Windows.Forms.Button();
this.btnEncryptFile = new System.Windows.Forms.Button();
this.txtPassword = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.btnSelectFile = new System.Windows.Forms.Button();
this.txtFileName = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// btnDecryptFile
//
this.btnDecryptFile.BackColor = System.Drawing.Color.Gainsboro;
this.btnDecryptFile.FlatAppearance.BorderSize = 0;
this.btnDecryptFile.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnDecryptFile.Location = new System.Drawing.Point(380, 200);
this.btnDecryptFile.Name = "btnDecryptFile";
this.btnDecryptFile.Size = new System.Drawing.Size(83, 36);
this.btnDecryptFile.TabIndex = 13;
this.btnDecryptFile.Text = "解密";
this.btnDecryptFile.UseVisualStyleBackColor = false;
this.btnDecryptFile.Click = new System.EventHandler(this.btnDecryptFile_Click);
//
// btnEncryptFile
//
this.btnEncryptFile.BackColor = System.Drawing.Color.Gainsboro;
this.btnEncryptFile.FlatAppearance.BorderSize = 0;
this.btnEncryptFile.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnEncryptFile.Location = new System.Drawing.Point(32, 200);
this.btnEncryptFile.Name = "btnEncryptFile";
this.btnEncryptFile.Size = new System.Drawing.Size(83, 36);
this.btnEncryptFile.TabIndex = 12;
this.btnEncryptFile.Text = "加密";
this.btnEncryptFile.UseVisualStyleBackColor = false;
this.btnEncryptFile.Click = new System.EventHandler(this.btnEncryptFile_Click);
//
// txtPassword
//
this.txtPassword.BackColor = System.Drawing.SystemColors.Window;
this.txtPassword.Location = new System.Drawing.Point(157, 75);
this.txtPassword.Name = "txtPassword";
this.txtPassword.Size = new System.Drawing.Size(179, 25);
this.txtPassword.TabIndex = 11;
//
// label2
//
this.label2.AutoSize = true;
this.label2.BackColor = System.Drawing.Color.Transparent;
this.label2.Location = new System.Drawing.Point(28, 78);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(118, 19);
this.label2.TabIndex = 10;
this.label2.Text = "输入密码 (可选择):";
//
// btnSelectFile
//
this.btnSelectFile.BackColor = System.Drawing.Color.Gainsboro;
this.btnSelectFile.FlatAppearance.BorderSize = 0;
this.btnSelectFile.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnSelectFile.Location = new System.Drawing.Point(380, 34);
this.btnSelectFile.Name = "btnSelectFile";
this.btnSelectFile.Size = new System.Drawing.Size(83, 25);
this.btnSelectFile.TabIndex = 9;
this.btnSelectFile.Text = "选择文件";
this.btnSelectFile.UseVisualStyleBackColor = false;
this.btnSelectFile.Click = new System.EventHandler(this.btnSelectFile_Click);
//
// txtFileName
//
this.txtFileName.Location = new System.Drawing.Point(157, 34);
this.txtFileName.Name = "txtFileName";
this.txtFileName.ReadOnly = true;
this.txtFileName.Size = new System.Drawing.Size(179, 25);
this.txtFileName.TabIndex = 8;
//
// label1
//
this.label1.AutoSize = true;
this.label1.BackColor = System.Drawing.Color.Transparent;
this.label1.Location = new System.Drawing.Point(25, 38);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 19);
this.label1.TabIndex = 7;
this.label1.Text = "选择操作文件:";
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
//
// KeysightForm
//
this.ClientSize = new System.Drawing.Size(513, 350);
this.Controls.Add(this.btnDecryptFile);
this.Controls.Add(this.btnEncryptFile);
this.Controls.Add(this.txtPassword);
this.Controls.Add(this.label2);
this.Controls.Add(this.btnSelectFile);
this.Controls.Add(this.txtFileName);
this.Controls.Add(this.label1);
this.Font = new System.Drawing.Font("Microsoft YaHei", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "KeysightForm";
this.Text = "加密解密";
this.ResumeLayout(false);
this.PerformLayout();
}
private void FormExIni()
{
this.MaximumSize = Screen.PrimaryScreen.WorkingArea.Size;
SetStyles();
}
private void SetStyles()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
UpdateStyles();
}
private void WmNcHitTest(ref Message m) //调整窗体大小
{
int wparam = m.LParam.ToInt32();
Point mouseLocation = new Point(RenderHelper.LOWORD(wparam), RenderHelper.HIWORD(wparam));
mouseLocation = PointToClient(mouseLocation);
if (WindowState != FormWindowState.Maximized)
{
if (CanResize == true)
{
if (mouseLocation.X < 5 && mouseLocation.Y < 5)
{
m.Result = new IntPtr(Win32.HTTOPLEFT);
return;
}
if (mouseLocation.X > Width - 5 && mouseLocation.Y < 5)
{
m.Result = new IntPtr(Win32.HTTOPRIGHT);
return;
}
if (mouseLocation.X < 5 && mouseLocation.Y > Height - 5)
{
m.Result = new IntPtr(Win32.HTBOTTOMLEFT);
return;
}
if (mouseLocation.X > Width - 5 && mouseLocation.Y > Height - 5)
{
m.Result = new IntPtr(Win32.HTBOTTOMRIGHT);
return;
}
if (mouseLocation.Y < 3)
{
m.Result = new IntPtr(Win32.HTTOP);
return;
}
if (mouseLocation.Y > Height - 3)
{
m.Result = new IntPtr(Win32.HTBOTTOM);
return;
}
if (mouseLocation.X < 3)
{
m.Result = new IntPtr(Win32.HTLEFT);
return;
}
if (mouseLocation.X > Width - 3)
{
m.Result = new IntPtr(Win32.HTRIGHT);
return;
}
}
}
m.Result = new IntPtr(Win32.HTCLIENT);
}
private void UpdateMaxButton() //根据窗体的状态更换最大(还原)系统按钮
{
bool isMax = WindowState == FormWindowState.Maximized;
if (isMax)
{
SystemButtonManager.SystemButtonArray[1].NormalImg = Image.FromFile(@"Res\SystemButton\restore_normal.png");
SystemButtonManager.SystemButtonArray[1].HighLightImg = Image.FromFile(@"Res\SystemButton\restore_highlight.png");
SystemButtonManager.SystemButtonArray[1].PressedImg = Image.FromFile(@"Res\SystemButton\restore_press.png");
SystemButtonManager.SystemButtonArray[1].ToolTip = "还原";
}
else
{
SystemButtonManager.SystemButtonArray[1].NormalImg = Image.FromFile(@"Res\SystemButton\max_normal.png");
SystemButtonManager.SystemButtonArray[1].HighLightImg = Image.FromFile(@"Res\SystemButton\max_highlight.png");
SystemButtonManager.SystemButtonArray[1].PressedImg = Image.FromFile(@"Res\SystemButton\max_press.png");
SystemButtonManager.SystemButtonArray[1].ToolTip = "最大化";
}
}
protected void UpdateSystemButtonRect()
{
bool isShowMaxButton = MaximizeBox;
bool isShowMinButton = MinimizeBox;
Rectangle closeRect = new Rectangle(
Width - SystemButtonManager.SystemButtonArray[0].NormalImg.Width,
-1,
SystemButtonManager.SystemButtonArray[0].NormalImg.Width,
SystemButtonManager.SystemButtonArray[0].NormalImg.Height);
//update close button location rect.
SystemButtonManager.SystemButtonArray[0].LocationRect = closeRect;
//Max
if (isShowMaxButton)
{
SystemButtonManager.SystemButtonArray[1].LocationRect = new Rectangle(
closeRect.X - SystemButtonManager.SystemButtonArray[1].NormalImg.Width,
-1,
SystemButtonManager.SystemButtonArray[1].NormalImg.Width,
SystemButtonManager.SystemButtonArray[1].NormalImg.Height);
}
else
{
SystemButtonManager.SystemButtonArray[1].LocationRect = Rectangle.Empty;
}
//Min
if (!isShowMinButton)
{
SystemButtonManager.SystemButtonArray[2].LocationRect = Rectangle.Empty;
return;
}
if (isShowMaxButton)
{
SystemButtonManager.SystemButtonArray[2].LocationRect = new Rectangle(
SystemButtonManager.SystemButtonArray[1].LocationRect.X - SystemButtonManager.SystemButtonArray[2].NormalImg.Width,
-1,
SystemButtonManager.SystemButtonArray[2].NormalImg.Width,
SystemButtonManager.SystemButtonArray[2].NormalImg.Height);
}
else
{
SystemButtonManager.SystemButtonArray[2].LocationRect = new Rectangle(
closeRect.X - SystemButtonManager.SystemButtonArray[2].NormalImg.Width,
-1,
SystemButtonManager.SystemButtonArray[2].NormalImg.Width,
SystemButtonManager.SystemButtonArray[2].NormalImg.Height);
}
}
#endregion
private void btnSelectFile_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = openFileDialog1.FileName;
}
}
private void btnEncryptFile_Click(object sender, EventArgs e)
{
string inFile = txtFileName.Text;
if (string.IsNullOrEmpty(inFile))
{
MessageBox.Show("请选择加密文件");
return;
}
string outFile = inFile ".dat";
string password = txtPassword.Text;
try
{
DESFile.Encrypt(inFile, outFile, password);
File.Delete(inFile);
txtFileName.Text = string.Empty;
MessageBox.Show("加密成功");
}
catch (Exception)
{
}
}
private void btnDecryptFile_Click(object sender, EventArgs e)
{
string inFile = txtFileName.Text;
if (string.IsNullOrEmpty(inFile))
{
MessageBox.Show("请选择解密文件");
return;
}
string outFile = inFile.Substring(0, inFile.Length - 4);
string password = txtPassword.Text;
try
{
DESFile.DecryptFile(inFile, outFile, password);
File.Delete(inFile);
txtFileName.Text = string.Empty;
MessageBox.Show("解密成功");
}
catch (Exception)
{
}
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论