wpf 窗体自动关闭

.Net技术 码拜 8年前 (2016-04-29) 1825次浏览
各位高手,本人想写一个程序,点击按钮后的30秒假如窗口没有任何操作,窗口自动关闭。该怎么写?(本人刚学wpf,菜鸟一个请高手赐教)能否给个代码。
private void Button_Click(object sender, RoutedEventArgs e)
{
timer = new DispatcherTimer();
timer.Tick += new EventHandler(timer_Tick);
}
private void timer_Tick(object sender, EventArgs e)
{
this.Close();
}
private void MainWindow_TouchDown(object sender, TouchEventArgs e)
{
int i = 60;
if (timer.Interval != null)
{
timer.Interval = new TimeSpan(0, 0, i);
timer.Start();
}][/code]
解决方案

4

WPF很久不用了。你这是60s,你这样不行吗?

12

下面的代码是获取上次鼠标键盘操作的到现在的时间间隔。在定时器中调用这个函数

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
        /// <summary>
        /// 获取键盘和鼠标没有操作的时间
        /// </summary>
        /// <returns>用户上次使用系统到现在的时间间隔,单位为秒</returns>
        public static long GetLastInputTime()
        {
            LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
            vLastInputInfo.CBSize = Marshal.SizeOf(vLastInputInfo);
            if (!GetLastInputInfo(ref vLastInputInfo))
            {
                return 0;
            }
            else
            {
                long count = Environment.TickCount - (long)vLastInputInfo.DWTime;
                long icount = count / 1000;
                return icount;
            }
        }

3

还在用wpf。刚学习不到3个月。

10

不需要,使用的是系统的dll

10

private void timer_Tick(object sender, EventArgs e)
{
this.Dispatcher.BeginInvoke(new CloseDel(Close));
}
private delegate void CloseDel();
private void Close()
{
this.Close();
}
线程间操作无效,委托关闭就OK了

6

引用:
Quote: 引用:

下面的代码是获取上次鼠标键盘操作的到现在的时间间隔。在定时器中调用这个函数

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
        /// <summary>
        /// 获取键盘和鼠标没有操作的时间
        /// </summary>
        /// <returns>用户上次使用系统到现在的时间间隔,单位为秒</returns>
        public static long GetLastInputTime()
        {
            LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
            vLastInputInfo.CBSize = Marshal.SizeOf(vLastInputInfo);
            if (!GetLastInputInfo(ref vLastInputInfo))
            {
                return 0;
            }
            else
            {
                long count = Environment.TickCount - (long)vLastInputInfo.DWTime;
                long icount = count / 1000;
                return icount;
            }
        }

你好。你给的上面的三行代码是非托管引用吗直接写进去不用在添加其他引用吗

不用,直接写可以用


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明wpf 窗体自动关闭
喜欢 (0)
[1034331897@qq.com]
分享 (0)