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;
}
posted @ 2021-09-28 21:34  meteoric_cry  阅读(475)  评论(0编辑  收藏  举报