【问题标题】:How to ask Windows to open an external file based on file association?如何要求Windows根据文件关联打开外部文件?
【发布时间】:2008-12-14 23:14:53
【问题描述】:

使用特定于 Win32 的 API,是否有一种简单的方法可以启动外部应用程序来打开文件,只需传入文件的路径/名称?

例如,假设我有一个名为 C:\tmp\image.jpg 的文件。我可以调用一个 API 来告诉 Windows 打开与 .jpg 文件关联的应用程序吗?无需进行大量注册表查找?

我想我记得很多年前做过这个,但我找不到。

【问题讨论】:

    标签: windows winapi file-association


    【解决方案1】:

    ShellExecute

    对指定文件执行操作。

    语法

    C++

    HINSTANCE ShellExecute(
      _In_opt_ HWND    hwnd,
      _In_opt_ LPCTSTR lpOperation,
      _In_     LPCTSTR lpFile,
      _In_opt_ LPCTSTR lpParameters,
      _In_opt_ LPCTSTR lpDirectory,
      _In_     INT     nShowCmd
    );
    

    参数

    ...

    nShowCmd [in]

    类型:INT

    指定应用程序打开时如何显示的标志。如果 lpFile 指定了一个文档文件,则该标志被简单地传递给相关的应用程序。由应用程序决定如何处理它。这些值在 Winuser.h 中定义...

    【讨论】:

      【解决方案2】:

      ShellExecute 是您正在寻找的功能。它可以处理可执行类型和已注册的文件类型,对文件执行各种操作(动词),具体取决于它所支持的内容。

      语法是:

      HINSTANCE ShellExecute(
          HWND hwnd,            // handle to owner window.
          LPCTSTR lpOperation,  // verb to do, e.g., edit, open, print.
          LPCTSTR lpFile,       // file to perform verb on.
          LPCTSTR lpParameters, // parameters if lpFile is executable, else NULL.
          LPCTSTR lpDirectory,  // working directory or NULL for current directory.
          INT nShowCmd          // window mode e.g., SW_HIDE, SW_SHOWNORMAL.
      );
      

      有关更多详细信息,请参阅您友好的社区 MSDN 文档。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-03
        相关资源
        最近更新 更多