Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...