利用WPF互操作性elementhost控件承载 System.Windows.Controls
控件,并绑定WPF控件的TouchEnterEvent 和 TouchUpEvent两个事件。
直接使用 System.Windows.Forms 系列控件操作触摸屏不好使。
第一步:在winform窗口上增加一个ElementHost控件,命名为ElementHost1,此控件用于wpf互操作。再增加一个form button 按钮并命名为 button1.
第二步:代码生成WPF控件并添加到elementhost控件Controls集合中。
System.Windows.Controls.Image m_Btn_zoomin = new System.Windows.Controls.Image()
{
Name = "Btn_zoomin"
};
brush = new BitmapImage();
brush.BeginInit();
brush.UriSource = new Uri(@"D:\img\zoomin.png", UriKind.Absolute);
brush.EndInit();
m_Btn_zoomin.Source = brush;
ElementHost1.Child = m_Btn_zoomin;
m_Btn_zoomin.AddHandler(System.Windows.Controls.Button.TouchEnterEvent, new System.Windows.RoutedEventHandler(touch_down), true);
m_Btn_zoomin.AddHandler(System.Windows.Controls.Button.TouchUpEvent, new System.Windows.RoutedEventHandler(touch_up), true);
第三步:增加响应事件
private void touch_down(object sender, RoutedEventArgs e)
{
//在此处写按下响应程序
button1.text="touch down";
}
private void touch_up(object sender, RoutedEventArgs e)
{
//在此处写抬起响应程序
button1.text="touch up"
}
以上方法经过项目测试非常完美,最开始想用传统winform的mousedown和mouseup 事件实现,但实际无法达到理想触摸效果,最终经过摸索得出以上方法,总结出来希望对碰到此坑的朋友有所帮助。