windows程序自动锁屏功能的实现
2010-02-09 14:24
483 查看
这个问题来自论坛提问
Q:当我登录进入主界面时,在规定的时间内没有做任何操作,系统就注销(就像电脑的屏保)。
A: 其实很简单,参考一个api函数就可以了:GetLastInputInfo,以下是参考代码
演示程序在你10秒内没有任何鼠标键盘活动既最小化,最小化后晃动鼠标即可复原
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication28
... {
public partial class Form1 : Form
... {
[StructLayout(LayoutKind.Sequential)]
public struct LASTINPUTINFO
... {
[MarshalAs(UnmanagedType.U4)]
public int cbSize;
[MarshalAs(UnmanagedType.U4)]
public uint dwTime;
}
[DllImport( " user32.dll " )]
public static extern bool GetLastInputInfo( ref LASTINPUTINFO plii);
public long getIdleTick()
... {
LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
if ( ! GetLastInputInfo( ref vLastInputInfo)) return 0 ;
return Environment.TickCount - ( long )vLastInputInfo.dwTime;
}
public Form1()
... {
InitializeComponent();
}
private void Form1_Load( object sender, EventArgs e)
... {
this .timer1.Interval = 2000 ;
this .timer1.Enabled = true ;
}
private void timer1_Tick( object sender, EventArgs e)
... {
long i = getIdleTick();
this .Text = string .Format( " jinjazz说:您已经{0}ms没有动了 " ,i);
if (i > 10 * 1000 )
... {
this .WindowState = FormWindowState.Minimized;
}
else
... {
this .WindowState = FormWindowState.Normal;
}
}
}
}
Q:当我登录进入主界面时,在规定的时间内没有做任何操作,系统就注销(就像电脑的屏保)。
A: 其实很简单,参考一个api函数就可以了:GetLastInputInfo,以下是参考代码
演示程序在你10秒内没有任何鼠标键盘活动既最小化,最小化后晃动鼠标即可复原
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication28
... {
public partial class Form1 : Form
... {
[StructLayout(LayoutKind.Sequential)]
public struct LASTINPUTINFO
... {
[MarshalAs(UnmanagedType.U4)]
public int cbSize;
[MarshalAs(UnmanagedType.U4)]
public uint dwTime;
}
[DllImport( " user32.dll " )]
public static extern bool GetLastInputInfo( ref LASTINPUTINFO plii);
public long getIdleTick()
... {
LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
if ( ! GetLastInputInfo( ref vLastInputInfo)) return 0 ;
return Environment.TickCount - ( long )vLastInputInfo.dwTime;
}
public Form1()
... {
InitializeComponent();
}
private void Form1_Load( object sender, EventArgs e)
... {
this .timer1.Interval = 2000 ;
this .timer1.Enabled = true ;
}
private void timer1_Tick( object sender, EventArgs e)
... {
long i = getIdleTick();
this .Text = string .Format( " jinjazz说:您已经{0}ms没有动了 " ,i);
if (i > 10 * 1000 )
... {
this .WindowState = FormWindowState.Minimized;
}
else
... {
this .WindowState = FormWindowState.Normal;
}
}
}
}
相关文章推荐
- windows程序自动锁屏功能的实现
- windows程序自动锁屏功能的实现
- 利用PHP安装windows自动运行的服务,PHP程序可以实现长时间、自动运行、定时更新功能,直接可以用在项目中的类源代码
- 转:Windows任务计划实现自动执行ArcGIS相关功能
- Android 程序自动更新功能模块实现
- Android 程序自动更新功能模块实现
- Windows下C++实现线程池功能(固定线程数,自动调整未实现)(
- (转载整理)程序安装流程及如何实现文件在系统后自动重启(windows)
- Motorola 企业移动终端(Mobile Computers)冷启动后软件程序自动安装的功能和实现
- ASP 程序实现自动升级功能
- 用程序自动实现一些功能的方式
- 在WINDOWS任务计划程序下执行PHP文件 PHP定时功能的实现
- ASP 程序实现自动升级功能
- windows下用vim实现qt代码的自动补全功能(zz)
- FL2440-A7模块用程序实现GPS自动定位解析功能
- 在windows平台下electron-builder实现前端程序的打包与自动更新
- Android 程序自动更新功能模块实现
- 基于Visual C++之Windows核心编程代码分析(3)实现程序自动安装
- 2005中ClickOnce实现程序的自动升级功能。