Skip to content

Commit

Permalink
modefy
Browse files Browse the repository at this point in the history
  • Loading branch information
raoyi committed Nov 18, 2017
1 parent cbc5279 commit 8e61ba3
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# ScreenSwitch
automatic screen switch
关闭显示器
99 changes: 99 additions & 0 deletions ScreenSwitch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#python 3.4 win7
#关闭显示器
from ctypes import *
import sys
import time

#定义变量
HWND_BROADCAST = 0xffff
WM_SYSCOMMAND = 0x0112
SC_MONITORPOWER = 0xf170
MonitroPowerOn = -1 #打开显示器
MonitorGoLowPower = 1 #进入低电量
MonitorPowerOff = 2 #关闭显示器

def PowerOn():
windll.user32.PostMessageW(HWND_BROADCAST, WM_SYSCOMMAND,SC_MONITORPOWER, MonitroPowerOn)

def GoLowPower():
windll.user32.PostMessageW(HWND_BROADCAST, WM_SYSCOMMAND,SC_MONITORPOWER, MonitorGoLowPower)

def PowerOff():
windll.user32.PostMessageW(HWND_BROADCAST, WM_SYSCOMMAND,SC_MONITORPOWER, MonitorPowerOff)

if len(sys.argv) == 2:
if sys.argv[1] == 'on':
PowerOn()
exit()
elif sys.argv[1] == 'low':
GoLowPower()
exit()
elif sys.argv[1] == 'off':
PowerOff()
exit()

if len(sys.argv) == 3:
startime = sys.argv[1].split(':')
endtime = sys.argv[2].split(':')

#获取开始/结束分钟
if len(startime) >= 2 and int(startime[1]) >= 0 and int(startime[1]) < 60:
startm = int(startime[1])
else:
startm = 0

if len(endtime) >= 2 and int(endtime[1]) >= 0 and int(endtime[1]) < 60:
endm = int(endtime[1])
else:
endm = 0

#获取开始/结束小时
if int(startime[0]) >= 0 and int(startime[0]) < 24:
starth = int(startime[0])

if int(endtime[0]) >= 0 and int(endtime[0]) < 24:
endh = int(endtime[0])

#程序开始
while True:
#获取当前时间(小时/分钟),用于关屏
nowtime = time.asctime().split(' ')[3]
nowh = int(nowtime.split(':')[0])
nowm = int(nowtime.split(':')[1])
#关闭屏幕
if nowh > starth or (nowh == starth and nowm >= startm):
PowerOff()

while True:
#获取当前时间(小时/分钟),用于开屏
nowtime = time.asctime().split(' ')[3]
nowh = int(nowtime.split(':')[0])
nowm = int(nowtime.split(':')[1])
#开启屏幕
if nowh == endh and nowm == endm:
PowerOn()
exit()
else:
time.sleep(50)
else:
print('Effect:Screen switch or Timing Screen Switch.\n\
\n\
Usage:\n\
(1)Screen Switch\n\
Format:\n\
ToolName [on] [off] [low]\n\
Parameter:\n\
on - Switch on\n\
off - Switch off\n\
low - Monitor low power\n\
\n\
(2)Timing Screen Switch\n\
Format:\n\
ToolName OffTime OnTime\n\
Parameter:\n\
OffTime - Screen power off time.\n\
(Time format(00:00), or just hour integer(0~23))\n\
OnTime - Screen power on time.\n\
(Time format(00:00), or just hour integer(0~23))')
input()
exit()

0 comments on commit 8e61ba3

Please sign in to comment.