以编程方式更改 WinForms WebBrowser 控件的目标打印机 [英] Programmatically changing the destination printer for a WinForms WebBrowser control

查看:15
本文介绍了以编程方式更改 WinForms WebBrowser 控件的目标打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一个不可见的 WebBrowser 控件来打印一个非常简单的 HTML 文档.我们的应用程序要求我们能够以这种方式打印多个文档,并且它们都可以发送到不同的打印机.不幸的是,我在将输出发送到正确的打印机方面并没有很成功.

I'm trying to use an invisible WebBrowser control to print a very simple HTML document. Our application requires that we be able to print several documents this way, and that they all can be sent to different printers. Unfortunately, I haven't been very successful in making the output go to the right printer.

它现在的工作方式是在打印文档之前,应用程序确定接收它的打印机,并相应地设置默认打印机.为此,它使用从 WinSpool.drv 导入的 SetDefaultPrinter().如果我在调试模式下执行代码,我可以清楚地看到默认打印机发生了变化(并且此更改反映在控制面板 UI 中),但 WebBrowser 仍然坚持使用原始默认打印机.

The way it works right now is that before printing a document, the application determines which printer is to receive it, and sets the default printer accordingly. To do this it uses SetDefaultPrinter() imported from WinSpool.drv. If I step the code in debug mode I can clearly see that the default printer changes (and this change is reflected in the control panel UI), but the WebBrowser still insists on using the original default printer.

据我所知,MSDN 文档并未真正为这种情况提供解决方案.我将不胜感激有关如何以编程方式完成此任务的一些意见.

The MSDN documentation, from what I've seen, doesn't really provide a solution for this scenario. I would greatly appreciate some input on how I can accomplish this programmatically.

推荐答案

鉴于您所说的,也许如果您重新启动包含 Web 浏览器控件的进程(或web 浏览器控件),更改默认打印机后?这就是 我看到的那种事情例如,发生在这里.

Given what you've said, perhaps if you restart the process which contains the web browser control (or the process which is the web browser control), after you change the default printer? That's the kind of thing I see happening here, for example.

我认为可以分出一个进行实际打印的后台进程,但我真的希望有一个更简单的解决方案.

I suppose it would be possible to fork off a background process that does the actual printing, but I'm really hoping for a simpler solution.

分叉我对可能最简单的解决方案的第一个想法.

Forking was my first thought towards a probably-simplest solution.

其他一些替代方案如下.

Some other alternatives are as follows.

1).Webbrowser 控件包装的 IE 通过 ActiveX 公开 API.其/那些 API 之一可能会让您指定目标打印机.

1). IE, which the webbrowser control is wrapping, exposes APIs via ActiveX. One of its/those APIs might let you specify the destination printer.

2).一些可执行文件(我不了解 IE)在注册表中有 printto 条目.例如,Acrobat Reader 有一个条目,其值如下:

2). Some executables (I don't know about IE) have printto entries in the registry. For example, Acrobat Reader has an entry whose value is as follows:

""C:Program FilesAdobeReader 9.0ReaderAcroRd32.exe""  /t "%1" "%2" "%3" "%4"

这用于指定命令行的语法,您可以使用它来打印和指定(非默认)打印机.您也可以在 Google 上搜索 printto,参见例如ShellExecute 中的 PrintTo 命令.

That's used for specifying the syntax of the command-line which you can use to print and specify a (non-default) printer. You can also Google for printto, see e.g. PrintTo command in the ShellExecute.

3).我已经实现了我自己的 HTML 控件 .NET,它不依赖于 IE.你说你的 HTML(和我认为的 CSS)很简单,所以也许 我可以渲染它,要么开箱即用,要么只需要一点额外的开发工作.我不支持打印,但是打印对于用户控件来说很容易实现.让我为您实现它会花费您数百美元,但谁知道呢,也许这对您来说是值得的.这将是一个非常轻量级的解决方案,并且可能得到很好的支持.如果你想进一步讨论,你可以给我发电子邮件.

3). I have implemented an HTML control for .NET of my own, which doesn't depend on IE. You say that your HTML (and CSS I presume) are simple, so perhaps I can render it, either out of the box or with only a little extra development effort. I don't support printing, but printing is quite easy for a user control to implement. Getting me to implement that for you would cost you several hundred but, who knows, maybe it's worth it to you. It would be quite a light-weight solution, and perhaps well supported. You could email me if you want to discuss that further.

4).您可能还会发现其他控件,与我的类似,或多或少有名/昂贵;或其他应用程序,例如OpenOffice 等

4). You might also find other controls, similar to mine, more or less famous/expensive; or other applications, e.g. OpenOffice etc etc.

5).您可以考虑将 HTML(以某种方式)转换为另一种格式(例如 PDF),您的应用程序可以为其提供更好的打印支持.

5). You could consider converting the HTML (somehow) to another format (e.g. PDF) for which you have an application which gives you better support for printing.

这篇关于以编程方式更改 WinForms WebBrowser 控件的目标打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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