C# WebBrowser.ShowPrintDialog() 不显示 [英] C# WebBrowser.ShowPrintDialog() not showing

查看:88
本文介绍了C# WebBrowser.ShowPrintDialog() 不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印 html 报告时遇到了这个特殊的问题.该文件本身是一个普通的本地 html 文件,位于我的硬盘上.

I have this peculiar problem while wanting to print a html-report. The file itself is a normal local html file, located on my hard drive.

为此,我尝试了以下方法:

To do this, I have tried the following:

    public static void PrintReport(string path)
    {
        WebBrowser wb = new WebBrowser();
        wb.Navigate(path);
        wb.ShowPrintDialog()
    }

我有一个带有点击事件按钮的表单:

And I have this form with a button with the click event:

    private void button1_Click(object sender, EventArgs e)
    {
        string path = @"D:\MyReport.html";
        PrintReport(path);
    }

这绝对没有任何作用.这有点奇怪......但事情变得奇怪......

This does absolutely nothing. Which is kind of strange... but things get stranger...

编辑打印功能时要做到以下几点:

When editing the print function to do the following:

    public static void PrintReport(string path)
    {
        WebBrowser wb = new WebBrowser();
        wb.Navigate(path);
        MessageBox.Show("TEST");
        wb.ShowPrintDialog()
    }

它有效.是的,只添加了一个 MessageBox.MessageBox 正在显示,然后出现打印对话框.我也尝试过使用 Thread.Sleep(1000) 代替,但它不起作用.谁能向我解释这里发生了什么?为什么消息框会有所作为?

It works. Yes, only adding a MessageBox. The MessageBox is showing and after it comes the print dialog. I have also tried with Thread.Sleep(1000) instead, which doesn't work. Can anyone explain to me what's going on here? Why would a messagebox make any difference?

这可能是某种权限问题吗?我在 Windows 7 和 8 上都复制了这个,同样的事情.我只用上面的代码制作了这个小应用程序来隔离问题.我很确定它可以在 Windows XP 上运行,因为我正在使用的应用程序的旧版本在它上面运行.当尝试直接使用 mshtml-dll 执行此操作时,我也会遇到问题.

Can it be some kind of permission problem? I've reproduced this on both Windows 7 and 8, same thing. I made this small application with only the above code to isolate the problem. I am quite sure it works on windows XP though, since an older version of the application I'm working on runs on it. When trying to do this directly with the mshtml-dll instead I also get problems.

非常感谢任何输入或澄清!

Any input or clarification is greatly appreciated!

推荐答案

问题是浏览器还没有准备好打印.您需要将事件处理程序 WebBrowserDocumentCompletedEventHandler 添加到 WebBrowser 对象.示例代码如下.

The problem is that the browser is not ready to print yet. You will want to add an event handler WebBrowserDocumentCompletedEventHandler to the WebBrowser Object. Sample code below.

public static void PrintReport(string path)
{
    WebBrowser wb = new WebBrowser();
    wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
    wb.Navigate(path);
}

public static void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    WebBrowser wb = (WebBrowser)sender;
    if (wb.ReadyState.Equals(WebBrowserReadyState.Complete))
        wb.ShowPrintDialog();
}

这篇关于C# WebBrowser.ShowPrintDialog() 不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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