我在这里找到了我这样使用的代码:
printDialog1.PrintDocument
(((IDocumentPaginatorSource)webBrowserMap.Document).DocumentPaginator, "Alchemy Map");
...但是我得到了两个错误消息,即:
0)'System.Windows.Forms.PrintDialog' does not contain a definition for 'PrintDocument' and no extension method 'PrintDocument' accepting a first argument of type 'System.Windows.Forms.PrintDialog' could be found (are you missing a using directive or an assembly reference?)
1) The type or namespace name 'IDocumentPaginatorSource' could not be found (are you missing a using directive or an assembly reference?)
然后我尝试以这种方式从这里调整代码:
private void buttonPrint_Click(object sender, EventArgs e)
{
WebBrowser wb = getCurrentBrowser();
wb.ShowPrintDialog();
}
private WebBrowser getCurrentBrowser()
{
return (WebBrowser)tabControlAlchemyMaps.SelectedTab.Controls[0];
}
...但是得到,“ System.InvalidCastException 未处理 HResult=-2147467262 消息=无法将类型为 'System.Windows.Forms.Button' 的对象转换为类型 'System.Windows.Forms.WebBrowser'。
然后,我尝试使用以下代码从我在这里找到的内容中得出:
private void buttonPrint_Click(object sender, EventArgs e)
{
WebBrowser web = getCurrentBrowser();
web.ShowPrintDialog();
}
private WebBrowser getCurrentBrowser()
{
// I know there's a better way, because there is only one WebBrowser control on the tab
foreach (var wb in tabControlAlchemyMaps.SelectedTab.Controls.OfType<WebBrowser>())
{
return wb;
}
return null;
}
...而且,虽然我可以逐步完成它,而且它似乎可以工作(我到达“web.ShowPrintDialog()”行,没有错误消息),但我看不到打印对话框。那么如何才能打印 WebBrowser 控件的内容呢?