I have a different requirement in one of my projects, when I run my exe and make it idle (i.e. without click, min, max), after a period of time (timer) it should be automatically closed. If开发者_运维知识库 anyone clicked before the particular time, the timer must reset for the same period. How can I find out whether the exe is idle or not?
You might want to take a look at the Application.Idle
event (Note: Only applicable to a WinForms application, as far as I'm aware).
If you combine it with a timer that you stop/reset whenever your application receives input, that should give you pretty much what you're looking for.
public class GlobalMouseHandler : IMessageFilter
{
public delegate void EventHandlerForActiveState();
public event EventHandlerForActiveState onActive;
public event EventHandlerForActiveState onStateChanged;
private const int WM_KEYDOWN = 0x100;
//private const int WM_HSCROLL = 0x114;
//private const int WM_VSCROLL = 0x115;
private const int WM_LBUTTONDOWN = 0x201;
private const int WM_LBUTTONUP = 0x202;
private const int WM_RBUTTONDOWN = 0x204;
private const int WM_RBUTTONUP = 0x205;
//private const int WM_MBUTTONDBLCLK = 0x209;
private const int WM_MOUSEWHEEL = 0x20A;
private const int WM_GETMINMAXINFO = 0x024;
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == 275)
{
return false;
}
switch (m.Msg)
{
case WM_LBUTTONDOWN:
if (onActive != null)
onActive();
break;
case WM_LBUTTONUP:
if (onActive != null)
onActive();
break;
case WM_RBUTTONDOWN:
if (onActive != null)
onActive();
break;
case WM_RBUTTONUP:
if (onActive != null)
onActive();
break;
case WM_MOUSEWHEEL:
if (onActive != null)
onActive();
break;
//case WM_ACTIVATE:
// if (onActive != null)
// onActive();
// break;
case WM_KEYDOWN:
if (onActive != null)
onActive();
break;
case WM_GETMINMAXINFO:
if (onStateChanged != null)
onStateChanged();
break;
//case WM_HSCROLL:
// if (onActive != null)
// onActive();
// break;
//case WM_VSCROLL:
// if (onActive != null)
// onActive();
// break;
}
return false;
}
}
GlobalMouseHandler handle = new GlobalMouseHandler(); handle.onActive += new GlobalMouseHandler.EventHandlerForActiveState(handle_onActive); Application.AddMessageFilter(handle); I used this class and done this.
精彩评论