vue3+electron开发桌面软件(8)—— electron操作注册表,实现windows二级级联菜单

系列文章目录

系列第一篇: vue3+electron开发桌面软件入门与实战(0)——创建electron应用



前言

上一篇文章我们已经介绍过如何通过手动操作注册表的方式,自定义我们的二级级联菜单。下面我们要做的就是,怎么在electron中操作注册表,复现上篇文章实现的功能。


一、nsis脚本

如果你查看过本系列文章:vue3+electron开发桌面软件(6)——系统级右键实现文件上传,那么应该了解electron是通过nsis来操作注册表的。如果没有特别浓厚的兴趣,我同样不建议去深入研究nsis,因为这种工具,目前来看,应该只是我们前进路上的一个小插曲,实在不值得浪费太多时间。
我这里已经写好.nsh脚本:

!macro customInstall

   SetRegView 64
   WriteRegStr HKCR "*\shell\test文件上传" "SubCommands" "test.tome.outside;test.tome.secret;test.toother;test.get"
   WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\test.tome.outside" "" "发送给自己-互联网"
   WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\test.tome.secret" "" "发送给自己-涉密网"
   WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\test.toother" "" "发送给他人"
   WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\test.get" "" "接收"
   WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\test.tome.outside\command" "" '"$INSTDIR\test-NOSECRET.exe" "upload-tome-outside" "%1"'
   WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\test.tome.secret\command" "" '"$INSTDIR\test-NOSECRET.exe" "upload-tome-secret" "%1"'
   WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\test.toother\command" "" '"$INSTDIR\test-NOSECRET.exe" "upload-toother" "%1"'
   WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\test.get\command" "" '"$INSTDIR\test-NOSECRET.exe" "upload-get" "%1"'

!macroend
;卸载时清除
!macro customUninstall
   DeleteRegKey HKCR "*\shell\test文件上传"
   DeleteRegKey HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\test.tome.outside"
   DeleteRegKey HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\test.tome.secret"
   DeleteRegKey HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\test.toother"
   DeleteRegKey HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\test.get"
!macroend

二、脚本讲解

先解释用法的关键点:

  1. WriteRegStr:注册表写入,后面的参数是写入的地址,包含了上篇文章提到的项、数值、字符串等概念。
  2. HKCR:地址简写,全称为:“HKEY_CLASSES_ROOT”
  3. DeleteRegKey:注册表删除

这里有几个注意点:

  1. SetRegView 64:提供访问X64注册表的能力,如果不设置,最终的命令会注册到地址“计算机\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\”中,在这里维护的命令,无法成功调用(我的64位电脑是这样)。而我们上篇文章介绍过,正确的命令注册地址是:“计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\”
  2. 创建“*\shell\test文件上传”项时,不要先创建test文件上传,再创建其属性值SubCommands,有可能会出现上篇文章提到的SubCommands默认项为空的情况,导致二级菜单无法展开。建议按照我的写法,替换成自己的命令行。
  3. 打包成软件后,安装时,选择为所有用户安装,否则会因为权限问题,无法正常写入注册表。如果不想为所有用户安装,需要使用解决下权限问题。
  4. 因为点赞收藏过少,作者君总是彻夜难眠,秀发难全。

总结

按照惯例,我应该把上面第二节的内容放到这里,但是没办法,如果放过来,显得文章太单薄。不过文章体量不大,其实知识点很有价值,直接参考文章的同学,是不会理解我把注册表一项一项地翻一遍,查找二级菜单失效原因时的苦恼的。网络上相关资源甚少,契合electron的就更少了。为了这么几行脚本,把64位和SubCommands空值的坑趟过去,我把Microsoft官网关于注册表的知识都快翻几遍了,衷心希望后面的同学通过上面文章,不需要浪费时间在细枝末节上。

至于为什么把注册表操作水成两篇文章,当然是因为学(认)习(证)需要o(╯□╰)o

猜你喜欢

转载自blog.csdn.net/zjsj_lize/article/details/129948348