windows自动切换深色模式(夜晚模式)

windows系统上怎么根据日出日落时间判断切换为深色模式或浅色模式呢?

windows系统自带了一个叫做“任务计划程序”的软件。可以通过“开始菜单”中的搜索找到。

然后选择“创建基本任务”

然后“触发器”选择每天,“操作”选启动程序,“程序”用powershell,可以直接复制下面的path

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe

添加参数输入下面的脚本,要先修改一下经纬度参数,我这个是成都的lat=31&lng=103,经度31&维度103。其他不用修改。后面会讲解这段脚本。

$IsLight;$Daylight = (Invoke-RestMethod "http://api.sunrise-sunset.org/json?lat=31&lng=103&formatted=0").results;$Sunrise  = ($Daylight.Sunrise | Get-Date);$Sunset   = ($Daylight.Sunset | Get-Date);if(($Daylight.Sunrise | Get-Date) -lt (get-date)) {$IsLight=0} elseif(($Daylight.Sunset | Get-Date) -lt (get-date)) {$IsLight=1}New-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name SystemUsesLightTheme -Value $IsLight -Type Dword -Force; New-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name AppsUseLightTheme -Value $IsLight -Type Dword -Force;

最后在添加两个触发器,一共3个触发器,早晚各运行一次,在加登录系统时运行一次,这样就保证脚本一定会运行,从而切换模式。当然早晚时间要自己斟酌一下,根据自己的习惯来。

解释一下脚本的内容。最核心的是最后两行命令,设置 SystemUsesLightTheme 和 AppsUseLightTheme 这两个系统的变量为0或1,通过名字很好理解其含义。

上面的代码就是求是要设置为0还是1。最重要的就是通过一个api接口获取所在经纬度的日出日落时间,通过与当前时间做对比从而确定需要深色还是浅色。当前时间超过日落时间,就要深色,否则如果当前时间超过日出时间,就要浅色。

$IsLight;
$Daylight = (Invoke-RestMethod "http://api.sunrise-sunset.org/json?lat=31&lng=103&formatted=0").results;
$Sunrise  = ($Daylight.Sunrise | Get-Date);
$Sunset   = ($Daylight.Sunset | Get-Date);
if(($Daylight.Sunrise | Get-Date) -lt (get-date)) {
$IsLight=0
} elseif(($Daylight.Sunset | Get-Date) -lt (get-date)) {
$IsLight=1
}
New-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name SystemUsesLightTheme -Value $IsLight -Type Dword -Force; 
New-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name AppsUseLightTheme -Value $IsLight -Type Dword -Force;
posted @ 2022-08-18 23:14  让速不让路  阅读(1188)  评论(1编辑  收藏  举报