如何让Windows根据文件关联打开外部文件?

发布于 2024-07-11 01:15:19 字数 202 浏览 5 评论 0原文

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

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

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

Using Win32-specific APIs, is there an easy way to start an external application to open a file simply by passing in the path/name of the file?

For example, say I have a file called C:\tmp\image.jpg. Is there a single API that I can call to tell Windows to open the application associated with .jpg files? Without having to do a bunch of registry lookups?

I thought I remembered doing this many years ago, but I cannot find it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独夜无伴 2024-07-18 01:15:19

ShellExecute

对指定文件执行操作。

语法

C++

HINSTANCE ShellExecute( 
    _In_opt_ HWND hwnd, 
    _In_opt_ LPCTSTR lpOperation, 
    _In_ LPCTSTR lpFile, 
    _In_opt_ LPCTSTR lp参数, 
    _In_opt_ LPCTSTR lp目录, 
    _In_ INT nShowCmd 
  ); 
  

参数

...

nShowCmd [输入]

类型:INT

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

ShellExecute

Performs an operation on a specified file.

Syntax

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
);

Parameters

...

nShowCmd [in]

Type: INT

The flags that specify how an application is to be displayed when it is opened. If lpFile specifies a document file, the flag is simply passed to the associated application. It is up to the application to decide how to handle it. These values are defined in Winuser.h...

葬花如无物 2024-07-18 01:15:19

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 文档以获取更多详细信息。

ShellExecute is the function you're looking for. It can handle both executable types and registered file types, and perform all sorts of actions (verbs) on the file, depending on what it supports.

The syntax is:

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.
);

Consult your friendly neighborhood MSDN documentation for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文