How to get Touchscreen to use Mouse Events instead of Touch Events in C# app

六月ゝ 毕业季﹏ 提交于 2019-11-27 08:22:56

问题


I have developed a C# app that overrides the PreviewMouseUp, PreviewMouseMove, and PreviewMouseDown events in some custom controls to allow scrolling a scroll viewer by clicking and dragging. This custom code works great on non-touch screens when the mouse is used to drag and scroll, but when I use it on a real touch screen monitor it fails to work because the PreviewMouseUp does not get fired when the monitor is touched and dragged with a finger.

I want to be able to use my existing code with minimal changes. This would allow me to use the mouse to click/drag while debugging on my laptop, and use the touch screen to touch/drag.

Is there anyway to configure a windows 7 computer to treat all touches (PreviewTouchUp, PreviewTouchDown, and PreviewTouchMove) as mouse events instead?

UPDATE: I forgot to mention. I thought that if the Touch events are not handled, than .NET automatically raises the Mouse events afterwards. This does not appear to be happening even though none of the code I wrote handles the Touch Events currently. Any ideas why this may be happening?

Thanks, brian


回答1:


What you can do is to use the same method for both, something like this:

protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
{
    OnPreviewTouchMouseUp(e);
}

protected override void OnPreviewTouchUp(TouchEventArgs e)
{
    OnPreviewTouchMouseUp(e);
}

private void OnPreviewTouchMouseUp(EventArgs e)
{

}

The only difference will be the way you get the coordinates of the mouse/touch:

For touch:

e.GetTouchPoint(this).Position;

For mouse:

e.GetPosition(this);


来源:https://stackoverflow.com/questions/11245551/how-to-get-touchscreen-to-use-mouse-events-instead-of-touch-events-in-c-sharp-ap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!