📄 xpasswordedit.cs
字号:
namespace Imps.Client.Pc.Controls
{
using Imps.Client.Resource;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
public class XPasswordEdit : XTextBox
{
private IContainer components;
private char defaultPasswordChar = '*';
public DlgtEncryptString DoEncrypt;
private Dictionary<int, char> encryptedHash = new Dictionary<int, char>();
private string encryptedText = string.Empty;
private string modifyPasswordTip = string.Empty;
private int passwordMaxLen = 0x10;
private int passwordMinLen = 6;
public XPasswordEdit()
{
this.InitializeComponent();
this.PasswordMinLen = 6;
this.PasswordMaxLen = 0x10;
this.PasswordChar = this.defaultPasswordChar;
}
public void Clear()
{
base.Text = string.Empty;
this.encryptedText = string.Empty;
this.encryptedHash.Clear();
}
public string GetHashPassword()
{
int num = this.encryptedHash.get_Count();
if ((this.encryptedText != string.Empty) && (num == 0))
{
return this.encryptedText;
}
if (num == 0)
{
return string.Empty;
}
char[] chArray = new char[num];
for (int i = 0; i < num; i++)
{
this.encryptedHash.TryGetValue(i, ref chArray[i]);
}
if (this.DoEncrypt == null)
{
return new string(chArray);
}
byte[] plainText = Encoding.UTF8.GetBytes(chArray);
string text = this.DoEncrypt(plainText);
for (int j = 0; j < plainText.Length; j++)
{
plainText[j] = 0;
}
for (int k = 0; k < chArray.Length; k++)
{
chArray[k] = '\0';
}
return text;
}
[DllImport("imm32.dll")]
public static extern IntPtr ImmAssociateContext(IntPtr hWnd, IntPtr hImc);
private void InitializeComponent()
{
base.SuspendLayout();
base.Name = "PasswordEdit";
base.Size = new Size(0x6b, 0x15);
base.Enter += new EventHandler(this.XPasswordEdit_Enter);
base.KeyPress += new KeyPressEventHandler(this.XPasswordEdit_KeyPress);
base.TextChanged += new EventHandler(this.XPasswordEdit_TextChanged);
base.KeyDown += new KeyEventHandler(this.XPasswordEdit_KeyDown);
base.ResumeLayout(false);
}
private static bool IsPasswordChar(char ch)
{
int num = ch;
if (num >= 0x21)
{
return (num <= 0x7e);
}
return false;
}
public bool IsValidPassword(string pass)
{
if ((pass.Length < this.passwordMinLen) || (pass.Length > this.passwordMaxLen))
{
return false;
}
foreach (char ch in pass)
{
if (!IsPasswordChar(ch))
{
return false;
}
}
return true;
}
protected override void WndProc(ref Message m)
{
if (m.Msg != 0x7b)
{
base.WndProc(ref m);
if (m.Msg == 15)
{
using (Graphics graphics = Graphics.FromHwnd(base.Handle))
{
int num = this.encryptedHash.get_Count();
if (((this.encryptedText != string.Empty) && (num == 0)) && (!string.IsNullOrEmpty(this.modifyPasswordTip) && !this.Focused))
{
TextRenderer.DrawText((IDeviceContext) graphics, this.modifyPasswordTip, this.Font, base.ClientRectangle, base.EmptyTextTipColor, 0x8004);
}
}
}
}
}
private void XPasswordEdit_Enter(object sender, EventArgs e)
{
base.ImeMode = ImeMode.Disable;
}
private void XPasswordEdit_KeyDown(object sender, KeyEventArgs e)
{
Keys keyCode = e.KeyCode;
if (keyCode != Keys.Delete)
{
if ((keyCode == Keys.A) && e.Control)
{
base.SelectAll();
}
}
else if (base.SelectionStart < base.Text.Length)
{
if (this.SelectionLength == 0)
{
ReArrangeHash.Remove(this.encryptedHash, base.SelectionStart, 1);
}
else if (this.SelectionLength > 0)
{
ReArrangeHash.Remove(this.encryptedHash, base.SelectionStart, this.SelectionLength);
}
}
}
private void XPasswordEdit_KeyPress(object sender, KeyPressEventArgs e)
{
if (!IsPasswordChar(e.KeyChar))
{
if (e.KeyChar != '\b')
{
e.Handled = true;
}
else if ((base.SelectionStart > 0) && (this.SelectionLength == 0))
{
ReArrangeHash.Remove(this.encryptedHash, base.SelectionStart - 1, 1);
}
else if (this.SelectionLength > 0)
{
ReArrangeHash.Remove(this.encryptedHash, base.SelectionStart, this.SelectionLength);
}
}
else
{
int num = this.encryptedHash.get_Count();
if ((this.encryptedText != string.Empty) && (num == 0))
{
this.encryptedText = string.Empty;
base.EmptyTextTip = StringTable.Password.EmptyPasswordTipText;
}
if (this.SelectionLength > 0)
{
ReArrangeHash.Remove(this.encryptedHash, base.SelectionStart, this.SelectionLength);
}
else if (num == this.MaxLength)
{
e.Handled = true;
return;
}
ReArrangeHash.Add(this.encryptedHash, base.SelectionStart, e.KeyChar);
e.set_KeyChar(this.PasswordChar);
}
}
private void XPasswordEdit_TextChanged(object sender, EventArgs e)
{
this.encryptedHash.get_Count();
int length = base.Text.Length;
}
public bool AutoInputed
{
get
{
return (this.encryptedHash.get_Count() == 0);
}
}
public string ModifyPasswordTip
{
get
{
return this.modifyPasswordTip;
}
set
{
this.modifyPasswordTip = value;
}
}
public char PasswordChar
{
get
{
if (base.PasswordChar == '\0')
{
return this.defaultPasswordChar;
}
return base.PasswordChar;
}
set
{
base.PasswordChar = value;
}
}
[DefaultValue(0x10)]
public int PasswordMaxLen
{
get
{
return this.passwordMaxLen;
}
set
{
try
{
this.passwordMaxLen = value;
}
catch
{
}
}
}
[DefaultValue(6)]
public int PasswordMinLen
{
get
{
return this.passwordMinLen;
}
set
{
try
{
this.passwordMinLen = value;
}
catch
{
}
}
}
public string Text
{
get
{
return this.GetHashPassword();
}
set
{
if (!string.IsNullOrEmpty(value))
{
this.Clear();
this.encryptedText = value;
base.EmptyTextTip = string.Empty;
base.Invalidate();
}
else if (value == string.Empty)
{
this.encryptedText = value;
base.Text = value;
this.encryptedHash.Clear();
base.Invalidate();
}
}
}
public delegate string DlgtEncryptString(byte[] plainText);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -