img { width: 30vw }

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;

Type Dword -Force;

最新文章

  1. [Java 进阶]异常
  2. CleanBlog(个人博客+源码)
  3. Matlab中给figure添加图例(legend),标题(title)和颜色(color)
  4. postman+newman(2)
  5. Sqoop-1.4.6.bin__hadoop-2.0.4-alpha 环境搭建
  6. Tomcat7.x 与 Tomcat6.x
  7. TCP非阻塞accept和非阻塞connect
  8. iOS 屏幕旋转 nav+tabbar+present(网页) 2016
  9. 2-SAT模板
  10. C++ 函数映射使用讲解
  11. unix & linux oralce用户 内存使用情况分析
  12. The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
  13. ubuntu 下查看caj文件
  14. Django项目的创建及基本使用
  15. iOS开发-工厂模式
  16. HDU1272小希的迷宫–并查集
  17. STM8的数据@near @tiny定义
  18. Python3基础 lambda 简单示例
  19. 安卓出现Invalid layout of java.lang.String at value
  20. MySQL Transaction--RC和RR区别

热门文章

  1. IDEA windows版本快捷键
  2. IDEA找不到类但实际存在的问题解决
  3. frp 用于内网穿透的基本配置和使用
  4. 编程式导航路由跳转到当前路由(参数不变), 多次执行会抛出NavigationDuplicated的警告错误?
  5. Xilinx DMA的几种方式与架构
  6. 用QT制作3D点云显示器——QtDataVisualization
  7. TypeScript(7)泛型
  8. SAP 动态选择屏幕实例
  9. 如何修改 node_modules 里的文件
  10. Failed to Setup IP tables: Unable to enable SKIP DNAT rule: (iptables failed: iptables --wait -t nat -I DOCKER -i br-b1938128a963