win32ts.WTSRegisterSessionNotification
監控計算機狀態改變
https://msdn.microsoft.com/en-us/library/aa383841(v=vs.85).aspxsession
用戶狀態改變後,執行函數myststa(event),其中event表明用戶狀態信息,具體值查
看https://msdn.microsoft.com/en-us/library/aa383828(v=vs.85).aspx
代碼:ide
import win32con import win32gui import win32ts WM_WTSSESSION_CHANGE = 0x2B1 class WTSMonitor(): className = "WTSMonitor" wndName = "WTS Event Monitor" def __init__(self): wc = win32gui.WNDCLASS() wc.hInstance = hInst = win32gui.GetModuleHandle(None) wc.lpszClassName = self.className wc.lpfnWndProc = self.WndProc self.classAtom = win32gui.RegisterClass(wc) style = 0 self.hWnd = win32gui.CreateWindow(self.classAtom, self.wndName, style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, hInst, None) win32gui.UpdateWindow(self.hWnd) win32ts.WTSRegisterSessionNotification(self.hWnd, win32ts.NOTIFY_FOR_ALL_SESSIONS) def start(self): win32gui.PumpMessages() def stop(self): win32gui.PostQuitMessage(0) def WndProc(self, hWnd, message, wParam, lParam): if message == WM_WTSSESSION_CHANGE: self.OnSession(wParam, lParam) def OnSession(self, event, sessionID): print(event) myststa(event) if __name__ == '__main__': m = WTSMonitor() m.start()