如何使用WebBrowser控件打印css应用的背景图像

时间:2022-11-11 23:07:22

I am using the webbrowser control in winforms and discovered now that background images which I apply with css are not included in the printouts.

我在winforms中使用webbrowser控件,现在发现我用css应用的背景图像不包含在打印输出中。

Is there a way to make the webbrowser print the background of the displayed document too?

有没有办法让webbrowser打印出所显示文档的背景?

Edit: Since I wanted to do this programatically, I opted for this solution:

编辑:由于我想以编程方式执行此操作,因此我选择了此解决方案:

using Microsoft.Win32;

...

RegistryKey regKey = Registry.CurrentUser
                    .OpenSubKey("Software")
                    .OpenSubKey("Microsoft")
                    .OpenSubKey("Internet Explorer")
                    .OpenSubKey("Main");

//Get the current setting so that we can revert it after printjob
var defaultValue = regKey.GetValue("Print_Background");
regKey.SetValue("Print_Background", "yes");

//Do the printing

//Revert the registry key to the original value
regKey.SetValue("Print_Background", defaultValue);

Another way to handle this might be to just read the value, and notify the user to adjust this himself before printing. I have to agree that tweaking with the registry like this is not a good practice, so I am open for any suggestions.

另一种处理方法可能是只读取值,并通知用户在打印前自行调整。我不得不同意像这样调整注册表并不是一个好习惯,所以我愿意接受任何建议。

Thanks for all your feedback

感谢您的所有反馈

5 个解决方案

#1


1  

If you're going to go and change an important system setting, make sure to first read the current setting and restore it when you are done.

如果您要更改重要的系统设置,请务必首先阅读当前设置并在完成后将其恢复。

I consider this very bad practice in the first place, but if you must do it then be kind.

我首先考虑这种非常糟糕的做法,但如果你必须这样做,那就要善良了。

Registry.LocalMachine

Also, try changing LocalUser instead of LocalMachine - that way if your app crashes (and it will), then you'll only confounded the user, not everyone who uses the machine.

此外,尝试更改LocalUser而不是LocalMachine - 如果您的应用程序崩溃(它会),那么您只会混淆用户,而不是每个使用该机器的人。

#2


2  

Another registry key would be : HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup\Print_Background HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\PageSetup\Print_Background

另一个注册表项是:HKEY_CURRENT_USER \ Software \ Microsoft \ Internet Explorer \ PageSetup \ Print_Background HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Internet Explorer \ PageSetup \ Print_Background

#3


1  

The corresponding HKCU key for this setting is: HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\Print_Background

此设置的相应HKCU密钥为:HKEY_CURRENT_USER \ Software \ Microsoft \ Internet Explorer \ Main \ Print_Background

#4


0  

By default, the browser does not print background images at all.

默认情况下,浏览器根本不打印背景图像。

In Firefox

* File > Page Setup > Check Off "Print Background"
* File > Print Preview

In IE

* Tools > Internet Options > Advanced > Printing
* Check Off "Print Background Images and Colors"

In Opera

* File > Print Options > Check Off "Print Page Background"
* File > Print Preview (You may have to scroll down/up to see it refresh)

#5


0  

var sh = new ActiveXObject("WScript.Shell");
key = "HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\Main\\Print_Background";
var defaultValue = sh.RegRead(key); 
sh.RegWrite(key,"yes","REG_SZ");
document.frames['detailFrame'].focus(); 
document.frames['detailFrame'].print();
sh.RegWrite(key,defaultValue,"REG_SZ");  
return false; 

#1


1  

If you're going to go and change an important system setting, make sure to first read the current setting and restore it when you are done.

如果您要更改重要的系统设置,请务必首先阅读当前设置并在完成后将其恢复。

I consider this very bad practice in the first place, but if you must do it then be kind.

我首先考虑这种非常糟糕的做法,但如果你必须这样做,那就要善良了。

Registry.LocalMachine

Also, try changing LocalUser instead of LocalMachine - that way if your app crashes (and it will), then you'll only confounded the user, not everyone who uses the machine.

此外,尝试更改LocalUser而不是LocalMachine - 如果您的应用程序崩溃(它会),那么您只会混淆用户,而不是每个使用该机器的人。

#2


2  

Another registry key would be : HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup\Print_Background HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\PageSetup\Print_Background

另一个注册表项是:HKEY_CURRENT_USER \ Software \ Microsoft \ Internet Explorer \ PageSetup \ Print_Background HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Internet Explorer \ PageSetup \ Print_Background

#3


1  

The corresponding HKCU key for this setting is: HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\Print_Background

此设置的相应HKCU密钥为:HKEY_CURRENT_USER \ Software \ Microsoft \ Internet Explorer \ Main \ Print_Background

#4


0  

By default, the browser does not print background images at all.

默认情况下,浏览器根本不打印背景图像。

In Firefox

* File > Page Setup > Check Off "Print Background"
* File > Print Preview

In IE

* Tools > Internet Options > Advanced > Printing
* Check Off "Print Background Images and Colors"

In Opera

* File > Print Options > Check Off "Print Page Background"
* File > Print Preview (You may have to scroll down/up to see it refresh)

#5


0  

var sh = new ActiveXObject("WScript.Shell");
key = "HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\Main\\Print_Background";
var defaultValue = sh.RegRead(key); 
sh.RegWrite(key,"yes","REG_SZ");
document.frames['detailFrame'].focus(); 
document.frames['detailFrame'].print();
sh.RegWrite(key,defaultValue,"REG_SZ");  
return false;