我找到了一个“粗鲁”的解决方案。
我看到当我触摸时,会出现“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 中,我需要为每个文本框添加鼠标单击、焦点输入和焦点离开事件。
MouseClick 或 focus enter 调用启用虚拟键盘检查“触摸状态”的功能。相反,焦点离开事件会杀死键盘。
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