QT 用Windows的API函数,调用打开方式对话框

来源:互联网 发布:淘宝星级怎么快速升级 编辑:程序博客网 时间:2024/05/16 15:03

      下面的代码实现的功能是,先用Windows的ShellExecuteW函数以默认的方式打开文本文件,如果文件打开失败,则用WinExec函数调用打开方式对话框。

 

注意要引入头文件#include <windows.h>  和 #include <shellapi.h>


            QString file = "C:/新建/中文.txt";

            int nRes = 0;
           //以默认方式打开文件
   nRes = (int)ShellExecuteW(NULL
                                      , QString("open").toStdWString().c_str()
                                      , file.toStdWString().c_str()
                                      , NULL
                                      , NULL
                                      , SW_SHOW
                                      );
            int nRes2 = 0;
            char* cmd = QString("rundll32 shell32, OpenAs_RunDLL %1")
                       .arg(file)
                       .toLocal8Bit().data();
            if(nRes <= SE_ERR_NOASSOC )
            {//ShellExecuteW 返回值小于31表示执行出现错误,
//调用打开方式对话框
               nRes2 =WinExec(cmd
                              ,SW_SHOWNORMAL
                              );
            }

附相关资料:
http://baike.baidu.com/view/1044533.htm#sub1044533