该问题来自论坛提问,两个api函数
GetLastInputInfo:获取闲置时间
ShowCursor:设置鼠标状态,这里要注意,本函数并不能直接影响鼠标状态,而是设置状态计数器,参数为True时计数器+1,反之-1,只有当计数器大于等于0时鼠标为显示,小于0时鼠标隐藏。所以可能会发生某次调用该函数而没有生效的情况。为了避免这个问题,可以用wile循环判断结果。
- using
System;
- using
System.Windows.Forms;
- using
System.Runtime.InteropServices;
- namespace
WindowsApplication5
- {
-
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
LASTINPUTINFOplii);
-
-
-
-
-
- [DllImport(
"user32.dll"
,EntryPoint=
"ShowCursor"
,CharSet=CharSet.Auto)]
-
public
static
extern
int
ShowCursor(
bool
bShow);
-
public
Form1()
- {
- InitializeComponent();
-
- System.Windows.Forms.Timertimer=
new
Timer();
- timer.Enabled=
true
;
- timer.Interval=100;
- timer.Tick+=
new
EventHandler(timer_Tick);
- }
-
-
int
iCount=0;
-
void
timer_Tick(
object
sender,EventArgse)
- {
-
-
long
i=getIdleTick();
-
if
(i>5000)
- {
-
while
(iCount>=0)
- {
- iCount=ShowCursor(
false
);
- }
- }
-
else
- {
-
while
(iCount<0)
- {
- iCount=ShowCursor(
true
);
- }
- }
- }
-
-
-
-
-
public
long
getIdleTick()
- {
- LASTINPUTINFOvLastInputInfo=
new
LASTINPUTINFO();
- vLastInputInfo.cbSize=Marshal.SizeOf(vLastInputInfo);
-
if
(!GetLastInputInfo(
ref
vLastInputInfo))
return
0;
-
return
Environment.TickCount-(
long
)vLastInputInfo.dwTime;
- }
-
- }
- }
优质内容筛选与推荐>>
1、转 美制电线标准AWG与公制、英制单位对照2、Keil中的警告的解决3、asp.net HttpHand和HttpModule的详细解释,包括Asp.Net对Http请求的处理流程。4、php-fpm-运行原理(转)5、职业理想
长按二维码向我转账
受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。