首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向WPF显示和隐藏Windows 8屏幕键盘

向WPF显示和隐藏Windows 8屏幕键盘
EN

Stack Overflow用户
提问于 2013-06-13 22:56:52
回答 8查看 21.3K关注 0票数 13

我正在为Windows 8平板电脑编写WPF应用程序。这是完整的windows8,而不是ARM/RT。

当用户输入文本框时,我使用以下代码显示屏幕上的键盘:

代码语言:javascript
复制
System.Diagnostics.Process.Start(@"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe");

这很好用,但是我不知道如何再次隐藏键盘?

有人知道怎么做吗?

另外,有没有什么方法可以调整我的应用程序的大小,以便在键盘出现时将焦点控件上移?有点像windows RT应用程序。

非常感谢

EN

回答 8

Stack Overflow用户

发布于 2014-03-14 12:46:29

我可以用下面的C#代码成功地关闭屏幕键盘。

代码语言:javascript
复制
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName,string lpWindowName);

[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);

public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;

private void closeOnscreenKeyboard()
{
    // retrieve the handler of the window  
    int iHandle = FindWindow("IPTIP_Main_Window", "");
    if (iHandle > 0)
    {
        // close the window using API        
        SendMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
    }  
}

private void Some_Event_Happened(object sender, EventArgs e)
{
    // It's time to close the onscreen keyboard.
    closeOnscreenKeyboard();
}

我希望这能对你有所帮助。

票数 19
EN

Stack Overflow用户

发布于 2015-08-08 04:34:13

有点晚了,我将改进tasaki的例子,当用户点击Windows8平板的WPF应用程序中的textBox时,我启用了在gotFocus/LostFocus事件上显示/隐藏事件。我希望这对有类似头痛问题的人有所帮助,因为禁用InkHelper,如果你想用触摸事件滚动,效果不是很好…

首先,你必须将这些引用添加到你的App.Xaml.cs文件中。

代码语言:javascript
复制
using System.Management;
using System.Runtime.InteropServices;

代码:

代码语言:javascript
复制
    protected override void OnStartup(StartupEventArgs eventArgs)
    {
        EventManager.RegisterClassHandler(typeof(TextBox), UIElement.GotFocusEvent,
                                new RoutedEventHandler(GotFocus_Event), true);
        EventManager.RegisterClassHandler(typeof(TextBox), UIElement.LostFocusEvent,
                                new RoutedEventHandler(LostFocus_Event), true);

       MainApplication.Show();
    }

    private static void GotFocus_Event(object sender, RoutedEventArgs e)
    {
       // TestKeyboard();
        OpenWindows8TouchKeyboard(sender, e);
    }
    //http://www.c-sharpcorner.com/UploadFile/29d7e0/get-the-key-board-details-of-your-system-in-windows-form/
    private static bool IsSurfaceKeyboardAttached()
    {
        SelectQuery Sq = new SelectQuery("Win32_Keyboard");
        ManagementObjectSearcher objOSDetails = new ManagementObjectSearcher(Sq);
        ManagementObjectCollection osDetailsCollection = objOSDetails.Get();
        //Windows 8 tablet are returnign 2 device when keyboard is connecto
        //My application won't be used for Desktop so this condition is fine
        //But u might want to see if keyboard is usb and == 1 so you are 
        //returning true or not on tablet.  
        return osDetailsCollection.Count <= 1 ? true : false;
    }

    private static void OpenWindows8TouchKeyboard(object sender, RoutedEventArgs e)
    {
        var textBox = e.OriginalSource as TextBox;        
        if (textBox != null && IsSurfaceKeyboardAttached())
        {
            var path = @"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe";
            if (!File.Exists(path))
            {
                // older windows versions
                path = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\osk.exe";
            }
            Process.Start(path);
            textBox.BringIntoView();//SetFocus so u dont lose focused area
        }
    }
    [DllImport("user32.dll")]
    public static extern int FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll")]
    public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);

    public const int WM_SYSCOMMAND = 0x0112;
    public const int SC_CLOSE = 0xF060;
    public const int SC_MINIMIZE = 0xF020;

    private void CloseOnscreenKeyboard()
    {
        // retrieve the handler of the window  
        int iHandle = FindWindow("IPTIP_Main_Window", "");
        if (iHandle > 0)
        {
            // close the window using API        
            SendMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
        }
    }

    private void LostFocus_Event(object sender, EventArgs e)
    {
        // It's time to close the onscreen keyboard.
        CloseOnscreenKeyboard();
    }
票数 6
EN

Stack Overflow用户

发布于 2016-08-24 15:36:05

我开源了我的项目,使WPF应用程序中关于TabTip集成的所有事情都自动化。

你可以在nuget上获得它,然后你只需要在你的应用程序启动逻辑中进行简单的配置:

代码语言:javascript
复制
TabTipAutomation.BindTo<TextBox>();

您可以将TabTip自动化逻辑绑定到任何UIElement。当任何这样的元素获得焦点时,虚拟键盘将打开,当元素失去焦点时,虚拟键盘将关闭。不仅如此,TabTipAutomation还会将UIElement (或窗口)移动到视图中,这样TabTip就不会阻塞聚焦的元素。

有关详细信息,请参阅project site

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17090325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档