程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python implementation of popocat Recycle Bin icon replacement

編輯:Python

1. The premise to prepare / Thought record ( Thinking process , The disorderly !)

At the beginning, various search engines , Blog , Data collection , Preparation , The following steps are optimized in the later stage , At the beginning, I recorded all kinds of useful , It's a mess , Code / The finished product can be viewed directly from the back

Via desktop 【 Right click 】—>【 Individualization 】—>【 The theme 】—>【 Desktop icon settings 】—>【 Change icon 】 You can see that the default icon is imageres.dll , This is a windos Default icon library .

Win 10 Style icon library has :

  1. ddores.dll

  2. dmdskres.dll

  3. imageres.dll

  4. mmres.dll

  5. networkexplorer.dll

  6. pnidui.dll

  7. sensorscpl.dll

  8. setupapi.dll

  9. shell32.dll

  10. wmploc.dll

  11. wpdshext.dll For more information, please refer to the blog :Windos Icon dll

1.1. Thought record

When I didn't know anything at first , Thinking about , Write a program to automatically replace this dll, Call the website to convert the icon format : transformation ico Then write a program to run ;
The procedure is mainly divided into the following parts :

  1. Replace dll

  2. Call the website to convert the icon format 【 Optional 】, It can also be called by program

  3. Automatically generate new dll【 Is difficult , Optional 】emmmm, It's not very big , You can refer to post (C++)

  4. Modify registry , Update the value of the recycle bin

  5. Desktop icon registry correspondence

1.2. pythonmagick

Third-party module ( At first I wanted to write GUI, eureka TKInterDesigner, But there are BUG, My computer doesn't work , Put it on hold ):
pythonmagick, You can't pip install, Manual required download , Pay attention to the version and CPU framework ( Check... By command pip debug --verbose, My is cp38-cp38-win_amd64)!cp10 Corresponding python edition 3.10,cp38 Corresponding python edition 3.8

import PythonMagickimg = PythonMagick.Image("logo.png") # Load the image to be converted img.sample('64x64') # Set the generated icon Size img.write("logo.ico") # Generate icon And save 

Where the Recycle Bin icon is located :

  1. 【 Restore 】 Location of the recycle bin default icon : HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{645FF040-5081-101B-9F08-00AA002F954E}\DefaultIcon

  2. 【 change 】 The location of the recycle bin custom icon : HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\CLSID\{645FF040-5081-101B-9F08-00AA002F954E}\DefaultIcon

    Items under the directory emptyfull The value of corresponds to empty and full recycle bin respectively ( Non empty ) When icon Icon position ;

  • If in the registry [HKEY_CURRENT_USER\Software\Classes\CLSID](WIN9X) or [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\CLSID](WIN2000/XP) The icon or name of a system folder is defined in , It will take precedence over [ HKEY_CLASSES_ROOT\CLSID ]. My computer on the desktop 、 My documents 、 Icons and names such as network places are defined here .

  • The types of values of items are REG_EXPAND_SZ, The data are : ( Default ): %SystemRoot%\System32\imageres.dll,-55 Empty: %SystemRoot%\System32\imageres.dll,-55 Full: %SystemRoot%\System32\imageres.dll,-54

2. The code analysis

2.1. ICO Icon conversion

take icons The pictures in the folder are converted to ico File and store it in iocn Under the folder ;

import osimport PythonMagickdef get_files(): filenames = os.listdir(os.path.join(os.getcwd(), 'icons')) for index, item in enumerate(filenames): filenames[index] = './icons/' + item return filenamesdef convert(sava_path): if not os.path.exists(sava_path) or os.path.isfile(sava_path): os.mkdir(sava_path) else: os.mkdir(sava_path) filenames = get_files() for item in filenames: img = PythonMagick.Image(item) # Load the image to be converted img.sample('256x256') # Set the generated icon Size img.write('./icon/' + item[8:len(item) - 3] + 'ico') # Generate icon And save if __name__ == '__main__': convert("icon")

2.2. Generate dll

Here I pass VS Create a new DLL project , Then import the... From the previous step ico file , Then generate . You can also directly ico The suffix to dll, In this case, the next step of code related logic also needs to be changed a little .

  1. Create a dynamically linked library project

  2. Add resource file (ico Icon )

  3. Generate dll

  4. From the generated path dll Copy ( Scrapbooking is OK ) One spare .

2.3. Modify registry

dll Files need to be stored locally , So I chose to store it in TEMP Under the folder , It can also be stored in other folders , Storage in... Is not recommended system32 Next .

import osimport winregdll_name = "cookie_icon.dll"dll_dir_path = os.path.join(os.getenv("TEMP"), "{645FF040-5081-101B-9F08-00AA002F954E}")dll_file_path = os.path.join(dll_dir_path, dll_name)key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Software\\Microsoft\\Windows\\CurrentVersion\\" r"Explorer\\CLSID\\{645FF040-5081-101B-9F08-00AA002F954E}" r"\\DefaultIcon", 0, winreg.KEY_ALL_ACCESS)try: winreg.SetValueEx(key, "empty", 0, winreg.REG_EXPAND_SZ, dll_file_path + ",0") winreg.SetValueEx(key, "full", 0, winreg.REG_EXPAND_SZ, dll_file_path + ",1") print(" Replacement successful ~ Double click the recycle bin to view the effect ") # TODO: Join the refresh mechanism , Refresh the desktop , After implementation, you can see the icon update effect without clicking the recycle bin # At present, most of them are found on the Internet taskkill explorer.exe And then restart , It will cause the desktop to jam temporarily , Generating zombie processes os.system("pause")except WindowsError as e: print("WindowsError: " + e)finally: winreg.CloseKey(key)

3. Results display

results ! My energy !!! Sobbing ~

4. Resource sharing

The relevant code has been packaged as exe 了 , Just run directly ( Limited to Windows),mac Never played , I do not know! , A little . Link fetch ;
Use pyinstaller pack :

pyinstaller -F -i ./popocat_full.ico popocat.py

Generate a single file exe, But there is no way to package external files into exe in , There are self extracting packages that can be packed in , But it can not achieve the desired effect .
So Baidu found Enigma Virtual Box Package software .

After packaging exe Self taking

5. summary

Although the actual code looks short , But if you really don't understand it, you really can't write it out , First, choose Java, It seems that the bottom layer of the operating system still needs to be found C Language , Switch to C After the language is written , But I have a headache writing about handles , Finally found out winreg This python library , It really works , Put a lot of useless ( Strictly speaking , Is not commonly used ) The parameters of are encapsulated .

If you're okay, you have to do more , Much more trouble , Life is gone .
author : My eyelashes are in my eyes !

Game programming , A game development favorite ~

If the picture is not displayed for a long time , Please use Chrome Kernel browser .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved