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