gpt4 book ai didi

c# - 隐藏密码文本

转载 作者:太空狗 更新时间:2023-10-29 21:46:12 25 4
gpt4 key购买 nike

我有一个使用 UseSystemPasswordChar 的文本框,因此它不会显示用户输入的密码。问题是密码仍然可以被 Spy++ 之类的东西读取。我正在寻找一种方法来隐藏它,就像他们在 Services.msc > 登录选项卡的密码字段中所做的那样。

最佳答案

这是我到目前为止所得到的。

您可以通过一些独特的事件来改进这一点,以指示是否已接受按下的键,是否已更改 InputFilter 或 RealText 等...

另一件需要改进的重要事情是 InputFilter 的默认用法,因为使用 char 和 Keys 对许多特殊键来说并不真正有效。例如 - 目前,如果您在 PasswordBox 处于焦点时按 Alt+F4,它将输入“s”...所以有一大堆错误需要修复。

最后,可能有一种比我在那里所做的更优雅的方式来处理大写字母和非大写字母的输入。

这里是:

public class PasswordBox : TextBox
{
private string _realText;

public string RealText
{
get { return this._realText; }
set
{
var i = this.SelectionStart;

this._realText = value ?? "";

this.Text = "";
this.Text = new string('*', this._realText.Length);

this.SelectionStart = i > this.Text.Length ? this.Text.Length : i;
}
}

private Func<KeyEventArgs, bool> _inputFilter;

public Func<KeyEventArgs, bool> InputFilter
{
get { return this._inputFilter; }
set { this._inputFilter = value ?? (e => true); }
}

public PasswordBox()
{
this.RealText = "";
this.InputFilter = e => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".Any(c => c == e.KeyValue);
}

protected override void OnKeyDown(KeyEventArgs e)
{
e.SuppressKeyPress = true;

switch (e.KeyCode)
{
case Keys.Back:
if (this.SelectionStart > 0 || this.SelectionLength > 0)
{
this.RealText = this.SelectionLength == 0
? this.RealText.Remove(--this.SelectionStart, 1)
: this.RealText.Remove(this.SelectionStart, this.SelectionLength);
}
break;
case Keys.Delete:
if (this.SelectionStart == this.TextLength)
{
return;
}

this.RealText = this.RealText.Remove(this.SelectionStart, this.SelectionLength == 0 ? 1 : this.SelectionLength);
break;
case Keys.X:
case Keys.C:
case Keys.V:
if (e.Control)
{
return;
}
goto default;
case Keys.Right:
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Shift:
case Keys.Home:
case Keys.End:
e.SuppressKeyPress = false;
base.OnKeyDown(e);
break;
default:
if (e.Control)
{
e.SuppressKeyPress = false;
base.OnKeyDown(e);
break;
}

if (this.InputFilter(e))
{
var c = (char)e.KeyValue;

if (e.Shift == IsKeyLocked(Keys.CapsLock))
{
c = char.ToLower(c);
}

this.RealText = this.RealText.Remove(this.SelectionStart, this.SelectionLength)
.Insert(this.SelectionStart, c.ToString());

this.SelectionStart++;
}
break;
}
}
}

关于c# - 隐藏密码文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10408954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com