在C ++中以编程方式打开文档 [英] Opening a document programmatically in C++

查看:59
本文介绍了在C ++中以编程方式打开文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C ++编写的控制台程序.现在,我想在程序的用户每次在控制台中键入"manual"时都打开一个手动文档(.txt或.pdf).我怎样才能做到这一点?指向教程的任何链接都将有所帮助.谢谢

I have a console program written in C++. Now I want to open a manual document(in .txt or .pdf) everytime a user of the program types 'manual' in console. How can I do this? Any links to a tutorial would be helpful.Thanks

推荐答案

尝试将此代码( Open.cpp )编译为 Open.exe 然后,您可以使用(例如)以下参数执行它:

Try to compile this code (Open.cpp) to Open.exe Then, you can execute it with (for example) these parameters :

打开"C:\ your file.doc"

打开"C:\ your file.exe"

打开记事本

#include "windows.h"

int main(int argc, char *argv[])
{
    ShellExecute(GetDesktopWindow(), "open", argv[1], NULL, NULL, SW_SHOWNORMAL);
}

程序说明:

  1. 您应该首先包含Windows库( windows.h )来获取 ShellExecute GetDesktopWindow 函数.
  2. ShellExecute 是使用参数执行文件的功能 argv [1] 是要打开的文件的路径
  3. lpOperation 的另一个选项参数而不是"open" 是空值."explore" "find" 还有选择,但它们不是用于打开文件.
  4. SW_SHOWNORMAL 是以正常模式显示程序(不显示最小化或最大化)
  1. You should first include windows library (windows.h) to get ShellExecute and GetDesktopWindow function.
  2. ShellExecute is the function to execute the file with parameter argv[1] that is path to the file to be opened
  3. Another option for lpOperation arguments instead of "open" is NULL. "explore" and "find" are also the options but they are not for opening a file.
  4. SW_SHOWNORMAL is the constant to show the program in normal mode (not minimize or maximize)

这篇关于在C ++中以编程方式打开文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆