在C#Winforms对话框中使用WebBrowser控件

时间:2016-12-23 09:43:34

标签: c# .net winforms dialog

我目前正在尝试在一个简单的弹出对话框窗口中实现HTML查看器(无用户交互,只显示格式化文本)。

调用时会抛出' ThreadStateException '。据我所知,这可以通过将 [STAThread] 附加到程序的主入口点来修复,但这是目前可用的选项。

创建WebBrowser对话框的代码是:

    public static void ShowUserHTMLDialog(string formTitle, string formLabel, int formWidth, int formHeight, string html)
    {
        var webBrowser = new WebBrowser();

        var form = CreateBasicForm(formTitle, formLabel, formWidth, formHeight);

        webBrowser.Name = "webWindow";
        webBrowser.Size = new System.Drawing.Size(formWidth, formHeight - 70);
        webBrowser.Location = new System.Drawing.Point(0, 35);
        webBrowser.AllowWebBrowserDrop = false;

        webBrowser.DocumentText = html;

        form.Controls.Add(webBrowser);

        DialogResult windowResult = form.ShowDialog();
    }

    static void Main(string[] args)
    {
        // Test program for various dialogs defined in class library.

        string html = "<h1>Test Window</h1>"
            + "<p>This is some more text.</p>"
            + "<ul>"
            + "<li>Item 1</li>"
            + "<li>Item 2</li>"
            + "</ul>";

        Dialog.ShowUserHTMLDialog("TEST", "TEST", 640, 480, html);
    }

2 个答案:

答案 0 :(得分:0)

只需创建一个自己的表单,使用这些参数并打开它(不知道你的标签是什么意思)^^

public partial class Form2 : Form
{
    public Form2(string formTitle, string formLabel, int formWidth, int formHeight, string html)
    {
        InitializeComponent();
        Text = formTitle;
        Width = formWidth;
        Height = formHeight;
        webBrowser1.DocumentText = html;
    }
}

这样称呼它(show或showdialog,取决于你^^)

new Form2("title", "label", 1000, 1000, "<h1>Test Window</h1>"
        + "<p>This is some more text.</p>"
        + "<ul>"
        + "<li>Item 1</li>"
        + "<li>Item 2</li>"
        + "</ul>").Show();

修改

通过将控件拖到窗体上添加

webbrowser ^^

再次修改 喜欢这个sry ^^

        Thread t = new Thread(() =>
        {
            new Form1("title", "label", 1000, 1000, "<h1>Test Window</h1>"
                + "<p>This is some more text.</p>"
                + "<ul>"
                + "<li>Item 1</li>"
                + "<li>Item 2</li>"
                + "</ul>").ShowDialog();
        });
        t.SetApartmentState(ApartmentState.STA);
        t.Start();
        Console.Read();

答案 1 :(得分:0)

工作解决方案:

    public static bool ShowUserHTMLDialog(string formTitle, string formLabel, int formWidth, int formHeight, string html)
    {
        DialogResult formResult = DialogResult.Abort;

        var thread = new Thread
            (
                () =>
                {
                    var webBrowser = new WebBrowser();

                    var form = CreateBasicForm(formTitle, formLabel, formWidth, formHeight);

                    webBrowser.Name = "webWindow";
                    webBrowser.Size = new System.Drawing.Size(formWidth, formHeight - 70);
                    webBrowser.Location = new System.Drawing.Point(0, 35);
                    webBrowser.AllowWebBrowserDrop = false;

                    webBrowser.DocumentText = html;

                    form.Controls.Add(webBrowser);

                    formResult = form.ShowDialog();
                }
            );
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
        thread.Join();

        return (formResult == DialogResult.OK);
    }