Python enables applications to add the open mode function in the context menu, and python context menu
Recently, a small tool developed by the project team wants to add an open method in the right-click menu. The youdao cloud note is used as an example to describe the demand and compile the code.
1. Demand dismantling:
How to manually add the right-click menu to open the menu:
Step 1:Open Registry Editor, Win + R-> enter "regedit"
Step 2:Add a key: YNote in HKEY_CLASSES_ROOT/*/shell (or HKEY_LOCAL_MACHINE/SOFTWARE/Classes/*/shell with the same directory), and then create a new command in this item, then, edit the string, add the Application Path, add space and "% 1" after the path and name, and right-click the application to find the YNote opening method.
2. Code Implementation
Method1: implemented through the _ winreg module:
import _winregfrom _winreg import KEY_ALL_ACCESSwith _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Classes\*\shell") as key:print keynewKey = _winreg.CreateKeyEx(key,"YNote",0,KEY_ALL_ACCESS)sub_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,r"SOFTWARE\Classes\*\shell\YNote")newsubKey = _winreg.CreateKey(sub_key,"command")_winreg.SetValue(newsubKey,"(Default)",1,"\"C:\Program Files (x86)\Youdao\YoudaoNote\YoudaoNote.exe\" \"%1\"")
Method2:Implemented through win32api and win32con Module
import win32apiimport win32conkey = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE,r"SOFTWARE\Classes\*\shell")newKey = win32api.RegCreateKey(key,"YNote")sub_key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE,r"SOFTWARE\Classes\*\shell\YNote")newsubKey = win32api.RegCreateKey(sub_key,"command")win32api.RegSetValue(newsubKey,"(Default)", win32con.REG_SZ,"\"C:\Program Files (x86)\Youdao\YoudaoNote\YoudaoNote.exe\" \"%1\"")
The above section describes how to add the open mode function to the python implementation application in the right-click menu. I hope it will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!