首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

WPF 密码框输入密码时挟制使用英文输入法

2012-08-28 
WPF 密码框输入密码时强制使用英文输入法最近项目遇到这个问题,网上搜了半天,也没有好的解决方案,哥怒了!

WPF 密码框输入密码时强制使用英文输入法

最近项目遇到这个问题,网上搜了半天,也没有好的解决方案,哥怒了!于是自己想了个笨办法:

在登录密码框登录时,只能按英文输入法输入,强制密码框使用英文输入法:

思路是:在密码框的键盘焦点事件和键盘输入事件,列出当前系统所有输入法,将系统输入法强制切换成英文输入法。可能也不是很完美的办法,希望大家多指教,先谢过了。

 

UI:

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <PasswordBox Height="23" HorizontalAlignment="Left" Margin="216,80,0,0" Name="passwordBox1" VerticalAlignment="Top" Width="120" InputMethod.IsInputMethodEnabled="False"/>                   
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="250,131,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

 

后台:

 public partial class MainWindow : Window
    {
        private const string DefaultInputLanguage = "en";


        public MainWindow()
        {
            InitializeComponent();

            passwordBox1.PreviewGotKeyboardFocus += new KeyboardFocusChangedEventHandler(passwordBox1_GotKeyboardFocus);
            passwordBox1.PreviewKeyDown += new KeyEventHandler(passwordBox1_PreviewKeyDown);
            passwordBox1.PasswordChanged += new RoutedEventHandler(passwordBox1_PasswordChanged);
            OutputInputLanguage();
        }

        void OutputInputLanguage()
        {
            using (StreamWriter sw = new StreamWriter("InputLangTest.txt"))//输出当期所有系统输入法
            {
                string strContent = string.Empty;
                foreach (var lang in InputLanguageManager.Current.AvailableInputLanguages)
                {
                    var langCultureInfo = lang as CultureInfo;
                    strContent += langCultureInfo.Name + "\r\n";
                }
                sw.WriteLine(strContent);
                sw.Flush();
            }
        }

        void passwordBox1_PasswordChanged(object sender, RoutedEventArgs e)
        {
            ChangeInputLanguage();
            if (!InputLanguageManager.Current.CurrentInputLanguage.Name.StartsWith(DefaultInputLanguage))
            {
                passwordBox1.Clear();
            }
            e.Handled = true;
        }

        void passwordBox1_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            ChangeInputLanguage();
        }

        void passwordBox1_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

            ChangeInputLanguage();
        }

        void ChangeInputLanguage()
        {

         //改变当前输入法为英文的。

        if (InputLanguageManager.Current.CurrentInputLanguage.Name.StartsWith(DefaultInputLanguage))
            {
                return;
            }

            foreach (var lang in InputLanguageManager.Current.AvailableInputLanguages)
            {
                var langCultureInfo = lang as CultureInfo;
                if (langCultureInfo.Name.StartsWith(DefaultInputLanguage))
                {
                    InputLanguageManager.Current.CurrentInputLanguage = langCultureInfo;
                    break;
                }
            }


        }


    }

 

 

热点排行