如何在WPF密码框中显示字符数秒?
如果用户在密码框中输入1985,则四个项目符号(●●●●) 将显示。我如何显示在几秒钟内输入的每个字母或数字,然后将其更改为项目符号?我想这不能在密码框中完成,但有其他方法吗?在密码框顶部放置一个文本框,然后使用一点数据绑定和动画。这段XAML将允许文本框o只要输入继续,文本框就会可见,但一旦输入停止,文本框就会消失,只留下显示密码字符的密码框如何在WPF密码框中显示字符数秒?,wpf,textbox,delay,passwordbox,Wpf,Textbox,Delay,Passwordbox,如果用户在密码框中输入1985,则四个项目符号(●●●●) 将显示。我如何显示在几秒钟内输入的每个字母或数字,然后将其更改为项目符号?我想这不能在密码框中完成,但有其他方法吗?在密码框顶部放置一个文本框,然后使用一点数据绑定和动画。这段XAML将允许文本框o只要输入继续,文本框就会可见,但一旦输入停止,文本框就会消失,只留下显示密码字符的密码框 <Window.Resources> <Storyboard x:Key="Storyboard1">
<Window.Resources>
<Storyboard x:Key="Storyboard1">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="textBox">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:1.2" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="TextBoxBase.TextChanged" SourceName="textBox">
<StopStoryboard BeginStoryboardName="Storyboard1_BeginStoryboard"/>
<BeginStoryboard x:Name="Storyboard1_BeginStoryboard" Storyboard="{StaticResource Storyboard1}"/>
</EventTrigger>
</Window.Triggers>
<PasswordBox x:Name="passwordBox"/>
<TextBox x:Name="textBox"
Text="{Binding ElementName=passwordBox, Path=Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Opacity="100"/>
tbxPwd_PreviewTestInput方法用于检索用户键入的字符。tbxPwd_PreviewKeyDown方法用于检索退格键或要检测的任何其他控制字符键
此代码中没有延迟,因此它始终以明文显示密码字符串的最后一个字符。添加一些代码以及计时器应该很容易,以便在延迟一段时间后将最后一个字符更改为pwd字符
上面的代码还没有经过彻底调试,因此如果用户将整个密码退格以重新开始,可能会出现问题
提示:Alt+0149显示“bullet”密码字符。这只需使用一个
文本框即可实现。请参阅下面的代码:
窗口的XAML代码:
<Label x:Name="Pwd" Height="30" Width="70" HorizontalAlignment="Left" FontSize="14"
Margin="10,10,0,0" VerticalAlignment="Top" Content="Password:"/>
<TextBox x:Name="textBox" Width="130" Height="30" Margin="30,10,0,0"
VerticalAlignment="Top" MaxLength="12" FontSize="14"
PreviewKeyDown="TextBox_PreviewKeyDown"
KeyDown="TextBox_KeyDown" />
<CheckBox x:Name="ckhShowPassword" Height="30"
Content="Show password characters"
Margin="69,0,59,42" VerticalAlignment="Bottom"
Checked="ckhShowPassword_Checked" Unchecked="ckhShowPassword_UnChecked"/>
<Label x:Name="lblActualPwd" Height="30" Width="200"
Margin="10,100,0,0" VerticalAlignment="Top" FontSize="14"
HorizontalAlignment="Center" HorizontalContentAlignment="Center"/>
C#代码隐藏:
#region "CLASS LEVEL VARIABLES"
System.Windows.Threading.DispatcherTimer dispatcherTimer =
new System.Windows.Threading.DispatcherTimer();
string actualPwd = "";
#endregion
#region "WINDOW EVENTS"
public Window2()
{
InitializeComponent();
}
private void Window2_Loaded(object sender, RoutedEventArgs e)
{
lblActualPwd.Visibility = Visibility.Hidden;
textBox.Focus();
}
#endregion
#region "TEXTBOX EVENTS"
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Back)
{
actualPwd += GetCharFromKey(e.Key); //Store actual characters for later retrieval
}
else if (e.Key == Key.Back)
{
if (actualPwd.Length > 0)
actualPwd = actualPwd.Remove(actualPwd.Length - 1);
else
actualPwd = "";
}
else
{
actualPwd += GetCharFromKey(e.Key);
}
string str = "";
for (int i = 0; i < textBox.Text.Length; i++)
str += char.ConvertFromUtf32(8226);
textBox.Text = str;
textBox.Select(textBox.Text.Length, 0);
}
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1);
dispatcherTimer.Start();
}
#endregion
#region "DISPATCHER EVENT"
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
string str = "";
for(int i = 0; i < textBox.Text.Length; i++)
str += char.ConvertFromUtf32(8226);
textBox.Text = str;
textBox.Select(textBox.Text.Length, 0);
}
#endregion
#region "CHECKBOX EVENTS"
private void ckhShowPassword_Checked(object sender, RoutedEventArgs e)
{
if (actualPwd.Length > 0)
{
lblActualPwd.Foreground = Brushes.Blue;
lblActualPwd.Content = actualPwd;
lblActualPwd.Visibility = Visibility.Visible;
}
else
{
lblActualPwd.Foreground = Brushes.Red;
lblActualPwd.Content = "Please input password.";
lblActualPwd.Visibility = Visibility.Visible;
}
}
private void ckhShowPassword_UnChecked(object sender, RoutedEventArgs e)
{
lblActualPwd.Content = string.Empty;
lblActualPwd.Visibility = Visibility.Hidden;
}
#endregion
#region "ENUM TYPE - MAP KEY TO CHARACTER"
public enum MapType : uint
{
MAPVK_VK_TO_VSC = 0x0,
MAPVK_VSC_TO_VK = 0x1,
MAPVK_VK_TO_CHAR = 0x2,
MAPVK_VSC_TO_VK_EX = 0x3,
}
#endregion
#region "INTEROP DLL IMPORT"
[DllImport("user32.dll")]
public static extern bool GetKeyboardState(byte[] lpKeyState);
[DllImport("user32.dll")]
public static extern uint MapVirtualKey(uint uCode, MapType uMapType);
[DllImport("user32.dll")]
#endregion
#region "VIRTUAL KEY UNICODE CONVERSION"
public static extern int ToUnicode(
uint wVirtKey,
uint wScanCode,
byte[] lpKeyState,
[Out, MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 4)]
StringBuilder pwszBuff,
int cchBuff,
uint wFlags);
#endregion
#region "FUNCTION - CHAR FROM KEY"
public static char GetCharFromKey(Key key)
{
char ch = ' ';
int virtualKey = KeyInterop.VirtualKeyFromKey(key);
byte[] keyboardState = new byte[256];
GetKeyboardState(keyboardState);
uint scanCode = MapVirtualKey((uint)virtualKey, MapType.MAPVK_VK_TO_VSC);
StringBuilder stringBuilder = new StringBuilder(2);
int result = ToUnicode((uint)virtualKey, scanCode, keyboardState, stringBuilder, stringBuilder.Capacity, 0);
switch (result)
{
case -1:
break;
case 0:
break;
case 1:
{
ch = stringBuilder[0];
break;
}
default:
{
ch = stringBuilder[0];
break;
}
}
return ch;
}
#endregion
#区域“类级别变量”
System.Windows.Threading.Dispatchermer Dispatchermer=
新的System.Windows.Threading.Dispatchermer();
字符串actualPwd=“”;
#端区
#区域“窗口事件”
公共窗口2()
{
初始化组件();
}
已加载私有无效窗口2_(对象发送方,路由目标)
{
lblActualPwd.Visibility=Visibility.Hidden;
textBox.Focus();
}
#端区
#区域“文本框事件”
私有void文本框_PreviewKeyDown(对象发送方,KeyEventArgs e)
{
if(e.Key!=Key.Back)
{
actualPwd+=GetCharFromKey(e.Key);//存储实际字符以备以后检索
}
else if(e.Key==Key.Back)
{
如果(实际长度>0)
actualPwd=actualPwd.Remove(actualPwd.Length-1);
其他的
实际值=”;
}
其他的
{
actualPwd+=GetCharFromKey(e.Key);
}
字符串str=“”;
for(int i=0;i0)
{
lblActualPwd.Foreground=画笔.Blue;
lblActualPwd.Content=实际值;
lblActualPwd.Visibility=可见性.Visibility;
}
其他的
{
lblActualPwd.Foreground=画笔.Red;
lblActualPwd.Content=“请输入密码。”;
lblActualPwd.Visibility=可见性.Visibility;
}
}
私有无效ckhShowPassword_未选中(对象发送方,路由目标)
{
lblActualPwd.Content=string.Empty;
lblActualPwd.Visibility=Visibility.Hidden;
}
#端区
#区域“枚举类型-将键映射到字符”
公共枚举映射类型:uint
{
MAPVK_VK_TO_VSC=0x0,
MAPVK_VSC_TO_VK=0x1,
MAPVK_VK_TO_CHAR=0x2,
MAPVK_VSC_TO_VK_EX=0x3,
}
#端区
#区域“互操作DLL导入”
[DllImport(“user32.dll”)]
公共静态外部布尔GetKeyboardState(字节[]lpKeyState);
[DllImport(“user32.dll”)]
公共静态外部uint MapVirtualKey(uint uCode,MapType uMapType);
[DllImport(“user32.dll”)]
#端区
#区域“虚拟密钥UNICODE转换”
图尼科德公共静态外部设备(
uint wVirtKey,
uint wScanCode,
字节[]lpKeyState,
[Out,marshallas(UnmanagedType.LPWStr,SizeParamIndex=4)]
StringBuilder pwszBuff,
int cchBuff,
uint-wFlags);
#端区
#区域“函数-键中的字符”
公共静态字符GetCharFromKey(Key-Key)
{
char ch=“”;
int virtualKey=KeyInterop.VirtualKeyFromKey(键);
字节[]键盘状态=新字节[256];
GetKeyboardState(keyboardState);
uint scanCode=MapVirtualKey((uint)virtualKey,MapType.MAPVK_VK_TO_VSC);
StringBuilder StringBuilder=新的StringBuilder(2);
int result=ToUnicode((uint)virtualKey,扫描码,键盘状态,stringBuilder,stringBuilder.Capacity,0);
开关(结果)
{
案例1:
打破
案例0:
打破
案例1:
{
ch=stringBuilder[0];
打破
}
违约:
{
ch=stringBuilder[0];
打破
}
}
返回ch;
}
#端区
MapType
enum
检索自我已经实现了类似的功能,这里只是一个简单的示例,passwordbox被tex替换
<Label x:Name="Pwd" Height="30" Width="70" HorizontalAlignment="Left" FontSize="14"
Margin="10,10,0,0" VerticalAlignment="Top" Content="Password:"/>
<TextBox x:Name="textBox" Width="130" Height="30" Margin="30,10,0,0"
VerticalAlignment="Top" MaxLength="12" FontSize="14"
PreviewKeyDown="TextBox_PreviewKeyDown"
KeyDown="TextBox_KeyDown" />
<CheckBox x:Name="ckhShowPassword" Height="30"
Content="Show password characters"
Margin="69,0,59,42" VerticalAlignment="Bottom"
Checked="ckhShowPassword_Checked" Unchecked="ckhShowPassword_UnChecked"/>
<Label x:Name="lblActualPwd" Height="30" Width="200"
Margin="10,100,0,0" VerticalAlignment="Top" FontSize="14"
HorizontalAlignment="Center" HorizontalContentAlignment="Center"/>
#region "CLASS LEVEL VARIABLES"
System.Windows.Threading.DispatcherTimer dispatcherTimer =
new System.Windows.Threading.DispatcherTimer();
string actualPwd = "";
#endregion
#region "WINDOW EVENTS"
public Window2()
{
InitializeComponent();
}
private void Window2_Loaded(object sender, RoutedEventArgs e)
{
lblActualPwd.Visibility = Visibility.Hidden;
textBox.Focus();
}
#endregion
#region "TEXTBOX EVENTS"
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Back)
{
actualPwd += GetCharFromKey(e.Key); //Store actual characters for later retrieval
}
else if (e.Key == Key.Back)
{
if (actualPwd.Length > 0)
actualPwd = actualPwd.Remove(actualPwd.Length - 1);
else
actualPwd = "";
}
else
{
actualPwd += GetCharFromKey(e.Key);
}
string str = "";
for (int i = 0; i < textBox.Text.Length; i++)
str += char.ConvertFromUtf32(8226);
textBox.Text = str;
textBox.Select(textBox.Text.Length, 0);
}
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1);
dispatcherTimer.Start();
}
#endregion
#region "DISPATCHER EVENT"
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
string str = "";
for(int i = 0; i < textBox.Text.Length; i++)
str += char.ConvertFromUtf32(8226);
textBox.Text = str;
textBox.Select(textBox.Text.Length, 0);
}
#endregion
#region "CHECKBOX EVENTS"
private void ckhShowPassword_Checked(object sender, RoutedEventArgs e)
{
if (actualPwd.Length > 0)
{
lblActualPwd.Foreground = Brushes.Blue;
lblActualPwd.Content = actualPwd;
lblActualPwd.Visibility = Visibility.Visible;
}
else
{
lblActualPwd.Foreground = Brushes.Red;
lblActualPwd.Content = "Please input password.";
lblActualPwd.Visibility = Visibility.Visible;
}
}
private void ckhShowPassword_UnChecked(object sender, RoutedEventArgs e)
{
lblActualPwd.Content = string.Empty;
lblActualPwd.Visibility = Visibility.Hidden;
}
#endregion
#region "ENUM TYPE - MAP KEY TO CHARACTER"
public enum MapType : uint
{
MAPVK_VK_TO_VSC = 0x0,
MAPVK_VSC_TO_VK = 0x1,
MAPVK_VK_TO_CHAR = 0x2,
MAPVK_VSC_TO_VK_EX = 0x3,
}
#endregion
#region "INTEROP DLL IMPORT"
[DllImport("user32.dll")]
public static extern bool GetKeyboardState(byte[] lpKeyState);
[DllImport("user32.dll")]
public static extern uint MapVirtualKey(uint uCode, MapType uMapType);
[DllImport("user32.dll")]
#endregion
#region "VIRTUAL KEY UNICODE CONVERSION"
public static extern int ToUnicode(
uint wVirtKey,
uint wScanCode,
byte[] lpKeyState,
[Out, MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 4)]
StringBuilder pwszBuff,
int cchBuff,
uint wFlags);
#endregion
#region "FUNCTION - CHAR FROM KEY"
public static char GetCharFromKey(Key key)
{
char ch = ' ';
int virtualKey = KeyInterop.VirtualKeyFromKey(key);
byte[] keyboardState = new byte[256];
GetKeyboardState(keyboardState);
uint scanCode = MapVirtualKey((uint)virtualKey, MapType.MAPVK_VK_TO_VSC);
StringBuilder stringBuilder = new StringBuilder(2);
int result = ToUnicode((uint)virtualKey, scanCode, keyboardState, stringBuilder, stringBuilder.Capacity, 0);
switch (result)
{
case -1:
break;
case 0:
break;
case 1:
{
ch = stringBuilder[0];
break;
}
default:
{
ch = stringBuilder[0];
break;
}
}
return ch;
}
#endregion
<PasswordBox Name="LicencePasswordBox" MouseEnter="LicencePasswordBox_MouseEnter"></PasswordBox>
<TextBox IsReadOnly="True" Name="LicencePasswordTextBox" MouseLeave="LicencePasswordBox_MouseLeave" Visibility="Hidden"></TextBox>
private void LicencePasswordBox_MouseEnter(object sender, MouseEventArgs e)
{
LicencePasswordBox.Visibility = Visibility.Hidden;
LicencePasswordTextBox.Visibility = Visibility.Visible;
}
private void LicencePasswordBox_MouseLeave(object sender, MouseEventArgs e)
{
LicencePasswordBox.Visibility = Visibility.Visible;
LicencePasswordTextBox.Visibility = Visibility.Hidden;
}