1. 程式人生 > >C#長按事件

C#長按事件

part dia true dsta public 交互 nav system component

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;

using System.Windows.Shapes;
using System.Threading;
namespace WpfApplication9
{
/// <summary>
/// MainWindow.xaml 的交互邏輯
/// </summary>
public partial class MainWindow : Window
{
bool isPressed;
bool entryTouch;
Thread th = null;
public delegate void CloseDelegate();
CloseDelegate closeDelegate;

public MainWindow()
{
InitializeComponent();
isPressed = false;
entryTouch = false;
}
private void Btn_MouseDown(object sender, RoutedEventArgs e)
{
isPressed = true;
if (th != null)

{
if (th.ThreadState == ThreadState.Running || th.ThreadState == ThreadState.WaitSleepJoin)
{
th.Abort();
}
}
th = new Thread(new ThreadStart(()=>{
Thread.Sleep(1500);
if (isPressed)
{
MessageBox.Show("長按1.5秒");
isPressed = false;
}
else
{
MessageBox.Show("少於1.5秒");
}
}));
th.Start();

}
private void Btn_MouseUp(object sender, RoutedEventArgs e)
{
if (entryTouch)
{
MessageBox.Show("進入長按事件");
}
isPressed = false;
}
}
}

C#長按事件