gpt4 book ai didi

c# - 软键盘重叠控件(WPF、桌面)

转载 作者:行者123 更新时间:2023-11-30 12:59:37 24 4
gpt4 key购买 nike

首先我想强调一下,我正在寻找一个Windows DESKTOP WPF 解决方案,所以请不要使用android。谢谢。

其次,我对 WPF 和为 Windows 平板电脑设计软件还很陌生,所以请多多包涵...请务必明确回答。

第三,目标软件将在 Windows 平板电脑 (Win 8.0/8.1) 上运行,并且正在使用 Visual Studio 2013 Pro(C#、桌面应用程序、WPF)进行开发。

好的,这是我的问题:

我一直在使用文本框继承类来在用户触摸文本框时显示软键盘(类代码如下)。这部分实际上运作良好。我的问题是软键盘可能会在我实际控制时弹出。在 WinRT 中,这不会发生,因为操作系统似乎会向上滚动所述控件,直到它变得可见,但对于桌面应用程序,不存在此类功能。

所以我的问题是:有人知道如何解决这个重叠问题吗?

我如何(在 Win 桌面应用程序中)复制正常的 WinRT 行为?

我应该使用其他一些 API 来调用软键盘吗?

“TouchTextbox”类的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace WPF_DT_Soft_Keyboard
{
class TouchTextbox : TextBox
{

[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;

protected override void OnGotFocus(System.Windows.RoutedEventArgs e)
{
base.OnGotFocus(e);
ShowSoftKeyboard();
}

protected override void OnLostFocus(System.Windows.RoutedEventArgs e)
{
base.OnLostFocus(e);
HideSoftKeyboard();
}

protected override void OnPreviewKeyUp(System.Windows.Input.KeyEventArgs e)
{
base.OnPreviewKeyUp(e);
if (e.Key == System.Windows.Input.Key.Enter) HideSoftKeyboard();
}

private void ShowSoftKeyboard()
{
System.Diagnostics.Process.Start(@"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe");
}

private void HideSoftKeyboard()
{
int iHandle = FindWindow("IPTIP_Main_Window", "");
if (iHandle > 0)
{
SendMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
}
}
}
}

最佳答案

好吧,我想我终于找到了一个像样的解决方案。我已经将它封装到下面的 TouchTextbox 类中。为了使其正常工作,TouchTextbox 必须包含在 [ScrollViewer] 控件中。

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Threading;

namespace WPF_DT_Soft_Keyboard
{
class TouchTextbox : TextBox
{

/// <summary>
/// Private Container
/// </summary>
DispatcherTimer mBringIntoViewTimer = new DispatcherTimer();

/// <summary>
/// DLL imports
/// </summary>
[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);
[DllImport("user32.dll")]
public static extern int MoveWindow(int hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
public static extern int SetWindowPos(int hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("Kernel32.dll")]
public static extern uint GetLastError();

/// <summary>
/// Constante
/// </summary>
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
public const int HWND_TOP = 0;
public const int SWP_NOSIZE = 0x0001;
public const int SWP_NOZORDER = 0x0004;
public const int SWP_SHOWWINDOW = 0x0040;
public const int BRING_INTO_VIEW_DELAY = 500;


/// <summary>
/// TouchTextbox
/// </summary>
public TouchTextbox()
{
mBringIntoViewTimer.IsEnabled = false;
mBringIntoViewTimer.Tick += MyTimer_Tick;
mBringIntoViewTimer.Interval = new TimeSpan(0, 0, 0, 0, BRING_INTO_VIEW_DELAY);
}

/// <summary>
/// MyTimer_Tick
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MyTimer_Tick(object sender, EventArgs e)
{
mBringIntoViewTimer.IsEnabled = false;
this.BringIntoView();
}

/// <summary>
/// OnGotFocus
/// </summary>
/// <param name="e"></param>
protected override void OnGotFocus(System.Windows.RoutedEventArgs e)
{
base.OnGotFocus(e);
ShowSoftKeyboard();
mBringIntoViewTimer.IsEnabled = true;
}

///// <summary>
///// OnLostFocus
///// </summary>
///// <param name="e"></param>
//protected override void OnLostFocus(System.Windows.RoutedEventArgs e)
//{
// base.OnLostFocus(e);
// HideSoftKeyboard();
//}

/// <summary>
/// OnPreviewKeyUp
/// </summary>
/// <param name="e"></param>
protected override void OnPreviewKeyUp(System.Windows.Input.KeyEventArgs e)
{
base.OnPreviewKeyUp(e);
if (e.Key == System.Windows.Input.Key.Enter) HideSoftKeyboard();
}

/// <summary>
/// ShowSoftKeyboard
/// </summary>
private void ShowSoftKeyboard()
{
Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\TabletTip\1.7", true).SetValue("EdgeTargetDockedState", "1", Microsoft.Win32.RegistryValueKind.DWord);
System.Diagnostics.Process.Start(@"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe");
}

/// <summary>
/// HideSoftKeyboard
/// </summary>
private void HideSoftKeyboard()
{
int iHandle = FindWindow("IPTIP_Main_Window", "");
if (iHandle > 0)
{
SendMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
}
}
}
}

关于c# - 软键盘重叠控件(WPF、桌面),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24517936/

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