C#PasswordBox不接受来自OnScreenKeyborad的输入

时间:2019-05-04 15:32:10

标签: c# wpf

平台 = C#,Visual Studio 2013

我在WPF窗口上有一个PasswordBox,它从OnScreenKeyboard接收输入。以下是我的代码可以毫无问题地满足的要求:

1::当用户在PasswordBox内单击鼠标时,应显示OnScreenKeyboard。

2 :当用户在外部点击鼠标PasswordBox时,OnScreenKeyboard应该消失。

问题: 每当我单击“ 外部PasswordBox(即窗口),然后单击“ 内部PasswordBox时,我什至无法从OnScreenKeyboard键入任何内容尽管光标在PasswordBox内闪烁。

但是,当加载窗口并“首先”单击“ 内部PasswordBox(即窗口)时,它将成功从OnScreenKeyboard接受输入。

要求: 每当我在PasswordBox内单击时,无论天气如何,它都应从OnScreenKeyBoard输入信息,而我首先单击Window或在PasswordBox侧面。

XML代码:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" FocusManager.FocusedElement="{Binding ElementName=p1}"   Height="740" Width="1840" MouseLeftButtonDown="Window_MouseLeftButtonDown" Loaded="Window_Loaded" Margin="0,0,0,0" >
    <Grid x:Name="g1"  >
        <PasswordBox x:Name="p1" HorizontalAlignment="Left" Margin="900,600,0,0" VerticalAlignment="Top" Width="110" Height="32" RenderTransformOrigin="-0.433,0.645" PreviewMouseLeftButtonDown="p1_PreviewMouseLeftButtonDown" />

    </Grid>
</Window>

C#源代码:

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        [DllImport("user32.dll")]
        static extern bool ShowWindow2(IntPtr hWnd, int nCmdShow); 

        [DllImportAttribute("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImportAttribute("user32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        [DllImportAttribute("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        public MainWindow()
        {
            InitializeComponent();

        }   

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ShowOnScreenKeyBoard();
            Thread.Sleep(1000);
            HideOnScreenKeyBoard();

        }
         private void ShowOnScreenKeyBoard()
        {
            bool isVisible = false;
            PreviewMouseLeftButtonDown += (ss, ee) =>
            {
                if (!p1.IsMouseOver && isVisible)
                {
                    Process[] Processes = Process.GetProcessesByName("CCOnScreenKeyboard");
                }
                else if (!isVisible)
                {
                    System.Diagnostics.Process.Start("D:\\CCOnScreenKeyboard.exe");
                    isVisible = true;
                }
            };

            try
            {
                Process[] Processes = Process.GetProcessesByName("CCOnScreenKeyboard");

                foreach (Process p2 in Processes)
                {
                    ShowToFront(p2.MainWindowTitle);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
         private void HideOnScreenKeyBoard()
         {
             try
             {
                 Process p = Process.GetProcesses().Where(d => d.ProcessName.Contains("CCOnScreenKeyboard")).DefaultIfEmpty(null).FirstOrDefault();

                 IntPtr hWndOSK = p.MainWindowHandle;
                 ShowWindow(hWndOSK, 2);
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.ToString());
             }
         }

        private void p1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ShowOnScreenKeyBoard();
        }
        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            HideOnScreenKeyBoard();
        }
        public static void ShowToFront(string windowName)
        {
            try
            {
                IntPtr firstInstance = FindWindow(null, windowName);
                ShowWindow(firstInstance, 1);
                SetForegroundWindow(firstInstance);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }


    }
}

0 个答案:

没有答案