I'm porting an app to Univeral Windows Platform (Windows 10).
Android has a onLongPress event. Is there an equivalent for UWP?
I found there is a Holding event which I tried to use something like this:
private void Rectangle_Holding(object sender, HoldingRoutedEventArgs e)
{
if (e.HoldingState == HoldingState.Started)
{
Debug.WriteLine("Holding started!");
}
}
Problem is the event is not triggered on Windows Desktop, when mouse is used instead of touch.
Mouse input doesn't produce Holding events by default, you should use RightTapped event to show the context menu, it is triggered when user long presses in a touch device and right clicks in a mouse device.
Take a look at GestureRecognizer.Holding and Detect Simple Touch Gestures, you can achieve that using the following code
public sealed partial class MainPage : Page
{
GestureRecognizer gestureRecognizer = new GestureRecognizer();
public MainPage()
{
this.InitializeComponent();
gestureRecognizer.GestureSettings = Windows.UI.Input.GestureSettings.HoldWithMouse;
}
void gestureRecognizer_Holding(GestureRecognizer sender, HoldingEventArgs args)
{
MyTextBlock.Text = "Hello";
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
gestureRecognizer.Holding += gestureRecognizer_Holding;
}
private void Grid_PointerPressed(object sender, PointerRoutedEventArgs e)
{
var ps = e.GetIntermediatePoints(null);
if (ps != null && ps.Count > 0)
{
gestureRecognizer.ProcessDownEvent(ps[0]);
e.Handled = true;
}
}
private void Grid_PointerMoved(object sender, PointerRoutedEventArgs e)
{
gestureRecognizer.ProcessMoveEvents(e.GetIntermediatePoints(null));
e.Handled = true;
}
private void Grid_PointerReleased(object sender, PointerRoutedEventArgs e)
{
var ps = e.GetIntermediatePoints(null);
if (ps != null && ps.Count > 0)
{
gestureRecognizer.ProcessUpEvent(ps[0]);
e.Handled = true;
gestureRecognizer.CompleteGesture();
}
}
}
Related
I was trying to set up a system to check if the mouse is clicked in a winforms application and have ended up doing this
private void lblChips_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
private void lblChips_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
eight times over (for each label).
Does anyone know if there's a more efficient way of doing this? I tried using
private void frmGame4_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
private void frmGame4_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
but that doesnt work. Any help would be greatly appreciated.
this
public Form1()
{
InitializeComponent();
CollectFormControls(this);
ControlList.Add(this);
MergeEvents();
}
List<Control> ControlList = new List<Control>();
private void CollectFormControls(Control c)
{
foreach (Control control in c.Controls)
{
ControlList.Add(control);
CheckSubControls(control);
}
}
private void CheckSubControls(Control control)
{
if (control.Controls.Count > -1)
{
CollectFormControls(control);
}
}
private void MergeEvents()
{
for (int i = 0; i < ControlList.Count; i++)
{
ControlList[i].MouseDown += All_MouseDown;
ControlList[i].MouseUp += All_MouseUp;
}
}
public void All_MouseUp(object sender, MouseEventArgs e)
{
this.Text = "up";
}
public void All_MouseDown(object sender, MouseEventArgs e)
{
this.Text = "down";
}
You could make two methods, one for mouse up and one for mouse down, like this:
private void labels_mouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
private void labels_mouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
Then, link all the labels to these methods. You can also do this from Code:
label.mouseDown += labels_mouseDown;
label.mouseUp += labels_mouseUp;
This has to be done somewhere under the InitializeComponent(); method. You still have to do this for every label but in the end it's less code.
I'm trying to implement a customized exit prompt in my WinForms. (I should not be using DialogBox)
I have a User Control Object placed in my main form that is invisible and disabled by default. Clicking in a certain button I have placed on the form shows and enables the object, disabling everything in my form except the User Control.
private void btn_close_Click(object sender, EventArgs e) {
prompt1.Visible = true;
prompt1.Enabled = true;
disableControls();
//Wait for a button to be pressed in prompt1
//Make an action based on a button pressed.
//closeApp returns a boolean
if (!prompt1.closeApp)
{
prompt1.Visible = false;
prompt1.Enabled = false;
enableControls();
}
else
{
Application.Exit();
}
}
Here's my code at the prompt object:
public partial class Prompt : UserControl
{
bool exit;
public bool closeApp
{
get{return exit;}
}
public Prompt()
{
InitializeComponent();
}
private void btn_yes_Click(object sender, EventArgs e)
{
exit = true;
}
private void btn_no_Click(object sender, EventArgs e)
{
exit = false;
this.Hide();
}
}
What I want to do is wait for a button to be pressed in my prompt object before proceeding to the next line in the btn_close_Click().
What should I do? Is there a better way to implement this?
Add events to your usercontrol then handle those events on your main form.
In your usercontrol:
public event EventHandler<EventArgs> ExitCancelled;
public event EventHandler<EventArgs> ExitApplication;
private void btn_yes_Click(object sender, EventArgs e)
{
ExitApplication?.Invoke(this, EventArgs.Empty);
}
private void btn_no_Click(object sender, EventArgs e)
{
ExitCancelled?.Invoke(this, EventArgs.Empty);
}
Handle the events on your form:
public void prompt1_ExitApplication(object sender, EventArgs e)
{
Application.Exit();
}
public void prompt1_ExitCancelled(object sender, EventArgs e)
{
prompt1.Hide();
enablecontrols();
}
i already make a simple program to recording mouse position and playback. Now i wanna add event if the mouse left click and right mouse click. But i still not understand how to do it. I already try code from many site, but still not work. Any one wanna help me please ? I'm still learning about programing, i just wanna make simple program.
this is my code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AutoClicker
{
public partial class Form1 : Form
{
ListViewItem lv;
int a, b;
public Form1()
{
InitializeComponent();
this.Closing += new System.ComponentModel.CancelEventHandler(this.FormClosingEventCancle_Closing); //Menangkap event x di klik
}
private void FormClosingEventCancle_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
DialogResult dr = MessageBox.Show("Yakin ingin keluar?", "Konfirmasi", MessageBoxButtons.YesNo); if (dr == DialogResult.No)
e.Cancel = true;
else
e.Cancel = false;
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
btn_putar.Enabled = false;
btn_rekam.Enabled = false;
btn_berhenti.Enabled = true;
}
private void timer2_Tick(object sender, EventArgs e)
{
//set posisi baru mouse
if (a != b)
{
Cursor.Position = new Point(int.Parse(listView1.Items[a].SubItems[0].Text), int.Parse(listView1.Items[a].SubItems[1].Text));
a++;
}
//agar bisa rekam ulang dan data di set ulang
else
{
btn_rekam.Enabled = true;
btn_putar.Enabled = false;
btn_berhenti.Enabled = false;
listView1.Clear();
a = 0;
b = 0;
timer2.Stop();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
lv = new ListViewItem(Cursor.Position.X.ToString());
lv.SubItems.Add(Cursor.Position.Y.ToString());
listView1.Items.Add(lv);
b++;
}
private void btn_berhenti_Click(object sender, EventArgs e)
{
btn_rekam.Enabled = true;
btn_putar.Enabled = true;
timer1.Stop();
timer2.Stop();
}
private void btn_putar_Click(object sender, EventArgs e)
{
timer2.Start();
btn_putar.Enabled = false;
btn_rekam.Enabled = false;
btn_berhenti.Enabled = false;
}
private void Form1_Load(object sender, EventArgs e)
{
a = 0;
b = 0;
btn_berhenti.Enabled = false;
btn_putar.Enabled = false;
}
}
}
You can use some of Mouse events like MouseClick, MouseDown, MouseUp, etc.
For example:
protected override void OnMouseDown(MouseEventArgs e)
{
if(e.Button == System.Windows.Forms.MouseButtons.Left)
{
//Do some stuff
MessageBox.Show("Lefty!");
}
else if(e.Button == System.Windows.Forms.MouseButtons.Right)
{
//Do some stuff
MessageBox.Show("Righty!");
}
}
Assuming you copy/pase this code somewhere in Form1 class, it will override Form1's OnMouseDown event.
When you left/right click on form, you'll get related MessageBox.
If you want to use the event on any other Control, you need to override that control's related event.
*Edit after comment:
public void OnMouseDown(object sender, MouseEventArgs e)
{
if(e.Button == System.Windows.Forms.MouseButtons.Left)
{
//Do some stuff
MessageBox.Show("Lefty!");
}
else if(e.Button == System.Windows.Forms.MouseButtons.Right)
{
//Do some stuff
MessageBox.Show("Righty!");
}
}
And add OnMouseDown event to any Control on form load. For example:
private void Form1_Load(object sender, EventArgs e)
{
a = 0;
b = 0;
button1.MouseDown += OnMouseDown;
listView1.MouseDown += OnMouseDown;
}
That way, when you left/right click on button1 or listView1 you will get a MessageBox.
You can setup an event handler to fire whenever the mouse button is clicked (shown below;)
namespace MouseClickDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MouseClick += Form1_MouseClick;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
//Left mouse button hit
}
if(e.Button == MouseButtons.Right)
{
//Right mouse button hit
}
}
}
How to save radio button selection when navigating the back and forward?
For textbox, I know it's something like this:
private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
if (e.PageState != null)
{
this.text5_input.Text = e.PageState["txtContents"] as string;
}
}
private void navigationHelper_SaveState(object sender, SaveStateEventArgs e)
{
e.PageState["txtContents"] = this.text5_input.Text;
}
However when we use the similar code for radio buttons, an error pops up saying cannot convert string(e.PageState) to bool(radio button).
How do I make this happen?
You can use RadioButtonInstance.IsChecked.ToString and to recover the data use bool.Parse((bool)e.PageState["rbState"])
private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
if (e.PageState != null)
{
this.text5_input.Text = e.PageState["txtContents"] as string;
this.RadioButtonInstance.IsChecked = (bool)e.PageState["rbState"];
}
}
private void navigationHelper_SaveState(object sender, SaveStateEventArgs e)
{
e.PageState["txtContents"] = this.text5_input.Text;
e.PageState["rbState"] = this.RadioButtonInstance.IsChecked;
}
In theory this is for bool, can you try?
I´m trying to work with Kinect SDK 1.7 in WPF and C#.
Is there any option how to control WPF elements - for example WPF button, WPF slider, etc.
I cannot find how to do this. When I take a WPF button to Kinect region the KinectPointer doesn´t react with these controls. I thought that this pointer is something like mouse pointer.
Please can you help me, how to do that?
Thank you very much!
This link might help you to Kinectify your own controllers. The example is about a checkbox, but you can extend it to other controllers as well:
public class MyCheckBox : CheckBox
{
private static readonly bool IsInDesignMode = DesignerProperties.GetIsInDesignMode(new DependencyObject());
private HandPointer _capturedHandPointer;
public MyCheckBox()
{
if (!IsInDesignMode)
{
Initialise();
}
}
private void Initialise()
{
KinectRegion.AddHandPointerPressHandler(this, this.OnHandPointerPress);
KinectRegion.AddHandPointerGotCaptureHandler(this, this.OnHandPointerCaptured);
KinectRegion.AddHandPointerPressReleaseHandler(this, this.OnHandPointerPressRelease);
KinectRegion.AddHandPointerLostCaptureHandler(this, this.OnHandPointerLostCapture);
KinectRegion.AddHandPointerEnterHandler(this, this.OnHandPointerEnter);
KinectRegion.AddHandPointerLeaveHandler(this, this.OnHandPointerLeave);
KinectRegion.SetIsPressTarget(this, true);
}
}
private void OnHandPointerLeave(object sender, HandPointerEventArgs e)
{
if (!KinectRegion.GetIsPrimaryHandPointerOver(this))
{
VisualStateManager.GoToState(this, "Normal", true);
}
}
private void OnHandPointerEnter(object sender, HandPointerEventArgs e)
{
if (KinectRegion.GetIsPrimaryHandPointerOver(this))
{
VisualStateManager.GoToState(this, "MouseOver", true);
}
}
private void OnHandPointerLostCapture(object sender, HandPointerEventArgs e)
{
if (_capturedHandPointer == e.HandPointer)
{
_capturedHandPointer = null;
IsPressed = false;
e.Handled = true;
}
}
private void OnHandPointerCaptured(object sender, HandPointerEventArgs e)
{
if (_capturedHandPointer == null)
{
_capturedHandPointer = e.HandPointer;
IsPressed = true;
e.Handled = true;
}
}
private void OnHandPointerPress(object sender, HandPointerEventArgs e)
{
if (_capturedHandPointer == null && e.HandPointer.IsPrimaryUser && e.HandPointer.IsPrimaryHandOfUser)
{
e.HandPointer.Capture(this);
e.Handled = true;
}
}
private void OnHandPointerPressRelease(object sender, HandPointerEventArgs e)
{
if (_capturedHandPointer == e.HandPointer)
{
if (e.HandPointer.GetIsOver(this))
{
OnClick();
VisualStateManager.GoToState(this, "MouseOver", true);
}
else
{
VisualStateManager.GoToState(this, "Normal", true);
}
e.Handled = true;
}
}
KinectPointer is quite different to a normal Mousepointer, it doesn't work with normal WPF Controls out of the box.
please have a look at the Events that are available at the kinect region:
http://msdn.microsoft.com/en-us/library/microsoft.kinect.toolkit.controls.kinectregion_events.aspx
If i'm remembering correctly, the sourcecode for the Kinect Interaction Tookit Controls is included in the SDK, have a look at KinectButtonBase.cs to see how you could develop Kinect-Controls.