【问题标题】:What does the win32api.ShellExecute() function do?win32api.ShellExecute() 函数有什么作用?
【发布时间】:2021-01-24 05:57:51
【问题描述】:

我正在学习如何在 python 中打印文件。我找到了很多方法来做到这一点,我见过的最常见的方法之一是使用 win32api 模块。

import win32api
win32api.ShellExecute(0, "print", path_for_file , None, ".", 0)

当我运行这个程序时,文件打印出来没有任何问题。

但问题是我不明白win32api.ShellExecute() 函数中实际发生了什么以及它的参数的功能是什么。 论据,我的意思是:(0, "print", path_for_file , None, ".", 0)

谁能解释一下win32api.ShellExecute() 函数中的每个参数的作用?

如果有人可以帮助我,那就太好了。

【问题讨论】:

  • 你在找对应的winapi文档吗? docs.microsoft.com/en-us/windows/win32/api/shellapi/…
  • 正如我所说,如果您不知道窗口所有权是什么,以及它对对话框的影响,那么您将不会得到这种格式的答案。你需要做一些研究。
  • 也就是说,您的打印方法非常不稳定,以至于您最不关心父窗口句柄。

标签: python winapi printing


【解决方案1】:

基于ShellExecute 文档:

ShellExecute(0,              // NULL since it's not associated with a window
             "print",        // execute the "print" verb defined for the file type
             path_for_file,  // path to the document file to print
             None,           // no parameters, since the target is a document file
             ".",            // current directory, same as NULL here
             0)              // SW_HIDE passed to app associated with the file type

简而言之,这与在 Windows 资源管理器中右键单击 path_for_file 文档,然后从上下文菜单中选择 print 执行相同的操作。与文件类型关联的应用程序使用print 动词和SW_HIDE show 命令执行,这通常意味着它将静默打印文档,而不显示任何 UI。

【讨论】:

  • 您好,感谢您的解释,但我不明白第一个、第四个和第五个参数的用法。您能否更详细地解释一下三个参数的功能?
  • @Lenovo360 第一次见Handle for ShellExecute() - Parent Window?(tl;dr 最常见的是NULL)。第 4 次,如果您使用 ShellExecute 运行可执行文件,而不是逐个关联,那么这些参数将在命令行上传递给目标可执行文件(启动文档文件时不适用)。更多关于 shell 对象和动词的信息,例如Launching Applications。
  • ... 第 5 次,用于为目标进程设置 current working directory,并在那里传递完整路径将允许 path-to-file 使用相对路径。但是,在您的示例中,"." 是当前工作目录,因此它与传递NULL 具有相同的效果。
猜你喜欢
  • 1970-01-01
  • 2018-11-19
  • 2014-05-23
  • 2017-06-19
  • 2020-06-06
  • 2017-06-25
  • 2012-07-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多