1. 程式人生 > 其它 >C++選擇檔案開啟方式的函式

C++選擇檔案開啟方式的函式

最近讓同事給UE4一個功能,識別出 .ts、.json檔案,然後雙擊這些檔案可以直接開啟。


預設雙擊 .json 時,呼叫 Windows 自帶的記事本開啟檔案,不習慣,想著能否像右鍵選單一樣選擇用哪個應用程式開啟,比如我指定Visual Studio Code 或者 Sublime Text。

圖片也類似,預設不用系統自帶的,比如我安裝的其它瀏覽圖片工具(如下圖的 ImageGlass)


這個效果是“Open With‘,而不是‘Open’


程式碼也比較簡單,就是呼叫 SHOpenWithDialog 函式。

#include <iostream>
#include <Windows.h>
#include 
<ShlObj.h> int main() { //std::cout << "Hello World!\n"; //reference : https://stackoverflow.com/questions/18326507/how-to-get-command-line-of-windows-open-with-function OPENASINFO info = {0}; info.pcszFile = L"C:\\Users\\xx\\Desktop\\test.png"; info.pcszClass = NULL; info.oaifInFlags
= OAIF_ALLOW_REGISTRATION | OAIF_EXEC; SHOpenWithDialog(NULL, &info); return 0; }