c#如何获知机器处于锁屏状态 如题,望大虾指点。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://blog.csdn.net/windless0530/article/details/5496469 @mjfmjf,非常感谢,思路非常重要!以下代码本地调试成功。class SessionChangeHandler : Control // allows us to override WndProc { [DllImport("WtsApi32.dll")] private static extern bool WTSRegisterSessionNotification(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)]int dwFlags); [DllImport("WtsApi32.dll")] private static extern bool WTSUnRegisterSessionNotification(IntPtr hWnd); private const int NOTIFY_FOR_THIS_SESSION = 0; private const int WM_WTSSESSION_CHANGE = 0x2b1; private const int WTS_SESSION_LOCK = 0x7; private const int WTS_SESSION_UNLOCK = 0x8; public SessionChangeHandler() { if (!WTSRegisterSessionNotification(this.Handle, NOTIFY_FOR_THIS_SESSION)) { Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error()); } } protected override void OnHandleDestroyed(EventArgs e) { // unregister the handle before it gets destroyed WTSUnRegisterSessionNotification(this.Handle); base.OnHandleDestroyed(e); } protected override void WndProc(ref Message m) { if (m.Msg == WM_WTSSESSION_CHANGE) { int value = m.WParam.ToInt32(); if (value == WTS_SESSION_LOCK) { OnMachineLocked(EventArgs.Empty); } else if (value == WTS_SESSION_UNLOCK) { OnMachineUnlocked(EventArgs.Empty); } } base.WndProc(ref m); } protected virtual void OnMachineLocked(EventArgs e) { EventHandler temp = myMachineLockedHandler; if (temp != null) { temp(this, e); } } protected virtual void OnMachineUnlocked(EventArgs e) { EventHandler temp = myMachineUnlockedHandler; if (temp != null) { temp(this, e); } } public EventHandler myMachineLockedHandler { get; set; } public EventHandler myMachineUnlockedHandler { get; set; } }以上代码摘录自http://pinvoke.net/default.aspx/wtsapi32.WTSRegisterSessionNotification 一个关于正则表达式的问题!!! 求书````关于重写控件的书``` C# string类型如何转换为ref char ListView or GridView 我想用HTML的按钮 执行WEB按钮里面的方法怎么办? 如何在空TreeNode前面显示可展开标记(就是那个小十字方框) 关于C#,线程中出现未将对象引用设置到对象的实例的错误, 一个有关组件的问题 请用过朝暮数据 http://www.zhaomu.com/ 空间的大侠进来看一下 怎样编程实现用给定的R,G,B三个值来为textbox的BackColor负值? 判断输入的数是否为素数 VB.net转C#问题
class SessionChangeHandler : Control // allows us to override WndProc
{
[DllImport("WtsApi32.dll")]
private static extern bool WTSRegisterSessionNotification(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)]int dwFlags);
[DllImport("WtsApi32.dll")]
private static extern bool WTSUnRegisterSessionNotification(IntPtr hWnd); private const int NOTIFY_FOR_THIS_SESSION = 0;
private const int WM_WTSSESSION_CHANGE = 0x2b1;
private const int WTS_SESSION_LOCK = 0x7;
private const int WTS_SESSION_UNLOCK = 0x8; public SessionChangeHandler()
{
if (!WTSRegisterSessionNotification(this.Handle, NOTIFY_FOR_THIS_SESSION))
{
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
}
} protected override void OnHandleDestroyed(EventArgs e)
{
// unregister the handle before it gets destroyed
WTSUnRegisterSessionNotification(this.Handle);
base.OnHandleDestroyed(e);
} protected override void WndProc(ref Message m)
{
if (m.Msg == WM_WTSSESSION_CHANGE)
{
int value = m.WParam.ToInt32();
if (value == WTS_SESSION_LOCK)
{
OnMachineLocked(EventArgs.Empty);
}
else if (value == WTS_SESSION_UNLOCK)
{
OnMachineUnlocked(EventArgs.Empty);
}
}
base.WndProc(ref m);
} protected virtual void OnMachineLocked(EventArgs e)
{
EventHandler temp = myMachineLockedHandler;
if (temp != null)
{
temp(this, e);
}
} protected virtual void OnMachineUnlocked(EventArgs e)
{
EventHandler temp = myMachineUnlockedHandler;
if (temp != null)
{
temp(this, e);
}
} public EventHandler myMachineLockedHandler { get; set; } public EventHandler myMachineUnlockedHandler { get; set; }
}以上代码摘录自http://pinvoke.net/default.aspx/wtsapi32.WTSRegisterSessionNotification