# 如何替 Windows 特定檔案類型添加檔案總管的滑鼠右鍵選單

由於我經常在寫文章，文章中的圖片大多需要壓縮處理，為了這個經常的操作，我會想辦法把一些常用的命令包裝成一個簡單的 Windows 檔案總管的右鍵選單功能，讓我只要對特定圖檔直接按下滑鼠右鍵，就可以直接壓縮圖片。今天我就來說說我是怎樣做到的。

![image](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/72eb0ec47d0f45efa730-d1d8d3df-d088-45bb-9ba2-883b5d7eaf6e.webp)

### 預覽套用 JPEG 圖片類型的右鍵選單效果

先給各位看看我的使用情境：

1.  開啟 Windows 檔案總管
    
2.  在任意圖片上按下滑鼠右鍵，出現右鍵選單
    
3.  用滑鼠點擊我自訂的 `&1 Squoosh 壓縮圖片` 選項即可立即執行壓縮圖片任務
    
    因為我有設定「快捷鍵」的關係，你也可以按下鍵盤 `1` 快速定位與執行該選項
    
    ![Windows 檔案總管右鍵選單](https://stwillblogassets.blob.core.windows.net/files/images/external/github.com/51c18f818e9cc088968f-b59dbe45-20f7-4f88-b92e-b8bfb48810d5.jpg)
    

如果你也想做到一樣的效果，那麼請繼續看下去吧！

### 替特定檔案類型建立右鍵選單的方法

開始設定之前，請先以系統管理員身分開啟 **Registry Editor** (登錄編輯程式) 應用程式！(`C:\Windows\regedit.exe`)

以下我以 `.jpg` 檔案類型為例，來說明如何設定這個檔案類型的右鍵選單功能：

1.  先到 `HKEY_CLASSES_ROOT\SystemFileAssociations` 機碼下找尋你想關聯的檔案類型
    
    例如你想關聯 `.jpg` 檔案，就找到 `HKEY_CLASSES_ROOT\SystemFileAssociations\.jpg\Shell` 機碼。
    
2.  在 `HKEY_CLASSES_ROOT\SystemFileAssociations\.jpg\Shell` 機碼下建立一個子機碼，名稱為你想要的**右鍵選單名稱**
    
    例如我想要的右鍵選單名稱是 `Squoosh_CLI`，那麼我就建立一個 `Squoosh_CLI` 的子機碼。
    
    如果你希望可以自訂右鍵選單的顯示名稱，可以修改這個 `Squoosh_CLI` 子機碼的預設字串值內容：
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/github.com/d6ccd867317d8b828b1b-0a907b07-ee9c-488d-8f7b-c6efd7ad7ca4.png)
    
    > 注意: 你可以在其中一個字元前加上一個 `&` 符號，該字元就會自動變成「快捷鍵」，例如我的設定是 `&1 Squoosh 壓縮圖片`，我在 `1` 前面加上 `&` 符號，那麼在右鍵選單中，你可以用鍵盤按 `1` 就能立刻執行這個命令，非常方便。
    
3.  建立一個 `HKEY_CLASSES_ROOT\SystemFileAssociations\.jpg\Shell\Squoosh_CLI\command` 機碼
    
    在這個機碼的預設字串值輸入你要執行的執行檔、批次檔都可以，並且加上 `%1` 參數代表傳入你在檔案總管中選取的檔案名稱。
    
    ```batch
    "C:\Tools\Squoosh CLI\squoosh-cli-wrapper.bat" "%1"
    ```
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/github.com/b5ce7e32b4c07ee42c77-51d8598c-ac69-414c-9b6b-eb6a808a48ce.png)
    
    > 注意: 若你的執行檔或批次檔的路徑中有空白字元，那麼你就必須把整個路徑用**雙引號**包起來，例如 `"C:\Tools\Squoosh CLI\squoosh-cli-wrapper.bat"`，而你選中的檔案，其檔名或路徑也有可能包含空白字元，所以建議也都要用**雙引號**包起來，例如：`"%1"`。
    

### 直接套用 \*.reg 檔快速設定機碼

例如以下是我的 `套用在圖片檔案類型.reg` 檔案內容：

```reg
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\SystemFileAssociations\.png\Shell\Squoosh_CLI]
@="&1 Squoosh 壓縮圖片"

[HKEY_CLASSES_ROOT\SystemFileAssociations\.png\Shell\Squoosh_CLI\command]
@="\"C:\\Tools\\Squoosh CLI\\squoosh-cli-wrapper.bat\" \"%1\""

[HKEY_CLASSES_ROOT\SystemFileAssociations\.jpg\Shell\Squoosh_CLI]
@="&1 Squoosh 壓縮圖片"

[HKEY_CLASSES_ROOT\SystemFileAssociations\.jpg\Shell\Squoosh_CLI\command]
@="\"C:\\Tools\\Squoosh CLI\\squoosh-cli-wrapper.bat\" \"%1\""

[HKEY_CLASSES_ROOT\SystemFileAssociations\.jpeg\Shell\Squoosh_CLI]
@="&1 Squoosh 壓縮圖片"

[HKEY_CLASSES_ROOT\SystemFileAssociations\.jpeg\Shell\Squoosh_CLI\command]
@="\"C:\\Tools\\Squoosh CLI\\squoosh-cli-wrapper.bat\" \"%1\""

[HKEY_CLASSES_ROOT\SystemFileAssociations\.webp\Shell\Squoosh_CLI]
@="&1 Squoosh 壓縮圖片"

[HKEY_CLASSES_ROOT\SystemFileAssociations\.webp\Shell\Squoosh_CLI\command]
@="\"C:\\Tools\\Squoosh CLI\\squoosh-cli-wrapper.bat\" \"%1\""

[HKEY_CLASSES_ROOT\SystemFileAssociations\.gif\Shell\Squoosh_CLI]
@="&1 Squoosh 壓縮圖片"

[HKEY_CLASSES_ROOT\SystemFileAssociations\.gif\Shell\Squoosh_CLI\command]
@="\"C:\\Tools\\Squoosh CLI\\squoosh-cli-wrapper.bat\" \"%1\""
```

建立好檔案後，直接用滑鼠雙擊這個檔案，就可以快速設定完成了！👍

### 總結

這個技巧可以非常方便的擴充 Windows 檔案總管在「特定檔案類型」的右鍵選單，在妥當的設定下，可以大幅提升工作的生產力，值得多多利用！

如果你想在「任意資料夾」或「任意檔案」設定右鍵選單的話，也可以參考 [contextmenu - How add context menu item to Windows Explorer for folders - Stack Overflow](https://stackoverflow.com/a/29769228/910074) 這篇優質文章！👍

### 相關連結

-   [Application Registration - Win32 apps | Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/shell/app-registration?WT.mc_id=DT-MVP-4015686)
    -   [Registering a Perceived Type](https://learn.microsoft.com/en-us/windows/win32/shell/app-registration?WT.mc_id=DT-MVP-4015686#registering-a-perceived-type)
-   [registry - Add menu item to Windows context menu only for specific filetype - Stack Overflow](https://stackoverflow.com/a/47745854/910074)
-   [contextmenu - How add context menu item to Windows Explorer for folders - Stack Overflow](https://stackoverflow.com/a/29769228/910074)
