c#如何获知机器处于锁屏状态

张叫兽的技术研究院 2012-04-11 12:11:22
如题,望大虾指点。
...全文
222 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
@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
mjfmjf 2012-04-12
  • 打赏
  • 举报
回复
http://blog.csdn.net/windless0530/article/details/5496469
  • 打赏
  • 举报
回复
吃饭的大虾回来了吗?
startstartsvip 2012-04-11
  • 打赏
  • 举报
回复
一个多小时了还没折腾出来?


小儿 来碗大虾面

坐等大虾

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧