我已经创建了一个Windows窗体应用程序。我希望当应用程序安装在装有Windows 8和touchscren的电脑上时,如果我点击一个文本框,虚拟键盘会自动显示。
我见过像
这样的东西private void textbox_Enter(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("TabTip.exe");
}
但是当我使用鼠标时它也有效。我希望虚拟键盘可以在我使用触摸屏时使用。
由于
答案 0 :(得分:0)
我找到了一个“粗鲁”的解决方案。 我已经看到,当我触摸时,会出现“587”WM消息,然后是2“33”WM消息(当您使用鼠标点击时也是如此)。
所以我看到了:在文本框之前的“mouseclick”或“focus enter”事件之前:
a)如果您使用过鼠标,则会有1“33”Wm消息
b)如果您使用过触摸,则会有1“587”消息,然后是2“33”Wm消息。
所以,关于代码。
在MyForm.cs中,我有一个bool的公共列表,它保存了“触摸状态”。如果我从未触摸(或者我的电脑不支持触摸屏),状态将永远不会填充
namespace MyForm
{
public partial class MyForm: Form
{
public List<bool> v_touch = new List<bool>();
protected override void WndProc(ref Message msg)
{
switch (msg.Msg)
{
//touch
case 587:
v_touch.Add(true);
break;
//mouse or touch (on mouse click message 33 occurs 1 time; on touch tap message 587 occurs 1 time and message 33 occurs 2 times)
case 33:
//add just if some touch event (587) has occured (never fill v_touch for not touch devices)
if (v_touch.Count > 0)
{
v_touch.Add(false);
}
break;
}
base.WndProc(ref msg);
}
}
}
在所有myUserControls中,我需要为每个文本框添加moouseclick,焦点输入和焦点离开事件。 鼠标点击或焦点输入调用一个功能,使虚拟键盘检查“触摸状态”。相反,焦点离开事件会杀死键盘。
namespace MyForm
{
public partial class MyUserControl : UserControl
{
public void enableVirtualKeyboard(List<bool> v_touch)
{
//check if came from "touch". When I use touch, the last v_touch values are (true, false, false)
if (v_touch.Count >= 3 && v_touch[v_touch.Count - 3] == true)
{
string progFiles = @"C:\Program Files\Common Files\Microsoft Shared\ink";
string onScreenKeyboardPath = System.IO.Path.Combine(progFiles, "TabTip.exe");
Process.Start(onScreenKeyboardPath);
}
}
public void disableVirtualKeyboard()
{
Process[] oskProcessArray = Process.GetProcessesByName("TabTip");
foreach (Process onscreenProcess in oskProcessArray)
{
onscreenProcess.Kill();
}
}
private void textbox_MouseClick(object sender, MouseEventArgs e)
{
this.enableVirtualKeyboard(((MyForm)this.ParentForm).v_touch);
}
private void textbox_Enter(object sender, EventArgs e)
{
this.enableVirtualKeyboard(((MyForm)this.ParentForm).v_touch);
}
private textbox_Leave(object sender, EventArgs e)
{
this.disableVirtualKeyboard();
}
}
}
}
我们可以在公共类中编写disableVirtualKeyboard和enableVirtualKeyboard,因此我们可以在应用程序的每个控件的所有texbbx中使用2个方法。另外,在MyForm的Form_Closing事件
上使用disableVirtualKeyboard很好