How to determine if button was clicked or touched? - wpf

Is there a way to distinguish whether a button was clicked as in with a mouse or touched using a touchscreen in WPF?

You can subscribe to PreviewMouseDown and PreviewTouchDown.
Page.xaml
<Button PreviewMouseDown="Button_PreviewMouseDown"
PreviewTouchDown="Button_PreviewTouchDown" />
Page.xaml.cs
private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("Mouse was used.");
}
private void Button_PreviewTouchDown(object sender, TouchEventArgs e)
{
MessageBox.Show("Touchscreen was used.");
}
I don't believe you'll be able to access the eventargs of either in the actual click event.
If you need to perform work there as opposed to the preview events I would recommend setting an instance variable in the preview events so when you get to the click event you know where you came from.

You have to set up an event handler. In the designer, double click on the button and that will set on up for you.
Then in the code behind add what ever code you want.
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Title = "Clicked";
}
You can add Touch events as well TouchDown, TouchUp, etc.

Windows 7 and its higher versions have the ability to receive input from multiple touch-sensitive devices. WPF applications can also handle touch input as other input, such as the mouse or keyboard, by raising events when a touch occurs.
WPF exposes two types of events when a touch occurs − touch events and manipulation events. Touch events provide raw data about each finger on a touchscreen and its movement. Manipulation events interpret the input as certain actions. Both types of events are discussed in this section.
WPF enables applications to respond to touch. For example, you can interact with an application by using one or more fingers on a touch-sensitive device, such as a touchscreen This walkthrough creates an application that enables the user to move, resize, or rotate a single object by using touch.
Source MSDN : https://msdn.microsoft.com/en-us/library/ee649090.aspx
Also read this codeproject article - http://www.codeproject.com/Articles/692286/WPF-and-multi-touch

Related

How to prevent Windows forms' new MDI from clearing my graphics from my previous MDI? [duplicate]

I've written a Windows Forms app where I do custom drawing on a Panel using Control.CreateGraphics(). Here's what my Form looks like at startup:
The custom drawing is performed on the top panel in the Click event handler of the "Draw!" button. Here's my button click handler:
private void drawButton_Click(object sender, EventArgs e)
{
using (Graphics g = drawPanel.CreateGraphics())
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.Clear(Color.White);
Size size = drawPanel.ClientSize;
Rectangle bounds = drawPanel.ClientRectangle;
bounds.Inflate(-10, -10);
g.FillEllipse(Brushes.LightGreen, bounds);
g.DrawEllipse(Pens.Black, bounds);
}
}
After a click on drawButton, the form looks like this:
Success!
But when I shrink the form by dragging a corner...
...and expand it back to its original size,
part of what I drew is gone!
This also happens when I drag part of the window offscreen...
...and drag it back onscreen:
If I minimize the window and restore it, the whole image is erased:
What is causing this? How can I make it so the graphics I draw are persistent?
Note: I've created this self-answered question so I have a canonical Q/A to direct users to, as this is a common scenario that's hard to search for if you don't already know the cause of the problem.
TL;DR:
Don't do your drawing in response to a one-time UI event with Control.CreateGraphics. Instead, register a Paint event handler for the control on which you want to paint, and do your drawing with the Graphics object passed via the PaintEventArgs.
If you want to paint only after a button click (for example), in your Click handler, set a boolean flag indicating that the button has been clicked and then call Control.Invalidate(). Then do your rendering conditionally in the Paint handler.
Finally, if your control's contents should change with the size of the control, register a Resize event handler and call Invalidate() there too.
Example code:
private bool _doCustomDrawing = false;
private void drawPanel_Paint(object sender, PaintEventArgs e)
{
if (_doCustomDrawing)
{
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.Clear(Color.White);
Size size = drawPanel.ClientSize;
Rectangle bounds = drawPanel.ClientRectangle;
bounds.Inflate(-10, -10);
g.FillEllipse(Brushes.LightGreen, bounds);
g.DrawEllipse(Pens.Black, bounds);
}
}
private void drawButton_Click(object sender, EventArgs e)
{
_doCustomDrawing = true;
drawPanel.Invalidate();
}
private void drawPanel_Resize(object sender, EventArgs e)
{
drawPanel.Invalidate();
}
But why? What was I doing wrong, and how does this fix it?
Take a look at the documentation for Control.CreateGraphics:
The Graphics object that you retrieve through the CreateGraphics method should not normally be retained after the current Windows message has been processed, because anything painted with that object will be erased with the next WM_PAINT message.
Windows doesn't take responsibility for retaining the graphics you draw to your Control. Rather, it identifies situations in which your control will require a repaint and informs it with a WM_PAINT message. Then it's up to your control to repaint itself. This happens in the OnPaint method, which you can override if you subclass Control or one of its subclasses. If you're not subclassing, you can still do custom drawing by handling the public Paint event, which a control will fire near the end of its OnPaint method. This is where you want to hook in, to make sure your graphics get redrawn every time the Control is told to repaint. Otherwise, part or all of your control will be painted over to the control's default appearance.
Repainting happens when all or part of a control is invalidated. You can invalidate the entire control, requesting a full repaint, by calling Control.Invalidate(). Other situations may require only a partial repaint. If Windows determines that only part of a Control needs to be repainted, the PaintEventArgs you receive will have a non-empty ClipRegion. In this situation, your drawing will only affect the area in the ClipRegion, even if you try to draw to areas outside that region. This is why the call to drawPanel.Invalidate() was required in the above example. Because the appearance of drawPanel needs to change with the size of the control and only the new parts of the control are invalidated when the window is expanded, it's necessary to request a full repaint with each resize.

Windows 7 and Windows 8 Desktop WPF Touch and Mouse LeftButtonDown Event handling differences

I created a Border on a WPF User Control and signed up for the Touch Down and the Mouse Left Button Down Event.
<Border
TouchDown="Border_TouchDown_1"
MouseLeftButtonDown="Border_MouseLeftButtonDown_1">
<Label FontSize="28">Label</Label>
</Border>
In the code behind file I have following event handlers:
private void Border_TouchDown_1(object sender, TouchEventArgs e)
{
e.Handled = true;
Result.Append("Border_TouchDown_1" + "\n");
LogText = Result.ToString();
RaisePropertyChange();
}
private void Border_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
{
Result.Append("Border_MouseLeftButtonDown_1" + "\n");
LogText = Result.ToString();
RaisePropertyChange();
}
On a Touch Gesture in windows 8 I see both event handlers getting triggered even if the event is handled in the Border_TouchDown_1 method.
In Windows 7 I see only the Border_TouchDown_1 method gets called which seemed to be correct because the event is handled.
I guess Microsoft concisely decided to fire both events to make all Applications Touch aware which is ok for Mouse only application but my application is Touch aware and I would like to switch this behavior off.
Anybody with some insides out there???
Do you know what will blow your mind? If you DO NOT set e.Handled = true, you won't get the mouse event (at least for PreviewTouchDown / PreviewMouseDown). It's almost like someone made a typo for windows 8 and it is working backwards.

Simple One Finger Touch Event

Does anyone know how I can implement a single Touch Event. A simple, one finger touch event, not multiple gesture or anything... Just a simple one finger touch.
I want to learn how to do this instead of doing LeftButtonMouseUp.
Add this to any control:
XAML
Tap="Control_Tap"
CodeBehind
private void Control_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
// Tap Logic
}
Here is a link to the Gesture cheat sheet

Hotkeys in Silverlight?

Is there a way to create global keys in Silverlight? I added a KeyDown event handler to the topmost Page element. I can catch the key signal when elements like a Button, TextBox, Calendar, and RichTextBox have focus. But the DatePicker won't let me handle the key down event at the Page level. Any way to make a hotkey in non-OOB SL?
P.S. I am assuming that the DatePicker control behaves differently than others as it is still in the Silverlight Toolkit.
have you tried using the AddHandler method? With this method you can add a handler and define if you want to handle already handled events, too.
In my simple example I added it to the RootVisual in the app.xaml.cs.
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
RootVisual.AddHandler(UIElement.KeyDownEvent, new KeyEventHandler(HandleKeyDown), true);
}
private void HandleKeyDown(object sender, KeyEventArgs e)
{
//Add your keyhandling code here
Debug.WriteLine(e.Key);
}
I tried it with a DatePicker and it works.
Hope this helps!
BR,
TJ
This may help you. The author provides two ways using HTML bridge (when your Silverlight does not have focus) and using the KeyUp event on Application.RootVisual.
I have not tried it myself so I wouldn't know if it's working.

WPF 4 multi-touch drag and drop

I have a WPF 4 application where I have implemented Drag and Drop using the standard DragDrop.DoDragDrop approach, but Im doing it using touch instead of Mouse events.
My XAML for my Grid (that Im dragging) is as follows:
<Grid x:Name="LayoutRoot"
ManipulationStarting="ManipulationStarting"
ManipulationDelta="ManipulationDelta"
ManipulationCompleted="ManipulationCompleted"
IsManipulationEnabled="True">
<!-- children in here -->
</Grid>
Now the code behind is like this:
private void ManipulationStarting(object sender, ManipulationStartingEventArgs e)
{
e.ManipulationContainer = this;
e.Handled = true;
}
private void ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
e.Handled = true;
DragDrop.DoDragDrop(this, new DataObject(GetType(), this), DragDropEffects.Move);
}
private void ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
//completed stuff
}
BUT when I try to drag with one finger while already dragging with another finger (different hands for example, which would simulate two people) the second touches don't seem to register properly, infact, it seems that windows thinks that my two fingers are trying to scale (like a pinch gesture)...
Does anyone know a way to get around this?
Thanks a lot
Mark
For multi-touch, you're going to want to use the Surface Toolkit for Windows Touch. It includes a drag-and-drop framework suitable for multi-touch scenarios. It includes a drag-and-drop framework. That link includes several how-to scenarios.
Although this is just an educated guess, I would say that DragDrop.DoDragDrop() is not capable to handle multiple drag guestures in parallel.
Indices:
There's no possibility to pass a touch ID to the method (which would be neccessary for differentiating the ongoing drag gestures)
The implementation of DoDragDrop() is static
The call of DoDragDrop() is blocking until the drop occured or was canceled
It is based upon the OLE version of drag and drop (which was not updated for Win7)
However, I would be glad if someone could correct me in this matter, because I'm currently also searching an DND API suitable for touch support.
Regards,
Seven

Resources