如何将应用程序设置为以编程方式打开某种类型文件的默认程序?

发布于 2024-07-09 12:46:59 字数 528 浏览 8 评论 0原文

MFC 中的程序生成了一个可执行文件,我想将其用作打开 .jpg 文件的默认程序。 也就是说,每次我双击 .jpg 文件时,我的程序就会运行。

我尝试添加一些将 .jpg 文件与我的程序链接的注册表项,例如 HKEY_CLASSES_ROOT\.jpg\shell\open\command (将其值设置为 " myProgram.exe" "%1") 和 HKEY_CLASSES_ROOT\myProgram

该方法工作得很好,除非某些其他应用程序自行注册以打开 .jpg 文件。 例如,我在计算机上安装了acdSee,因此每次我双击.jpg文件时,它总是启动acdSee而不是我自己的程序。 但是当我在我的程序中注册一个全新类型的文件时,它可以在程序中打开。 我不知道如何以编程方式将我的程序设置为已注册文件的默认打开程序。 谁能帮我解决这个问题吗? 非常感谢!

There's an executable file generated from my program in MFC and I want to use it as the default program to open the .jpg files. That is to say, each time I double click a .jpg file, my program will run.

I tried to add some registry entries linking .jpg files with my program, such as HKEY_CLASSES_ROOT\.jpg\shell\open\command (set its value to "myProgram.exe" "%1"), and HKEY_CLASSES_ROOT\myProgram.

The method works just fine except when some other applications register themselves to open the .jpg files. For example, I have installed acdSee on my computer, so each time I doule click a .jpg file, it always start acdSee instead of my own program. But when I register a completely new type of file with my program, it can be open in the program. I don't know how to set my program as the default opening program of an already registered file programmatically. Can anyone help me solve this problem? Thank you very much!

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

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

发布评论

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

评论(2

甜扑 2024-07-16 12:46:59

执行此操作的更典型/标准方法是将“.jpg”键的默认值设置为更清楚地标识文件类型的名称,然后在那里设置各种关联的操作。 因此,对于 jpg,您可以这样做:

HKCR\.jpg
   @default = MyApp.JpegImage
HKCR\MyApp.JpegImage\shell\open\command
   @default = "myApp.exe "%1""

如果其他程序决定注册该类型,它们会将 HKCR.jpg 的默认值替换为其他值,例如 OtherProgram.Jpg。 此时,您可以通过将值设置回 MyApp.JpegImage 将其重新注册到您的应用程序。

免责声明:在进行此类更改时,还请尽量尊重用户的偏好。 例如,在安装应用程序时,为用户提供设置或不设置此文件关联的选项。 如果用户愿意,您还可以从已安装的应用程序内部提供命令来重置关联。

如果您想向现有注册类型添加一些附加命令,则可以读取 .jpg 键的默认值来查找文件类型的名称。 然后您可以打开该键并将一个操作添加到现有的操作集中。 例如,您可以添加以下内容:

HKCR\ExistingApp.JpegImage\shell\myopen\
    @default = "Open with MyApp"
HKCR\ExistingApp.JpegImage\shell\myopen\command\
    @default = "myApp.exe "%1""

The more typical/standard way for doing this is to set the default value of the ".jpg" key to a name that identifies the file type more clearly, and then setup the various associated actions there. So for jpgs, you might do this:

HKCR\.jpg
   @default = MyApp.JpegImage
HKCR\MyApp.JpegImage\shell\open\command
   @default = "myApp.exe "%1""

If some other program decides to register the type, they will replace the default value for HKCR.jpg with some other value, like OtherProgram.Jpg. At that point, you could re-register it to your app by setting the value back to MyApp.JpegImage.

Disclaimer: When making this sort of change, please also try to respect the user's preferences. For instance, when installing your application, give the user the option to set this file association or not set it. You can also provide a command from inside your installed application to reset the associations, if the user should wish to do so.

If you instead wanted to add some additional commands to an existing registered type, you would read the default value of the .jpg key to find the name of the file type. Then you could open that key and add an action to the existing set of actions. For instance, you could add the following:

HKCR\ExistingApp.JpegImage\shell\myopen\
    @default = "Open with MyApp"
HKCR\ExistingApp.JpegImage\shell\myopen\command\
    @default = "myApp.exe "%1""
蓝海似她心 2024-07-16 12:46:59

请注意,通过将密钥写入 HKCR,您实际上是在写入 HKLM\Software\Classes。 这将需要管理权限。 但是,您可以通过将密钥写入 HKCU\Classes\Root 来在用户上下文中进行每个用户的更改。

此外,HKCU 中的用户首选项将覆盖 HKLM 中的系统默认设置,这听起来像是您的问题所在。

这是当程序尚未将扩展注册为“默认”时(该程序是否在控制面板的“设置程序和默认值”中列出?)

启动时间 阅读文档

Note that by writing a key to HKCR, you're actually writing to HKLM\Software\Classes. This will require administrative privileges. However, you can make per-user changes within a user context by writing your keys to HKCU\Classes\Root instead.

Also, user preferences in HKCU will override the system defaults in HKLM, which sounds like what your problem might be.

This is when a program has not registered an extension as a "Default" (Is the program listed in Set Programs and Defaults in the Control Panel?)

Time to start reading documentation!

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