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

查看:71
本文介绍了以编程方式更改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浏览器控件的过程(或网络浏览器控件),更改默认打印机后?那就是我看到的那种东西例如.

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).Web浏览器控件包装的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 Files\Adobe\Reader 9.0\Reader\AcroRd32.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).我已经实现了自己的用于.NET的HTML控件,它不依赖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天全站免登陆