版权声明:本文为博主原创文章,未经博主允许不得转载。https://blog.csdn.net/config_man/article/details/7484257
有触摸屏,xp系统,代码:
方法一:
class Win32
{
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool GetCursorPos(out POINT pt);
}
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
int x, y;
DispatcherTimer timer = new DispatcherTimer();
public MainWindow()
{
InitializeComponent();
setButtonStyle();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = new TimeSpan(0, 0, 10); //10秒后开始运行
}
void timer_Tick(object sender, EventArgs e)
{
Win32.POINT pi = new Win32.POINT();
Win32.GetCursorPos(out pi);
int _x = pi.X;
int _y = pi.Y;
if ((x + 4 == _x) && (y + 3 == _y))
{
timer.IsEnabled = false;
if (MessageBoxResult.Yes == MessageBox.Show("确定退出吗?", "友情提示", MessageBoxButton.YesNo))
{
this.Close();
}
}
}
//鼠标左键按下时
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//鼠标按下时获取当前鼠标坐标
x = (int)(Mouse.GetPosition(e.Source as FrameworkElement).X);
y = (int)(Mouse.GetPosition(e.Source as FrameworkElement).Y);
timer.IsEnabled = true;
}
//鼠标右键按下时
private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
x = -1;
y = -1;
timer.IsEnabled = false;
}
}
方法二:
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
int x, y;
DispatcherTimer timer = new DispatcherTimer();
DispatcherTimer timer2 = new DispatcherTimer();
DateTime start;
public MainWindow()
{
InitializeComponent();
setButtonStyle();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = new TimeSpan(0, 0, 1);
timer.Start();
timer2.Tick += new EventHandler(timer2_Tick);
timer2.Interval = new TimeSpan(0, 0, 2);
timer2.Start();
}
void timer_Tick(object sender, EventArgs e)
{
timer.Stop();
x = (int)(Mouse.GetPosition(this).X);
y = (int)(Mouse.GetPosition(this).Y);
}
bool ff = true;
void timer2_Tick(object sender, EventArgs e)
{
int _x = (int)(Mouse.GetPosition(this).X);
int _y = (int)(Mouse.GetPosition(this).Y);
if ((x == _x) && (y == _y) && ff)
{
start = DateTime.Now;
ff = false;
}
if (x != _x || y != _y)
{
x = _x;
y = _y;
start = DateTime.Now;
ff = true;
}
TimeSpan ts = DateTime.Now.Subtract(start);
//鼠标或键盘误动作3分钟后开始播放视频
if (ts.Minutes >= 3)
{
//MessageBox.Show("x:" + x.ToString() + ";y:" + y.ToString() + "\n _x:" + _x.ToString() + ";_y=" + _y.ToString()+"\n"+ff.ToString().ToString() + " " + ts.Hours.ToString() + " " + ts.Minutes.ToString() + " " + ts.Seconds.ToString());
//关闭所有子窗体
WindowCollection windows = Application.Current.Windows;
foreach(Window window in windows)
{
string title = window.Title;
if(title != "MainWindow")
{
window.Close();
}
}
}
}
}